Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 4.1GitOps, Chapter 4

Deploy an app with Argo CD: auto-sync, self-heal, prune and a rollback

Level
Medium
50 min
Machines
a Kubernetes cluster (kind or minikube is fine), a GitHub account, kubectl, the argocd CLI
Status
Not started

Goal

Put an app into Git, let Argo CD deploy it, and see the GitOps promises with your own eyes: a change in Git is deployed by itself, a manual change in the cluster is undone (self-heal), a file you delete in Git is removed from the cluster (prune), and a rollback is just git revert.

Before you start

  • Argo CD installed (chapter Install and log in to Argo CD): kubectl -n argocd get pods all Running, and argocd login works.
  • Make a public GitHub repository gitops-lab with the folder apps/guestbook/ that has a Deployment (nginx:1.26, 2 replicas) and a Service.

Tasks

  1. Write an AppProject lab that allows only your repository and only the namespace guestbook.
  2. Write an Application guestbook in the project lab: source apps/guestbook, destination namespace guestbook, CreateNamespace=true, automated sync with prune and selfHeal.
  3. Apply both with kubectl (Argo CD manages them as normal objects) and wait until the app is Synced and Healthy.
  4. In Git, change the image to nginx:1.27 and push. Watch Argo CD deploy it without any command.
  5. Scale the deployment by hand to 5 replicas with kubectl. Watch Argo CD set it back to 2.
  6. Add a ConfigMap in Git with a sync-wave of -1 (so it is made before the Deployment), push, then delete it in Git and see it pruned.
  7. Roll back the image change with git revert and push.

Check your work

argocd app get guestbook
kubectl -n guestbook get deploy -o wide
argocd app history guestbook
  • Sync Status: Synced, Health Status: Healthy.
  • After task 5 the deployment shows 2/2 again within about a minute.
  • The history shows a sync for every Git commit; the last one is the revert (image nginx:1.26).

Hints

Show the hints
  • syncPolicy: automated: {prune: true, selfHeal: true} and syncOptions: [CreateNamespace=true].
  • Argo CD checks Git every 3 minutes by default; argocd app sync guestbook or a webhook makes it faster.
  • A sync wave is an annotation: argocd.argoproj.io/sync-wave: "-1".
  • Do not use argocd app rollback with auto-sync on: Git is the truth, so roll back in Git.

Solution

Show the solution
  1. Project and application.
    lab-project.yaml
    apiVersion: argoproj.io/v1alpha1
    kind: AppProject
    metadata:
      name: lab
      namespace: argocd
    spec:
      sourceRepos: ["https://github.com/OWNER/gitops-lab.git"]
      destinations:
        - server: https://kubernetes.default.svc
          namespace: guestbook
      clusterResourceWhitelist:
        - group: ""
          kind: Namespace
    guestbook-app.yaml
    apiVersion: argoproj.io/v1alpha1
    kind: Application
    metadata:
      name: guestbook
      namespace: argocd
    spec:
      project: lab
      source:
        repoURL: https://github.com/OWNER/gitops-lab.git
        targetRevision: main
        path: apps/guestbook
      destination:
        server: https://kubernetes.default.svc
        namespace: guestbook
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions: [CreateNamespace=true]
    kubectl apply -f lab-project.yaml -f guestbook-app.yaml
    argocd app wait guestbook --health --sync --timeout 300
  2. A change in Git.
    sed -i 's/nginx:1.26/nginx:1.27/' apps/guestbook/deployment.yaml
    git commit -am "guestbook: nginx 1.27" && git push
    kubectl -n guestbook rollout status deploy/guestbook
  3. Self-heal.
    kubectl -n guestbook scale deploy guestbook --replicas 5
    kubectl -n guestbook get deploy guestbook -w
  4. Wave and prune.
    apps/guestbook/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: guestbook-config
      annotations:
        argocd.argoproj.io/sync-wave: "-1"
    data:
      GREETING: hello
    git add apps/guestbook/configmap.yaml && git commit -m "Add config" && git push
    git rm apps/guestbook/configmap.yaml && git commit -m "Remove config" && git push
    kubectl -n guestbook get cm guestbook-config
    Error from server (NotFound): configmaps "guestbook-config" not found
  5. Roll back in Git.
    git revert --no-edit $(git log --format=%h -S "nginx:1.27" -1)
    git push
    argocd app history guestbook

My notes

No notes yet.

Lessons for this lab

Commands in this lab