Kubernetes
Reylo Migrate on Kubernetes
Build an image of your migrations from Reylo's:
FROM ghcr.io/reylosql/reylo:0.8.3
COPY reylo/ /work/reylo/
CMD ["migrate", "deploy"]It reads DATABASE_URL, and exits 1 when a migration fails. Copy .reylo.yaml too, if you have one.
Jobs
Run the image as a Job before your app's pods change. Each deploy tool can run it first:
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Release.Name }}-migrate
annotations:
# before the app changes, on install and on each upgrade
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
spec:
backoffLimit: 0
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: app-migrate:{{ .Values.tag }}
env:
- name: DATABASE_URL
valueFrom:
# a Secret made before the first install
secretKeyRef: { name: app-database, key: url }apiVersion: batch/v1
kind: Job
metadata:
name: app-migrate
annotations:
# before each sync
argocd.argoproj.io/hook: PreSync
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
spec:
backoffLimit: 0
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: app-migrate:1.4.0
env:
- name: DATABASE_URL
valueFrom:
# a Secret made before the first sync
secretKeyRef: { name: app-database, key: url }# the Job, in ./migrate/
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: app-migrate
spec:
sourceRef: { kind: GitRepository, name: app }
path: ./migrate/
interval: 60m
prune: true
# wait for the Job to finish, and run it again when its image changes
wait: true
force: true
---
# the app, once the Job has finished
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: app
spec:
dependsOn:
- name: app-migrate
sourceRef: { kind: GitRepository, name: app }
path: ./deploy/
interval: 60m
prune: trueA migration that fails fails the Job. The app stays on its old version, with the migrations before it applied.
Init containers
Run the image in each pod, before your app starts. On Postgres and MySQL, pods that start at once take turns, and the rest find nothing to run:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
# ...
template:
spec:
initContainers:
- name: migrate
image: app-migrate:1.4.0
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef: { name: app-database, key: url }
containers:
- name: app
# ...A migration that fails keeps the new pods from starting. An older image refuses a database that ran a newer migration, so after a rollback, its pods don't start either.