Tag 3: Sync-Policies und Drift
Ziel: Argo CD synchronisiert selbst, holt Änderungen von Hand zurück und räumt Objekte ab, die aus Git verschwinden. · Dauer: ca. 40 min · Voraussetzung: Tag 2
Briefing
automatedsynchronisiert jedes Mal, wenn sich Git ändert. Ändert jemand den Cluster, passiert damit noch nichts.selfHeal: trueschaltet das Zurückholen ein: Weicht der Cluster von Git ab, setzt Argo CD den Stand aus Git wieder durch.prune: truelöscht Objekte, deren Datei aus Git verschwunden ist. Ohneprunebleiben sie stehen, und die Application zeigtOutOfSync.- Als Abweichung zählt nur, was in Git steht. Felder, die Git gar nicht nennt, darf jeder setzen.
| Einstellung | Git ändert sich | Cluster ändert sich | Datei gelöscht |
|---|---|---|---|
| Manual | OutOfSync |
OutOfSync |
OutOfSync |
automated |
sync | OutOfSync |
bleibt stehen |
+ selfHeal |
sync | zurückgeholt | bleibt stehen |
+ prune |
sync | zurückgeholt | gelöscht |
Drill 1: Automatisch synchronisieren
Die Application aus Tag 2 bekommt eine Sync-Policy. Du schreibst sie als neue Datei:
mkdir -p ~/argo-lab/tag03
cd ~/argo-lab/tag03
cat > notes-app.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: notes
namespace: argocd
spec:
project: default
source:
repoURL: [email protected]:<du>/argocd-fleet.git
targetRevision: main
path: apps/notes
destination:
server: https://kubernetes.default.svc
namespace: notes
syncPolicy:
automated: {}
syncOptions:
- CreateNamespace=true
EOF
kubectl apply -f notes-app.yaml
argocd app get notes | grep -E "Sync Policy|Sync Status"Erwartet:
application.argoproj.io/notes configured
Sync Policy: Automated
Sync Status: Synced to main (f93579f)
Ab jetzt reicht ein git push. Spätestens nach dem
nächsten Abgleich zieht Argo CD nach.
Fortschritt speichern? Kostenlos registrieren
Drill 2: Drift ohne Self-Heal
Ein Kollege skaliert von Hand:
kubectl -n notes scale deploy notes --replicas=5Warte drei Minuten, also länger als ein Abgleich dauert:
sleep 180
kubectl -n notes get deploy notes
argocd app get notes | grep "Sync Status"
argocd app diff notesErwartet:
NAME READY UP-TO-DATE AVAILABLE AGE
notes 5/5 5 5 5m51s
Sync Status: OutOfSync from main (f93579f)
===== apps/Deployment notes/notes ======
126c126
< replicas: 5
---
> replicas: 2
Argo CD sieht die Abweichung und meldet sie, holt sie aber nicht
zurück. automated allein reagiert nur auf neue Commits.
Fortschritt speichern? Kostenlos registrieren
Drill 3: Self-Heal
cat > notes-app.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: notes
namespace: argocd
spec:
project: default
source:
repoURL: [email protected]:<du>/argocd-fleet.git
targetRevision: main
path: apps/notes
destination:
server: https://kubernetes.default.svc
namespace: notes
syncPolicy:
automated:
selfHeal: true
syncOptions:
- CreateNamespace=true
EOF
kubectl apply -f notes-app.yaml
sleep 15
kubectl -n notes get deploy notesErwartet:
application.argoproj.io/notes configured
NAME READY UP-TO-DATE AVAILABLE AGE
notes 2/2 2 2 6m18s
Kaum ist Self-Heal an, stehen wieder zwei Pods da. Jetzt der nächste Versuch, diesmal mit dem Image:
kubectl -n notes set image deploy/notes api=ghcr.io/thomaszachmann/notes-api:0.7.1
sleep 10
kubectl -n notes get deploy notes \
-o jsonpath='{.spec.template.spec.containers[0].image}'Erwartet:
deployment.apps/notes image updated
ghcr.io/thomaszachmann/notes-api:0.5.3
Was passiert ist, steht in den Events der Application:
kubectl -n argocd get events --field-selector involvedObject.name=notes \
--sort-by=.lastTimestampErwartet (gekürzt):
… application/notes Updated health status: Healthy -> Progressing
… application/notes Partial sync operation to f93579fe66cb… succeeded
… application/notes Updated sync status: OutOfSync -> Synced
… application/notes Updated health status: Progressing -> Healthy
Partial sync: Argo CD hat nur das abweichende Deployment
angefasst, nicht die ganze Application.
Fortschritt speichern? Kostenlos registrieren
Drill 4: Was nicht als Drift zählt
kubectl -n notes label deploy notes team=plattform
kubectl -n notes annotate deploy notes owner=thomas
sleep 15
argocd app get notes --refresh | grep "Sync Status"
kubectl -n notes get deploy notes --show-labelsErwartet:
deployment.apps/notes labeled
deployment.apps/notes annotated
Sync Status: Synced to main (137f99e)
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
notes 2/2 2 2 8m11s team=plattform
Label und Annotation bleiben, die Application ist
Synced. Argo CD vergleicht nur die Felder, die in Git
stehen. Was andere Programme ergänzen, etwa ein Autoscaler oder ein
Service-Mesh, stört deshalb nicht. Wer Felder aus Git bewusst anderen
überlassen will, nimmt ignoreDifferences (Tag 6).
Räum die beiden wieder ab:
kubectl -n notes label deploy notes team-
kubectl -n notes annotate deploy notes owner-Fortschritt speichern? Kostenlos registrieren
Drill 5: Prune
Eine neue Datei kommt ins Repo und verschwindet wieder:
cd ~/argo-lab/argocd-fleet
cat > apps/notes/info.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
name: notes-info
data:
team: plattform
EOF
git add .
git commit -m "notes-info"
git push
argocd app get notes --refresh
kubectl -n notes get cm notes-infoErwartet (gekürzt):
NAME DATA AGE
notes-info 1 9s
git rm apps/notes/info.yaml
git commit -m "notes-info entfernt"
git push
argocd app get notes --refresh
kubectl -n notes get cm notes-info
argocd app get notes | grep "Sync Status"Erwartet (gekürzt):
NAME DATA AGE
notes-info 1 25s
Sync Status: OutOfSync from main (137f99e)
Die ConfigMap lebt weiter, obwohl ihre Datei weg ist. Argo CD löscht
ohne Erlaubnis nichts. Einmal von Hand geht es mit
--prune:
argocd app sync notes --pruneErwartet (gekürzt):
…
Message: successfully synced (all tasks run)
…
ConfigMap notes notes-info Succeeded Pruned pruned
Dauerhaft erlaubst du es in der Application:
cd ~/argo-lab/tag03
cat > notes-app.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: notes
namespace: argocd
spec:
project: default
source:
repoURL: [email protected]:<du>/argocd-fleet.git
targetRevision: main
path: apps/notes
destination:
server: https://kubernetes.default.svc
namespace: notes
syncPolicy:
automated:
selfHeal: true
prune: true
syncOptions:
- CreateNamespace=true
EOF
kubectl apply -f notes-app.yaml
argocd app get notes | grep "Sync Policy"Erwartet:
application.argoproj.io/notes configured
Sync Policy: Automated (Prune)
Fortschritt speichern? Kostenlos registrieren
Drill 6: Die Application löschen
Was passiert mit notes-api, wenn die Application verschwindet?
kubectl delete application notes -n argocd
kubectl -n notes get deploy,svcErwartet:
application.argoproj.io "notes" deleted from argocd namespace
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/notes 2/2 2 2 7m34s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/notes NodePort 10.43.21.74 <none> 80:30080/TCP 7m34s
Alles läuft weiter. Ohne Finalizer löscht kubectl delete
nur die Application, nicht die Objekte. Legst du sie neu an, übernimmt
Argo CD die Objekte wieder:
kubectl apply -f notes-app.yaml
sleep 10
argocd app get notes | grep -E "Sync Status|Health Status"Erwartet:
application.argoproj.io/notes created
Sync Status: Synced to main (137f99e)
Health Status: Healthy
Soll das Löschen der Application auch die Objekte löschen, bekommt
sie den Finalizer resources-finalizer.argocd.argoproj.io.
Das kommt an Tag 7. argocd app delete löscht dagegen
standardmäßig mit.
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
argocd app get notes | grep "Sync Policy"Erwartet: Sync Policy: Automated (Prune).
kubectl get application notes -n argocd -o jsonpath='{.spec.syncPolicy.automated}'Erwartet: {"prune":true,"selfHeal":true}.
kubectl -n notes get cm notes-infoErwartet:
Error from server (NotFound): configmaps "notes-info" not found.
Challenge
Ein Kollege stellt den NodePort von notes-api auf 30081
um. Was passiert, und warum anders als beim Label aus Drill 4?
Hinweis:
kubectl -n notes patch svc notes --type=json und die Frage,
ob der NodePort in Git steht.
Lösung
kubectl -n notes patch svc notes --type=json \
-p '[{"op":"replace","path":"/spec/ports/0/nodePort","value":30081}]'
sleep 10
kubectl -n notes get svc notesErwartet:
service/notes patched
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
notes NodePort 10.43.21.74 <none> 80:30080/TCP 8m29s
Der NodePort steht in service.yaml, also ist die
Änderung Drift, und Self-Heal holt 30080 zurück. Das Label
aus Drill 4 stand in keiner Datei und blieb deshalb.
Fortschritt speichern? Kostenlos registrieren
Enterprise-Notiz
pruneundselfHealgehören zu GitOps dazu. Ohne sie ist Git nur ein Vorschlag. Geradepruneschalten Teams trotzdem oft erst ein, wenn sie dem Repo vertrauen.- Im Notfall darf ein Mensch kurz am Cluster drehen. Dann schaltet er
vorher
automatedab, sonst dreht Argo CD die Hilfe sofort zurück. - Einzelne Objekte schützt die Annotation
argocd.argoproj.io/sync-options: Prune=falsevor dem Löschen, etwa ein Volume mit Daten.