Synchronization strategies
Table of Contents
1 - Manual vs automatic sync
Manual sync
spec:
syncPolicy: {} # Pas de politique = manuel
# Synchroniser manuellement
argocd app sync my-app
Automatic sync
spec:
syncPolicy:
automated: {}
Comparison
| Aspect | Manual | Automatic |
|---|---|---|
| Control | Total | Delegated to Argo CD |
| Speed | Depends on the human | Instant |
| Risk | Review possible | Immediate deployment |
| Use case | Critical production | Dev, staging |
🔝 Back to table of contents
2 - Auto-sync
Enabling
spec:
syncPolicy:
automated:
prune: false # Ne pas supprimer les ressources orphelines
selfHeal: false # Ne pas corriger le drift
Behavior
- Argo CD detects a change in Git (polling every 3 min)
- Compares with the cluster state
- If OutOfSync → synchronizes automatically
Polling configuration
# argocd-cm ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
namespace: argocd
data:
timeout.reconciliation: 180s # 3 minutes par défaut
Webhook for instant sync
# Configurer un webhook GitHub/GitLab
# POST https://argocd.example.com/api/webhook
# Argo CD synchronise immédiatement au lieu d'attendre le polling
🔝 Back to table of contents
3 - Self-heal
Definition
Self-heal automatically corrects drift (manual modification of the cluster).
spec:
syncPolicy:
automated:
selfHeal: true
Drift example
# Git dit : replicas=3
# Quelqu'un fait :
kubectl scale deployment my-app --replicas=5
# Avec selfHeal=true :
# Argo CD détecte le drift et remet à 3 replicas
# Avec selfHeal=false :
# L'application reste OutOfSync jusqu'au prochain sync manuel
Workflow
When to use
| Scenario | selfHeal |
|---|---|
| Stable production | true |
| Debugging in progress | false |
| Manual tests | false |
| Strict compliance | true |
🔝 Back to table of contents
4 - Prune
Definition
Prune deletes cluster resources that are no longer in Git.
spec:
syncPolicy:
automated:
prune: true
Example
# Git contient :
# - deployment.yaml
# - service.yaml
# Vous supprimez service.yaml de Git
# Avec prune=true :
# Le Service est supprimé du cluster
# Avec prune=false :
# Le Service reste dans le cluster (orphelin)
Protection against deletion
metadata:
annotations:
# Cette ressource ne sera jamais prunée
argocd.argoproj.io/sync-options: Prune=false
PruneLast
spec:
syncPolicy:
syncOptions:
- PruneLast=true # Prune après le sync des autres ressources
🔝 Back to table of contents
5 - Sync Options
Available options
spec:
syncPolicy:
syncOptions:
- CreateNamespace=true # Créer le namespace si absent
- PruneLast=true # Prune à la fin
- ApplyOutOfSyncOnly=true # Appliquer seulement les ressources OutOfSync
- PrunePropagationPolicy=foreground # Politique de suppression
- Replace=true # Utiliser replace au lieu d'apply
- ServerSideApply=true # Server-side apply (K8s 1.22+)
CreateNamespace
spec:
destination:
namespace: new-namespace # Sera créé automatiquement
syncPolicy:
syncOptions:
- CreateNamespace=true
Retry
spec:
syncPolicy:
automated:
prune: true
selfHeal: true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
Sync Waves
# Déployer dans un ordre spécifique
metadata:
annotations:
argocd.argoproj.io/sync-wave: "1" # Déployé en premier
# Ordre : -1 → 0 → 1 → 2 → ...
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Test Self-heal
# 1. Créer une application avec self-heal
argocd app create test-heal \
--repo https://github.com/argoproj/argocd-example-apps.git \
--path guestbook \
--dest-server https://kubernetes.default.svc \
--dest-namespace default \
--sync-policy automated \
--self-heal
# 2. Synchroniser
argocd app sync test-heal
# 3. Modifier manuellement
kubectl scale deployment guestbook-ui --replicas=5
# 4. Observer
argocd app get test-heal
# Après quelques secondes, replicas revient à 1
Exercise 2: Complete configuration
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: production-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/gitops.git
path: apps/my-app
targetRevision: main
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- PruneLast=true
retry:
limit: 3
backoff:
duration: 5s
factor: 2
maxDuration: 1m
Quiz
Q1. What does prune: true do?
Answer
Prune deletes from the cluster resources that are no longer present in Git. If you delete a YAML file from the repo, the corresponding resource will be deleted from the cluster.
Q2. What is the difference between selfHeal and auto-sync?
Answer
- auto-sync: Synchronizes when Git changes
- selfHeal: Synchronizes when the cluster changes (drift)
Both are complementary: auto-sync reacts to Git changes, selfHeal corrects manual cluster modifications.
🔝 Back to table of contents
Key takeaways
- Auto-sync: automatic deployment on Git change
- Self-heal: automatic drift correction
- Prune: deletion of orphaned resources
- Use Sync Waves for deployment order
- CreateNamespace to auto-create namespaces