Q: Deploying a new microservice version requires running a database schema migration before new application pods boot. If you deploy pods and run migrations simultaneously, new pods crash due to missing database columns. If the migration fails, application pods must NOT be updated. How do you design and execute this orchestration in Argo CD using Sync Waves and Resource Hooks?
Engineering a fault-tolerant GitOps deployment lifecycle in Argo CD using Sync Waves, PreSync database migration jobs, and PostSync notification hooks with automated rollback on migration failures.
Want to master this scenario in a live sandbox? KodeKloud's Enterprise GitOps with ArgoCD & Kubernetes Rollouts covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Configure Database Migration as an Argo CD PreSync Job Hook
Execute database schema updates before applying application workloads:
- PreSync Hook Annotation: Annotated Kubernetes migration Job with
argocd.argoproj.io/hook: PreSync. - Hook Deletion Policy: Configured
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation, HookSucceededto automatically clean up completed migration jobs. - Migration Execution: Job runs Flyway / Liquibase database schema migration against production PostgreSQL.
Sequence Infrastructure & Workload Manifests via Sync Waves
Order resource application deterministically across execution phases:
- Wave 0 (Foundations): Namespaces, ServiceAccounts, RBAC roles:
argocd.argoproj.io/sync-wave: '0'. - Wave 1 (Configuration & PreSync): ConfigMaps, ExternalSecrets, and DB Migration PreSync Job:
sync-wave: '1'. - Wave 2 (Workloads): Deployments and StatefulSets:
sync-wave: '2'. - Wave 3 (Ingress & Routing): Ingress and Istio VirtualServices:
sync-wave: '3'.
Enforce Automated Sync Abort on PreSync Migration Failure
Guarantee application pods remain untouched if schema migrations fail:
- Sync Failure Behavior: If the PreSync migration Job fails (exit code 1), Argo CD halts the synchronization immediately.
- Workloads Untouched: Wave 2 (Deployments) is NEVER executed; existing running application pods continue serving traffic against the unmodified database.
- SyncFail Hook: Triggered
SyncFailnotification hook alerting the on-call engineer in Slack with the failed migration container logs.
Deploy PostSync Verification & Slack Notification Hooks
Verify end-to-end service availability after successful sync:
- PostSync Hook: Configured lightweight test job annotated with
argocd.argoproj.io/hook: PostSyncrunning smoke tests against/healthz. - Deployment Success: Posts deployment success confirmation to Slack with git commit details and author.
- Reliability Posture: 100% elimination of deployment outages caused by schema migration race conditions.
- Annotate database migration Jobs with argocd.argoproj.io/hook: PreSync.
- Order resources into sequential phases using sync waves: configs (Wave 1) -> pods (Wave 2) -> ingress (Wave 3).
- Automatically abort the rollout if PreSync migration jobs fail, leaving running pods safe.
- Run automated smoke tests in PostSync hooks to verify production health before marking success.