GitOps with Argo CD
In the pipelines so far, CI pushes each new version into the cluster. GitOps flips that around: a Git repository describes exactly what should run, and an agent inside the cluster keeps the cluster matching it. Deploying becomes a commit, rolling back becomes a revert, and every change has a reviewer and a history.
Push versus pull deploys
In a push deploy, the CI job runs kubectl apply or helm upgrade against the cluster. That works, but CI needs admin credentials for every cluster, and nothing notices if someone later edits the cluster by hand.
In a pull (GitOps) deploy, an agent such as Argo CD or Flux runs inside the cluster, watches a Git repository and applies whatever it says. CI never touches the cluster; it only commits to Git. If the cluster drifts from Git, the agent sees it and can put it back.
Two principles carry the whole idea: Git is the single source of truth for what runs, and the cluster is reconciled continuously, not just at deploy time.
The pieces
- A config repository with the Kubernetes manifests, Helm values or Kustomize overlays for each app and environment. Teams usually keep it separate from the app's code, so a config change doesn't trigger an app build and the history reads like a deploy log.
- Argo CD, installed in the cluster, with a web UI and a CLI.
- An Application: an Argo CD object that says "keep this path of this repo applied to this namespace".
Install Argo CD on your practice cluster
Use the kind cluster from Kubernetes chapter 6:
$ kubectl create namespace argocd
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
$ kubectl -n argocd get pods # wait until every pod is Running
# the first admin password, then the UI on https://localhost:8080 (accept the self-signed certificate)
$ kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo
$ kubectl port-forward svc/argocd-server -n argocd 8080:443
Log in as admin. The argocd CLI works too: argocd login localhost:8080 --username admin --insecure.
Your first Application
Argo CD's own example repository has a small app called guestbook. This manifest tells Argo CD to keep it running in a guestbook namespace:
# guestbook-app.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: guestbook
namespace: argocd # Applications live in Argo CD's namespace
spec:
project: default
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD # a branch, a tag or a commit
path: guestbook # the folder with the manifests
destination:
server: https://kubernetes.default.svc # this same cluster
namespace: guestbook
syncPolicy:
automated:
prune: true # delete things that were removed from Git
selfHeal: true # undo changes made by hand in the cluster
syncOptions:
- CreateNamespace=true
$ kubectl apply -f guestbook-app.yaml
$ kubectl -n guestbook get pods # Argo CD created them, not you
In the UI the app shows two statuses: Synced / OutOfSync (does the cluster match Git?) and Healthy / Progressing / Degraded (are the resources actually working?).
Drift and self-healing
Try changing the cluster behind Argo CD's back:
$ kubectl -n guestbook scale deployment guestbook-ui --replicas=5
$ kubectl -n guestbook get deployment guestbook-ui -w # within moments it goes back to what Git says
With selfHeal on, a hand-made change lasts seconds. That's the point: the only way to change production is a reviewed commit. Without selfHeal, the app just turns OutOfSync and waits for you to sync it.
How a new version gets deployed
- A developer merges a change to the app repository.
- CI tests it, builds the image and pushes
shop-api:3f9c2a1to the registry, exactly as before. - CI (or a bot such as Argo CD Image Updater) commits the new tag into the config repository, often through a pull request for production.
- Argo CD notices the commit (it checks about every 3 minutes by default, or straight away with a webhook) and rolls the Deployment out.
Rolling back is git revert on the config repository: the old tag comes back, and Argo CD applies it. The history shows who deployed what and when, and who rolled it back.
More than one app or environment
- A common layout is one folder per environment in the config repo (
envs/staging,envs/production), each an overlay with its own replicas and image tag, and one Application per folder. - The app of apps pattern is one Application whose folder contains other Application manifests, so a whole cluster is bootstrapped from one object.
- ApplicationSets generate Applications from a template, for example one per cluster or one per folder.
Flux is the other big GitOps tool. Both Argo CD and Flux are graduated CNCF projects; Argo CD is known for its UI, Flux for being a set of small controllers you drive entirely from Git.
Common problems
- Stuck OutOfSync on a field you never set: something else in the cluster (an autoscaler, a mutating webhook) changes it. Tell Argo CD to ignore that field with
ignoreDifferences, or let the HPA ownreplicasby leaving it out of the manifest. - "repository not accessible": private repos need credentials. Add them with
argocd repo addusing a deploy key or token. - Rollback button greyed out: Argo CD won't roll back while automated sync is on, because Git would win again a moment later. Revert in Git instead.
- Secrets in the config repo: never commit plain Kubernetes Secrets. Use Sealed Secrets, SOPS, or the External Secrets Operator to pull them from a secrets manager.