From bf2ee27b42e089645c8fedf0e225339b4ec7a4ea Mon Sep 17 00:00:00 2001 From: Claude Dioudonnat Date: Wed, 20 Mar 2024 18:39:44 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(jenny)=20add=20nginx=20in=20the=20?= =?UTF-8?q?pod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Django required to collect the static and service with a HTTP server. We add an Nginx container in the pods and add an init container to collect this statics. And use an ephemeral volume for static instead of a persistent one. --- jenny/templates/configmap.yaml | 90 +++++++++++++++++++++++++++++++++ jenny/templates/deployment.yaml | 59 ++++++++++++++++----- jenny/templates/ingress.yaml | 2 +- jenny/templates/jobs.yaml | 7 --- jenny/templates/pvc.yaml | 15 ------ jenny/templates/service.yaml | 7 ++- jenny/values.yaml | 19 ++----- 7 files changed, 146 insertions(+), 53 deletions(-) create mode 100644 jenny/templates/configmap.yaml diff --git a/jenny/templates/configmap.yaml b/jenny/templates/configmap.yaml new file mode 100644 index 0000000..68cf028 --- /dev/null +++ b/jenny/templates/configmap.yaml @@ -0,0 +1,90 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: "{{ template "jenny.fullname" . }}-nginx" +data: + config: | + # nginx.conf + user nobody nogroup; + # 'user nobody nobody;' for systems with 'nobody' as a group instead + error_log /var/log/nginx/error.log warn; + pid /var/run/nginx.pid; + + events { + worker_connections 1024; # increase if you have lots of clients + accept_mutex off; # set to 'on' if nginx worker_processes > 1 + # 'use epoll;' to enable for Linux 2.6+ + # 'use kqueue;' to enable for FreeBSD, OSX + } + + http { + upstream django_app { + server localhost:{{ .Values.django.port }}; + } + + server { + listen {{ .Values.nginx.port }}; + + root /usr/share/nginx; + location / { + # checks for static file, if not found proxy to app + try_files $uri @proxy_to_app; + } + + location @proxy_to_app { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + proxy_pass http://django_app; + } + } + + types { + text/html html htm shtml; + text/css css; + text/xml xml rss; + image/gif gif; + image/jpeg jpeg jpg; + application/x-javascript js; + text/plain txt; + text/x-component htc; + text/mathml mml; + image/png png; + image/x-icon ico; + image/x-jng jng; + image/vnd.wap.wbmp wbmp; + application/java-archive jar war ear; + application/mac-binhex40 hqx; + application/pdf pdf; + application/x-cocoa cco; + application/x-java-archive-diff jardiff; + application/x-java-jnlp-file jnlp; + application/x-makeself run; + application/x-perl pl pm; + application/x-pilot prc pdb; + application/x-rar-compressed rar; + application/x-redhat-package-manager rpm; + application/x-sea sea; + application/x-shockwave-flash swf; + application/x-stuffit sit; + application/x-tcl tcl tk; + application/x-x509-ca-cert der pem crt; + application/x-xpinstall xpi; + application/zip zip; + application/octet-stream deb; + application/octet-stream bin exe dll; + application/octet-stream dmg; + application/octet-stream eot; + application/octet-stream iso img; + application/octet-stream msi msp msm; + audio/mpeg mp3; + audio/x-realaudio ra; + video/mpeg mpeg mpg; + video/quicktime mov; + video/x-flv flv; + video/x-msvideo avi; + video/x-ms-wmv wmv; + video/x-ms-asf asx asf; + video/x-mng mng; + } + } diff --git a/jenny/templates/deployment.yaml b/jenny/templates/deployment.yaml index 97a2382..d2140b6 100644 --- a/jenny/templates/deployment.yaml +++ b/jenny/templates/deployment.yaml @@ -1,7 +1,8 @@ +--- apiVersion: apps/v1 kind: Deployment metadata: - name: {{ template "jenny.fullname" . }} + name: "{{ template "jenny.fullname" . }}" namespace: {{ .Release.Namespace | quote }} labels: {{- include "jenny.labels" . | nindent 4 }} @@ -22,41 +23,75 @@ spec: {{- include "jenny.selectorLabels" . | nindent 8 }} spec: {{- include "django.imagePullSecrets" . | nindent 6 }} - containers: - - name: {{ .Chart.Name }} + initContainers: + - name: "{{ .Chart.Name }}-collectstatic" image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} command: - {{- toYaml .Values.django.command | nindent 12 }} + - "python" + - "manage.py" + - "collectstatic" + - "--no-input" + volumeMounts: + - name: "static" + mountPath: /app/static + containers: + - name: "{{ .Chart.Name }}-django" + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} resources: {{- toYaml .Values.resources | nindent 12 }} livenessProbe: httpGet: - path: /__lbheartbeat__ + path: / port: {{ .Values.django.port }} + httpHeaders: + - name: "Host" + value: "{{ .Values.ingress.host}}" initialDelaySeconds: 15 periodSeconds: 30 readinessProbe: httpGet: - path: /__heartbeat__ + path: / port: {{ .Values.django.port }} + httpHeaders: + - name: "Host" + value: "{{ .Values.ingress.host}}" initialDelaySeconds: 5 periodSeconds: 5 env: {{- include "jenny.envs" . | nindent 12 }} + - name: "UWSGI_PORT" + value: "{{ .Values.django.port }}" {{- if .Values.persistence.enabled }} volumeMounts: - - name: {{ .Values.volumes.static.name }} - mountPath: {{ .Values.volumes.static.mountPath }} + - name: static + mountPath: /app/static - name: {{ .Values.volumes.media.name }} mountPath: {{ .Values.volumes.media.mountPath }} + - name: "{{ .Chart.Name }}-nginx" + image: nginx:1.25.4-alpine + volumeMounts: + - name: "static" + mountPath: /usr/share/nginx/static + - name: {{ .Values.volumes.media.name }} + mountPath: /usr/share/nginx/media + - name: nginx + mountPath: /etc/nginx + readOnly: true volumes: - - name: {{ .Values.volumes.static.name }} - persistentVolumeClaim: - claimName: {{ .Values.volumes.static.claimName }} - name: {{ .Values.volumes.media.name }} persistentVolumeClaim: claimName: {{ .Values.volumes.media.claimName }} + - name: nginx + configMap: + items: + - key: config + path: nginx.conf + name: "{{ .Chart.Name }}-nginx" + - name: "static" + emptyDir: + sizeLimit: 50Mi {{- end }} {{- with .Values.nodeSelector }} nodeSelector: @@ -69,4 +104,4 @@ spec: {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 8 }} - {{- end }} + {{- end }} \ No newline at end of file diff --git a/jenny/templates/ingress.yaml b/jenny/templates/ingress.yaml index e57bf97..e4d2510 100644 --- a/jenny/templates/ingress.yaml +++ b/jenny/templates/ingress.yaml @@ -34,5 +34,5 @@ spec: service: name: {{ include "jenny.fullname" . }}-service port: - number: {{ .Values.service.port }} + number: {{ .Values.nginx.port }} {{- end }} diff --git a/jenny/templates/jobs.yaml b/jenny/templates/jobs.yaml index b44ba84..9318642 100644 --- a/jenny/templates/jobs.yaml +++ b/jenny/templates/jobs.yaml @@ -32,19 +32,12 @@ spec: {{- if $.Values.resources }} resources: {{ toYaml $.Values.resources | nindent 12 }} {{- end }} - {{- if $.Values.persistence.enabled }} volumeMounts: - - name: {{ $.Values.volumes.static.name }} - mountPath: {{ $.Values.volumes.static.mountPath }} - name: {{ $.Values.volumes.media.name }} mountPath: {{ $.Values.volumes.media.mountPath }} volumes: - - name: {{ $.Values.volumes.static.name }} - persistentVolumeClaim: - claimName: {{ $.Values.volumes.static.claimName }} - name: {{ $.Values.volumes.media.name }} persistentVolumeClaim: claimName: {{ $.Values.volumes.media.claimName }} - {{- end }} restartPolicy: Never {{- end }} diff --git a/jenny/templates/pvc.yaml b/jenny/templates/pvc.yaml index 69fdc09..6844db2 100644 --- a/jenny/templates/pvc.yaml +++ b/jenny/templates/pvc.yaml @@ -13,19 +13,4 @@ spec: resources: requests: storage: {{ .Values.persistence.size }} ---- -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: {{ .Values.volumes.static.claimName }} - namespace: {{ .Release.Namespace | quote }} -spec: - accessModes: - {{ with .Values.persistence.accessModes }} - {{- toYaml . | indent 4 }} - {{- end }} - storageClassName: {{ .Values.persistence.storageClass }} - resources: - requests: - storage: {{ .Values.persistence.size }} {{- end }} diff --git a/jenny/templates/service.yaml b/jenny/templates/service.yaml index 5bc1f96..2dbb6f2 100644 --- a/jenny/templates/service.yaml +++ b/jenny/templates/service.yaml @@ -3,13 +3,12 @@ apiVersion: v1 kind: Service metadata: name: {{ include "jenny.fullname" . }}-service - namespace: {{ .Values.namespace }} labels: {{- include "jenny.labels" . | nindent 4 }} spec: ports: - - name: "{{ .Values.service.port }}-tcp" - port: {{ .Values.service.port }} - targetPort: {{ .Values.django.port }} + - name: "{{ .Values.nginx.port }}-tcp" + port: {{ .Values.nginx.port }} + targetPort: {{ .Values.nginx.port }} protocol: TCP {{- if (eq .Values.service.type "NodePort") }} nodePort: {{ .Values.service.nodePort.http }} diff --git a/jenny/values.yaml b/jenny/values.yaml index fe27ad2..01ea953 100644 --- a/jenny/values.yaml +++ b/jenny/values.yaml @@ -10,8 +10,7 @@ image: replicaCount: 1 -imagePullSecrets: ["regcred"] - +imagePullSecrets: [] podAnnotations: {} @@ -60,13 +59,7 @@ nodeSelector: {} tolerations: [] -resources: {} - volumes: - static: - name: "jenny-static" - claimName: "jenny-static-pvc" - mountPath: "/app/static" media: name: "jenny-media" claimName: "jenny-media-pvc" @@ -75,10 +68,6 @@ volumes: django: fullname: jenny port: 8080 - command: - - uwsgi - - "--http=:8080" - - "--module=jenny.wsgi" settings: "jenny.settings" configuration: "Base" allowed_hosts: "*" @@ -90,5 +79,7 @@ django: jobs: - name: dbmigrate command: ["python", "manage.py", "migrate", "--no-input"] - - name: collectstatic - command: ["python", "manage.py", "collectstatic", "--no-input"] + resources: {} + +nginx: + port: 8000 \ No newline at end of file