3

在 Kubernetes Django 部署的应用程序上使用 Job 和 Persistent Volume Claim 进行迁移和迁移模型的最佳方法是什么?

持久卷

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: csi-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: do-block-storage

工作

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
4

1 回答 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 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;']

你可以为部署做同样的事情

于 2020-01-04T02:25:34.887 回答