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

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

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

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:

确定