英文:
Django migrations by Kubernetes Job and persistent Volume Claim
问题
以下是翻译好的部分:
最佳方法是在Kubernetes上使用作业(Job)和持久卷声明(Persistent Volume Claim)来进行迁移和迁移模型的操作,用于Django部署应用程序。
持久卷声明(Persistent Volume Claim)
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
英文:
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
答案1
得分: 3
看起来对我来说没问题。不确定你是需要每次新的 Pod 启动时运行这个作业,还是只需运行一次?
如果需要在 Django 服务 Pod 启动之前每次运行,也许你可以使用 Init Containers 来获取帮助。
示例:
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo 应用正在运行! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'until nslookup myservice; do echo 等待 myservice; sleep 2; done;']
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', 'until nslookup mydb; do echo 等待 mydb; sleep 2; done;']
你可以对部署(deployment)也做同样的操作。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论