Is the best approach to make migrations and migrate models using a Job and a Persistent Volume Claim on Kubernetes Django deployed app?
Persistent Volume
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: do-block-storage
Job
apiVersion: batch/v1
kind: Job
metadata:
name: django-migrations-job
spec:
template:
spec:
containers:
- name: app
image: user/app:latest
command: ["/bin/sh", "-c"]
args: ["python manage.py makemigrations app; python manage.py migrate"]
volumeMounts:
- mountPath: "/container-code-dir/app/migrations"
name: my-do-volume
volumes:
- name: my-do-volume
persistentVolumeClaim:
claimName: csi-pvc
Looks fine for me. Not sure if you need run this job once or every time, when a new pod is up?
If it is running before Django service pod started every time, maybe you can get help with Init Containers
Example:
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
you can do the same for deployment