ENع
←GitOps
Lesson 15 · Delivery

GitOps

12 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 15 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Declare, 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"
  3. 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 yet
  4. The 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 CI
  5. Compare 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: OutOfSync
  6. Reconcile: 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: Synced
  7. Drift: 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 history
  8. Self-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: true
  9. Roll 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)'
  10. 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

Quiz Unlocks after the last step.