GitOps
About this lesson
GitOps runs a system from a Git repository. The repository holds the desired state as declarative files, and an agent inside the cluster, such as Argo CD or Flux, keeps pulling those files and making the live system match. Follow one Kubernetes Deployment as a commit scales it from 2 to 3 pods, a hand edit with kubectl drifts it to 1, the agent heals it from Git, and git revert rolls it back to 2.
Git holds the truth
GitOps runs a system from a Git repository. The repository holds the desired state: files saying what should run. An agent inside the cluster keeps the live system matching those files, so people change the system by changing the files.
Git repository = desired state (files) cluster = live state (what runs) GitOps agent = keeps live == desired to change the system: change Git
Read the lesson step by step
Git holds the truth
GitOps runs a system from a Git repository. The repository holds the desired state: files saying what should run. An agent inside the cluster keeps the live system matching those files, so people change the system by changing the files.
Git repository = desired state (files) cluster = live state (what runs) GitOps agent = keeps live == desired to change the system: change GitDeclare, do not instruct
web.yaml is a Kubernetes Deployment. It is declarative: it says what we want, not the steps to get there. Line 5 asks for 2 replicas, two copies of the web server, each in its own pod (the smallest unit Kubernetes runs).
# web.yaml apiVersion: apps/v1 kind: Deployment metadata: {name: web} spec: replicas: 2 selector: {matchLabels: {app: web}} template: metadata: {labels: {app: web}} spec: containers: - {name: web, image: "nginx:1.27"} declarative: "2 copies should run" not imperative: "start one, start another"Change it with a commit
Traffic grows, so a developer changes line 5 to replicas: 3 and opens a pull request. A teammate reviews and merges it. Git records a new commit; every earlier version stays in the history, untouched.
- replicas: 2 + replicas: 3 pull request → review → merge history: #1 replicas 2 #2 replicas 3 (new) the cluster has not changed yetThe agent pulls
The GitOps agent, such as Argo CD or Flux, runs inside the cluster and pulls the repository on a timer; Argo CD checks every 3 minutes by default. Because the cluster pulls, the build system never needs keys to the cluster.
agent (in the cluster) → git fetch Argo CD: polls every 3 minutes by default Flux: polls on the interval you set (a webhook can trigger it sooner) pull, not push: no cluster keys in CICompare desired with live
The agent compares the two states. Git wants 3 replicas; the cluster runs 2. They differ, so the app is marked OutOfSync. Compare, then act: this loop is how every Kubernetes controller works.
def reconcile(desired, live): if live < desired: return f"start {desired - live} pod(s)" if live > desired: return f"stop {live - desired} pod(s)" return "in sync" >>> reconcile(3, 2) 'start 1 pod(s)' status: OutOfSyncReconcile: make it so
The agent applies web.yaml to the cluster. Kubernetes' Deployment controller sees 3 pods wanted but 2 running, and starts a third. Live now matches Git, 3 = 3, so the app is Synced.
agent: apply web.yaml Deployment controller: want 3, have 2 → start 1 pod WANT 3 LIVE 3 SYNC = status: SyncedDrift: a change by hand
Late one night someone types kubectl scale deployment web --replicas=1. Two pods stop. The cluster has drifted: it no longer matches Git, and no review or commit records who did it or why.
$ kubectl scale deployment web --replicas=1 deployment.apps/web scaled WANT 3 LIVE 1 SYNC ≠ Git says 3; the cluster runs 1 no commit, no review, no historySelf-heal from Git
The agent keeps watching, spots the drift and applies the Git version again: back to 3 pods. Argo CD does this when self-heal is switched on; Flux re-applies on every interval. Git wins over hand edits.
>>> reconcile(3, 1) 'start 2 pod(s)' agent: apply web.yaml again WANT 3 LIVE 3 SYNC = Argo CD: syncPolicy.automated.selfHeal: trueRoll back with git revert
Traffic falls, so the team runs git revert on commit #2. That adds commit #3, which undoes it. The agent pulls it and scales back to 2 pods. A rollback is just another reviewed commit.
$ git revert <commit #2> history: #1 replicas 2 #2 replicas 3 #3 replicas 2 (revert of #2) >>> reconcile(2, 3) 'stop 1 pod(s)'The four principles
OpenGitOps names four principles: the desired state is declarative, versioned and immutable, pulled automatically and continuously reconciled. In the cloud: Flux on Azure AKS, Config Sync on Google GKE, Argo CD or Flux on AWS EKS.
OpenGitOps principles v1.0.0 1 Declarative the desired state is described, not scripted 2 Versioned and immutable kept in Git with a complete history 3 Pulled automatically agents pull the desired state from Git 4 Continuously reconciled agents keep making live match desired Azure AKS: GitOps with Flux v2 Google GKE: Config Sync AWS EKS: Argo CD or Flux
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In GitOps, where does the desired state of the system live?
Git is the single source of truth: files such as web.yaml say what should run, and every change to them is a reviewed, recorded commit.
02Git says replicas: 3 but the cluster runs 2 pods. What does the GitOps agent do?
The agent compares desired with live and changes the live system to match Git, never the other way round. That loop is called reconciliation.
03Why is the pull model (the agent pulls from Git) safer than a CI server pushing to the cluster?
The agent runs inside the cluster and reaches out to Git, so no outside system has to hold keys that can change the cluster.
04Someone runs kubectl scale deployment web --replicas=1 by hand. With self-heal on, what happens next?
The hand edit is drift: live no longer matches Git. A self-healing agent re-applies the Git version. In Argo CD self-heal is opt-in; Flux re-applies on every interval.
05Which list gives the four OpenGitOps principles?
OpenGitOps v1.0.0 names these four. Plan-code-build-test is the DevOps loop, and latency-traffic-errors-saturation are the SRE golden signals.
Glossary
- GitOps
- Running a system from a Git repository: the desired state is stored as declarative files in Git, and software agents keep the live system matching them.
- Desired state
- What should be running, written down in files (for example: 3 copies of nginx:1.27).
- Live state
- What is really running in the cluster right now.
- Declarative
- Describing the result you want rather than the steps to get there. A Kubernetes Deployment is declarative.
- Reconciliation
- The loop that compares desired state with live state and changes the live system until they match.
- Drift
- When the live state no longer matches the desired state in Git, often because of a change made by hand.
- GitOps agent
- A program in the cluster, such as Argo CD or Flux, that pulls the desired state from Git and reconciles the cluster to it.
- Pod
- The smallest unit Kubernetes runs: one or more containers that are scheduled together.
Notes and sources
- The four principles are from OpenGitOps v1.0.0, written by the CNCF GitOps Working Group. GitOps is not tied to Kubernetes, but Kubernetes is where it is used most, because Kubernetes objects are already declarative.
- Argo CD polls Git every three minutes by default and can be told sooner with a webhook. In Flux the polling interval is set per source (spec.interval). Real agents also watch the live cluster continuously, so drift is noticed within seconds.
- In Argo CD, automatic sync and self-heal are opt-in settings (syncPolicy.automated with selfHeal: true). Flux re-applies the manifests on every reconciliation interval, which also corrects drift.
- Two control loops are at work: the GitOps agent makes the Deployment object match Git, and Kubernetes’ own Deployment controller makes the running pods match the Deployment. The figure shows them as one step.
- web.yaml is a complete, valid Deployment. It uses YAML flow style ({key: value}) for short lines; the block style with one key per line means exactly the same. The reconcile function in the side panel is real Python 3.
- Never commit real secrets (passwords, tokens) to the GitOps repository in plain text. Teams store them encrypted (for example with Sealed Secrets or SOPS) or reference an external secret store.
- OpenGitOps: GitOps Principles v1.0.0
- Kubernetes docs: Controllers
- Kubernetes docs: Deployments
- Kubernetes docs: Pods
- Kubernetes docs: Declarative management of Kubernetes objects using configuration files
- Kubernetes docs: kubectl scale
- Argo CD docs: Git webhook configuration (polls every three minutes)
- Argo CD docs: Automated sync policy and self-healing
- Flux docs: Kustomization
- Git docs: git-revert
- Microsoft Learn: GitOps Flux v2 for AKS and Azure Arc-enabled Kubernetes
- Google Cloud: Config Sync overview
Quiz Unlocks after the last step.