Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Sync Strategies

Manual vs automated

  • manual is the default (no syncPolicy.automated), changes in git show as OutOfSync
  • automated syncs when git changes are detected
    • doesn’t retry the same failed commit+params again (use retry)
    • rollback isn’t possible with auto-sync on
syncPolicy:
  automated:
    prune: true # delete resources removed from git
    selfHeal: true # revert manual cluster changes
    allowEmpty: false # don't let a sync delete everything
argocd app set myapp --sync-policy automated --auto-prune --self-heal
argocd app set myapp --sync-policy none
argocd app sync myapp
argocd app sync myapp --revision v2.0.0
argocd app sync myapp --resource apps:Deployment:myapp   # selective sync
argocd app sync myapp --prune
argocd app sync myapp --dry-run
argocd app sync myapp --force                            # replace/recreate
argocd app sync myapp --apply-out-of-sync-only
argocd app sync myapp --server-side
argocd app sync myapp --wait --timeout 300

Prune

  • off by default, even with auto-sync (must be set explicitly)
  • without it, resources deleted from git stay in the cluster and the app is OutOfSync
  • per resource opt-out: argocd.argoproj.io/sync-options: Prune=false

Self-heal

  • reverts manual changes (kubectl scale ...) back to git
  • default timeout before heal is 5s (controller.self.heal.timeout.seconds)
  • drift is always detected (OutOfSync), self-heal only decides if it is fixed
  • default reconciliation timeout is 180s (timeout.reconciliation)

Sync options

  • set on the app under syncPolicy.syncOptions
  • can also be set per resource with the annotation argocd.argoproj.io/sync-options: Validate=false
    • works for Validate, Prune, PruneLast, Replace, ServerSideApply, Force, SkipDryRunOnMissingResource
syncPolicy:
  syncOptions:
    - Validate=true # kubectl schema validation
    - CreateNamespace=true
    - PrunePropagationPolicy=foreground # foreground | background | orphan
    - PruneLast=true # prune after everything else is healthy
    - ApplyOutOfSyncOnly=true # only touch resources that changed
    - Replace=false # kubectl replace instead of apply
    - ServerSideApply=true
    - RespectIgnoreDifferences=true
    - FailOnSharedResource=true
  • CreateNamespace=true: namespace is created first but is not deleted when the app is deleted
  • PrunePropagationPolicy
    • foreground - dependents first, then owner (default)
    • background - owner first, dependents async
    • orphan - dependents left behind
  • ServerSideApply=true is useful for CRDs and very large resources (fixes the annotation size limit)
  • Replace=true can be destructive, resources get recreated

Retry

syncPolicy:
  retry:
    limit: 5
    backoff:
      duration: 5s
      factor: 2
      maxDuration: 3m
  • waits 5s, 10s, 20s, 40s, 80s, capped at maxDuration

Phases, hooks, waves

  • order within a sync: PreSync -> Sync -> PostSync
  • SyncFail runs only if the sync failed
  • PostDelete runs after the app and its resources are deleted
  • each phase needs the previous to succeed, except SyncFail

Hooks

  • ordinary manifests (usually Job/Pod) with an annotation argocd.argoproj.io/hook: PreSync
metadata:
  annotations:
    argocd.argoproj.io/hook: PreSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
  • hook types: PreSync, Sync, PostSync, SyncFail, PostDelete, Skip
    • Skip tells Argo not to apply the resource at all
  • delete policies
    • HookSucceeded - delete after success
    • HookFailed - delete after failure
    • BeforeHookCreation - delete the old one before creating a new one (default)
  • use cases
    • PreSync: db migration, backup
    • PostSync: smoke tests, notifications
    • SyncFail: alert, cleanup
apiVersion: batch/v1
kind: Job
metadata:
  name: database-migration
  annotations:
    argocd.argoproj.io/hook: PreSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  backoffLimit: 3
  template:
    spec:
      containers:
        - name: migration
          image: migrate/migrate:latest
          command:
            [
              'migrate',
              '-path=/migrations',
              '-database=postgres://db:5432/myapp',
              'up',
            ]
      restartPolicy: Never
  • an app with a hook Job that never finishes will hang in Progressing

Waves

  • order resources inside a phase

Prune

  • argocd.argoproj.io/sync-wave: "<n>", lower first, default 0, negatives allowed

Prune

  • Argo applies a wave, waits for it to be healthy, then moves on (small delay between waves, ARGOCD_SYNC_WAVE_DELAY, default 2s)
  • phase first, then wave, then kind (namespaces first, … , by default) within a wave
  • hooks and waves combine: a PreSync hook with wave -1 runs before a PreSync hook with wave 0
  • pruning happens in reverse wave order
  • needs good health checks to be useful
# wave 0: namespace + configmap, wave 1: database, wave 2: app, wave 3: ingress
metadata:
  annotations:
    argocd.argoproj.io/sync-wave: '1'

Rolling out db -> app example

# db backup  (PreSync, wave -1)
# db migrate (PreSync, wave 0)
# app deploy (Sync, wave 1)
# smoke test (PostSync, wave 0)
# slack notify (SyncFail)

Debugging syncs

argocd app get myapp
argocd app history myapp
argocd app terminate-op myapp
argocd app rollback myapp 3