How to handle database migrations with Kubernetes and Skaffold How to handle database migrations with Kubernetes and Skaffold flask flask

How to handle database migrations with Kubernetes and Skaffold


I figured out a way to handle this. I'm not going to set it as the correct answer until someone else confirms if this is a good approach.

Basically, what I did was create a Persistent Volume Claim. Inside the server-deployment I hook up the migrations/ folder to that Persistent Volume. That way, whenever the pod gets deleted, the migrations/ folder remains and gets persisted across pod restarts.

It would look something like this inside the server deployment.

      containers:      ..........        volumeMounts:          - name: migrationstuff            mountPath: 'MyServerApplicationDirectory/migrations'      volumes:        - name: migrationstuff          persistentVolumeClaim:            claimName: migrate-pvc

The PVC would look like this:

apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: migrate-pvcspec:  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 1Gi

For those who decide to take this approach with flask migrate, there's a tricky "chicken or the egg" issue. When you run flask db init, it creates the migrations/ folder with some stuff in it. However, if there's a PVC creating the empty migrations/ folder, flask migrate already thinks the folder exists. You can't delete the folder either with rmdir because it's got a working process on it. However, you need to get the contents of the flask migrate init command into the empty migrations/ folder.....

The trick I found was:

python flask db init --directory migrationmv migration/* migrations/

This intialized all the files you need into a new "migration" folder. You then copy it all into the migrations/ folder to persist from then on. Flask migrate automatically looks for that folder if you leave out the --directory flag.

Then delete the migration folder rmdir migration(or just wait until your pod restarts in which case it'll disappear anyways).

You now have a proper migrations/ folder with everything in it. When you close your flask pod and restarted, the PVC injects that filled up migrations/ folder back into the pod. I can now upgrade/downgrade. Just have to be careful not to delete the pvc!