Skip to content

Commit

Permalink
🔧(jenny) add nginx in the pod
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
claudusd committed Mar 20, 2024
1 parent 8751063 commit bf2ee27
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 53 deletions.
90 changes: 90 additions & 0 deletions jenny/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -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;
}
}
59 changes: 47 additions & 12 deletions jenny/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
Expand All @@ -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:
Expand All @@ -69,4 +104,4 @@ spec:
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion jenny/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ spec:
service:
name: {{ include "jenny.fullname" . }}-service
port:
number: {{ .Values.service.port }}
number: {{ .Values.nginx.port }}
{{- end }}
7 changes: 0 additions & 7 deletions jenny/templates/jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
15 changes: 0 additions & 15 deletions jenny/templates/pvc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
7 changes: 3 additions & 4 deletions jenny/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
19 changes: 5 additions & 14 deletions jenny/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ image:

replicaCount: 1

imagePullSecrets: ["regcred"]

imagePullSecrets: []

podAnnotations: {}

Expand Down Expand Up @@ -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"
Expand All @@ -75,10 +68,6 @@ volumes:
django:
fullname: jenny
port: 8080
command:
- uwsgi
- "--http=:8080"
- "--module=jenny.wsgi"
settings: "jenny.settings"
configuration: "Base"
allowed_hosts: "*"
Expand All @@ -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

0 comments on commit bf2ee27

Please sign in to comment.