How best to run one-off migration tasks in a kubernetes cluster

后端 未结 3 444
生来不讨喜
生来不讨喜 2021-01-31 09:01

I have database migrations which I\'d like to run before deploying a new version of my app into a Kubernetes cluster. I want these migrations to be run automatically as part of

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 09:59

    Considering the age of this question I'm not sure if initContainers were available at the time but they are super helpful now.

    https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

    The way I recently set this up was to have a postgres pod and our django application running in the same namespace, however the django pod has 3 initContainers:

    1. init-migrations
    2. init-fixtures
    3. init-createsuperUser

    What this will do is run the django pod and the postgres pod in parallel but also continuously run the initContainers until the postgres pod comes up and then your migrations should run.

    As for the pods perpetually restarting, maybe they've fixed the restartPolicy by now. I'm currently pretty new to kubernetes but this is what I've found works for me.

提交回复
热议问题