Django 迁移通过 Kubernetes Job 和持久卷索赔

huangapple go评论98阅读模式
英文:

Django migrations by Kubernetes Job and persistent Volume Claim

问题

以下是翻译好的部分:

最佳方法是在Kubernetes上使用作业(Job)和持久卷声明(Persistent Volume Claim)来进行迁移和迁移模型的操作,用于Django部署应用程序。

持久卷声明(Persistent Volume Claim)

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: csi-pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. resources:
  9. requests:
  10. storage: 5Gi
  11. storageClassName: do-block-storage

作业(Job)

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: django-migrations-job
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - name: app
  10. image: user/app:latest
  11. command: ["/bin/sh", "-c"]
  12. args: ["python manage.py makemigrations app; python manage.py migrate"]
  13. volumeMounts:
  14. - mountPath: "/container-code-dir/app/migrations"
  15. name: my-do-volume
  16. volumes:
  17. - name: my-do-volume
  18. persistentVolumeClaim:
  19. 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

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: csi-pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. resources:
  9. requests:
  10. storage: 5Gi
  11. storageClassName: do-block-storage

Job

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: django-migrations-job
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - name: app
  10. image: user/app:latest
  11. command: ["/bin/sh", "-c"]
  12. args: ["python manage.py makemigrations app; python manage.py migrate"]
  13. volumeMounts:
  14. - mountPath: "/container-code-dir/app/migrations"
  15. name: my-do-volume
  16. volumes:
  17. - name: my-do-volume
  18. persistentVolumeClaim:
  19. claimName: csi-pvc

答案1

得分: 3

看起来对我来说没问题。不确定你是需要每次新的 Pod 启动时运行这个作业,还是只需运行一次?

如果需要在 Django 服务 Pod 启动之前每次运行,也许你可以使用 Init Containers 来获取帮助。

示例:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: myapp-pod
  5. labels:
  6. app: myapp
  7. spec:
  8. containers:
  9. - name: myapp-container
  10. image: busybox:1.28
  11. command: ['sh', '-c', 'echo 应用正在运行! && sleep 3600']
  12. initContainers:
  13. - name: init-myservice
  14. image: busybox:1.28
  15. command: ['sh', '-c', 'until nslookup myservice; do echo 等待 myservice; sleep 2; done;']
  16. - name: init-mydb
  17. image: busybox:1.28
  18. 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:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: myapp-pod
  5. labels:
  6. app: myapp
  7. spec:
  8. containers:
  9. - name: myapp-container
  10. image: busybox:1.28
  11. command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  12. initContainers:
  13. - name: init-myservice
  14. image: busybox:1.28
  15. command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  16. - name: init-mydb
  17. image: busybox:1.28
  18. command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

you can do the same for deployment

huangapple
  • 本文由 发表于 2020年1月4日 01:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定