DEV Community

Cover image for Managing database migrations in ArgoCD ๐Ÿ™
Adrien Mornet
Adrien Mornet

Posted on

Managing database migrations in ArgoCD ๐Ÿ™

ArgoCD is a fantastic tool for doing GitOps in Kubernetes. Most of modern backend frameworks comes with a system that allows to version control database schema definition which is named migrations (e.g. Laravel, Django โ€ฆ). This system is very powerful because it allows the code for a new feature to be versioned together with the database schema required for the feature to function correctly. This can be used, for example, to create a table, add a column, etc. But how to handle these migrations in ArgoCD ? Thatโ€™s what weโ€™ll be looking at in this article.

What a roll-out looks like

In the context of a Kubernetes cluster whose deployments are automated by ArgoCD, Argo will be responsible for synchronising the desired state of the Kubernetes cluster described in the Kubernetes manifest with the actual state of the cluster. The desired state is stored on Git and contains the exact version of the Docker image (via its SHA or tag) which contains your application. When youโ€™ll want to deploy a new version of the Docker image, you will push to the Manifest the new image version. The flow of the roll-out will look like this:

Image description

ArgoCD Resource Hooks

ArgoCD comes with a hook mechanism that enables actions to be taken during synchronisation, i.e. when it detects a difference between the state described in Git stored Kubernetes manifest and the actual state of the cluster. These actions are called Resource Hooks and take the form of Kubernetes jobs.

As we want our migrations to be executed just before the new version of the code is deployed, we will use the PreSync hook to run a job just before the synchronisation operation. Our new deployment flow will look like this:

Image description

The implementation

Here is an example of a Kubernetes Job that subscribe to ArgoCD PreSync hook that will run Laravel migrations:

---
apiVersion: batch/v1
kind: Job
metadata:
  generateName: my-app-migration-
  annotations:
    argocd.argoproj.io/hook: "PreSync"
    argocd.argoproj.io/hook-delete-policy: "BeforeHookCreation"
    argocd.argoproj.io/job-cleanup: "keep"
spec:
  template:
    spec:
      containers:
      - name: my-laravel-app
        image: my-laravel-docker-image:new-tag
        command:
          - "php"
          - "artisan"
          - "migrate"
      restartPolicy: Never
  backoffLimit: 0
Enter fullscreen mode Exit fullscreen mode

This Kubernetes job will be launched when ArgoCD detect a change in the Manifest. ArgoCD will wait the completion of the job (exit code 0) before syncing the rest of your application.

Conclusion

In summary, ArgoCDโ€™s Resource Hooks, especially the PreSync hook, streamline the execution of database migrations before deploying new code versions, ensuring seamless synchronization between application code and database schema. This approach enhances version control and consistency, offering a good solution for Kubernetes roll-out.

If you liked this post, you can find more on my blog https://adrien-mornet.tech/ ๐Ÿš€

Top comments (1)

Collapse
 
sourovpal profile image
Sourov Pal

Hi,
This is Sourov Pal. I am a freelance web developer and Software Developer. I can do one of project for free. If you like my work you will pay me otherwise you don't need to pay. No upfront needed, no contract needed. If you want to outsource your work to me you may knock me.

My what's app no is: +8801919852044
Github Profile: github.com/sourovpal
Thanks