What Is GitOps? Deploying from Git, Explained
A teammate asks which version of the shop is running in production. One person ran a deploy job yesterday, someone changed a setting by hand on Tuesday, and nobody knows whether the cluster still matches anything written down. This lesson answers what is GitOps: deploying so that a Git repository says what should run, and software in the cluster keeps the two in step.
Desired state, kept in Git
As the Kubernetes course showed, you describe what you want in declarative YAML, and Kubernetes stores it as the desired state, while what runs now is the actual state (see What Is Kubernetes?). Without GitOps, the desired state lives only in the cluster, put there by whoever last ran kubectl apply.
GitOps keeps the desired state in a Git repository and makes it the single source of truth: when the cluster and Git disagree, Git is right. The repository holds Kubernetes manifests, YAML files that each describe an object such as a Deployment (not lesson 7's image manifest), or Helm values, the settings that fill a chart's blanks (see Helm Basics).
An agent, a controller running inside the cluster, does the deploying. In the flowchart, notice that people work only in Git; everything in the dashed box is the agent's job.
The aha is the loop at the bottom. A pipeline deploys once and stops; the agent keeps comparing, so a deploy becomes a gap the agent closes, not a command you run. To ship 1.5.0, you change the image tag in Git and merge.
The four GitOps principles
OpenGitOps, a Cloud Native Computing Foundation (CNCF) project that defines GitOps without favouring a vendor, gives four principles:
- Declarative. The desired state says what should exist, such as
replicas: 3, not the steps to get there. - Versioned and immutable. It is stored with its full history, and stored versions never change. A Git commit's name is a hash that covers its files, message and parent, so an old commit cannot be edited in place; protect the main branch (lesson 5) against force pushes that rewrite it.
- Pulled automatically. Software agents fetch the desired state from the source themselves.
- Continuously reconciled. Agents keep observing the actual state and work to apply the desired state. To reconcile is to make the two match.
Git is the classic store, hence the name, but any store that keeps immutable versions qualifies.
Push vs pull: who holds the keys?
In a push deployment, the CI pipeline deploys: after the tests pass, a job runs kubectl apply or helm upgrade against the cluster. So the pipeline needs cluster credentials, a key that can change the cluster, stored as a CI secret, and the cluster's API must be reachable from CI.
In a pull deployment, the agent inside the cluster fetches from Git and compares. The pipeline still tests and builds the image, but its last step is committing the new tag to Git, often as a pull request. Compare the arrows in the two panels:
The aha: in pull, the cluster reaches out to Git, so no cluster credentials sit in CI. A leaked CI secret cannot change production directly; an attacker would need a change merged past branch protection and review. So guard the config repository like production.
Two widely used agents are Argo CD and Flux, both open source. An Argo CD that also manages other clusters holds their credentials, but inside your platform, not in CI.
What you gain: reviews, drift correction, git revert
Every change is a reviewed pull request. Someone proposes, a teammate reviews, checks run, and Git records who changed what, when and why: the config repository's log is your deployment history.
Drift is detected and corrected. Drift is when the actual state moves away from the desired state, for example after someone runs kubectl scale by hand. Follow the diagram:
The aha is step 3: a change that is not in Git does not last, so commit any fix you want to keep. The agent manages only what its source describes; objects created by hand and never committed are left alone.
In Argo CD, an Application, a custom object Argo CD adds to Kubernetes, says which repository folder to deploy where. This one, adapted from Argo CD's documentation, follows the guestbook folder of its example repository:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: guestbook
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc # this same cluster
namespace: guestbook
syncPolicy:
automated:
prune: true # delete objects removed from Git
selfHeal: true # undo live changes (drift)
By default, Argo CD checks Git about every three minutes; a webhook, a message your Git host sends on each change, can tell it sooner. Without automated, it only marks the app OutOfSync and waits for someone to sync; without selfHeal, it applies new commits but leaves hand edits until the next sync. Flux reverts hand edits whenever it reconciles.
Rollback is git revert. Revert the commit that changed the tag (a revert is a new commit undoing an earlier one, as in lesson 10), merge it, and the agent deploys the previous version. Nothing is rebuilt if the old image is still in the registry. With automated sync on, use this route: Argo CD refuses its own rollback command there.
Watch-outs: secrets and repository layout
Secrets must not sit in plain text in Git: anyone who can read the repository can read them, and history keeps them after you delete the file. A Secret's YAML is only base64-encoded, which anyone can reverse (see ConfigMaps and Secrets). Two patterns help:
- Encrypt before you commit. Sealed Secrets turns a Secret into a SealedSecret that only a controller in your cluster can decrypt, safe even in a public repository, its README says.
- Commit a reference. The External Secrets Operator reads the value from a store such as AWS Secrets Manager or HashiCorp Vault and creates the Secret in the cluster; Git holds only where to look.
Keep app code and config apart. Argo CD's best practices recommend a separate config repository for manifests: its history reads as a list of deployments, people can commit code without being able to change production, and CI avoids a loop where committing a tag to the repository it builds from starts another build.
Check yourself
Version 1.5.0 is failing, so a teammate runs kubectl set image to put 1.4.2 back. Argo CD has selfHeal: true. What happens next, and what should they do instead?
Argo CD sees the live image differ from Git, which still says 1.5.0, and puts 1.5.0 back. Git is the source of truth, so revert the tag commit; the agent then deploys 1.4.2 and keeps it.
Your CI runners hold admin credentials for the production cluster. How does pull-based GitOps change that risk, and what must you protect instead?
The credentials leave CI: the agent inside the cluster applies changes, and CI only commits to Git. The config repository becomes the way into production, so protect its main branch and require reviews.
One repository holds the app's code and its manifests, and CI commits each new image tag back to it. Why might the builds never stop?
Each tag commit is a new push to the repository CI builds from, so it starts another build, which commits another tag. A separate config repository breaks the loop.