Chapter 03: GitOps with Flux
Interactive Explainer
Video is being produced. Check back soon.
Learning Objectives
By the end of this chapter, you will be able to:
- Explain what you gain when a controller keeps the cluster equal to Git - and what that means for a change made by hand
- Trace any Flux-managed object to the Flux Kustomization and the Git file it comes from
- Recognise a suspended Kustomization, even when it says
Ready, and resume it - Preview with
flux diffwhat a change in Git would create, change or delete before it is merged
Recall Check
Before continuing, quickly recall:
- In the Chapter 01 story, why did Flux delete a worker and two NetworkPolicies from production? (Chapter 01)
- Why does Terraform ignore the labels on the environment namespaces? (Chapter 02)
- Which three commands prove that your cluster is green? (Chapter 00)
If you can’t answer these, revisit the corresponding chapter before proceeding.
Context
Terraform built your cluster and installed Flux (Chapter 02). From then on, everything else in the cluster - the applications, the databases, the network policies, the admission policies, the observability stack - comes from Git. Flux fetches the repository every minute, and each part of the cluster is then made to match it on its own interval - backend-production every 10 minutes, frontend-production every 5 - again and again.
That loop is what you get GitOps for:
- Git tells you what should run, and why. Every environment is a folder; every change to it is a commit with an author, a reason and a review. “What should production run, and who changed it?” is answered by
git log, not by asking around. Whether production really runs it is a second question: the revision a Kustomization has applied, and the application’s own signals, answer that one - this chapter’s incident is what happens when the two drift apart. - The cluster heals itself. A NetworkPolicy deleted by mistake, a Deployment edited by a script, a setting an attacker changed - at the next reconcile Flux puts back what Git says.
- Nobody needs to write to production by hand. Changes arrive through pull requests, so everyday access can be read-only. On the Hetzner cluster it is (Chapter 01): the OIDC sign-in - on-call’s too - gets the read-only role
oidc-restricted-readonlyinproduction; admins may additionally suspend, resume and reconcile Flux objects influx-system, nothing more. An emergency write to production takes the separate Terraform admin kubeconfig, kept for break-glass. - Going back and starting over are Git operations. A rollback is
git revert. A new cluster follows the repository and becomes the same platform - Chapter 00 built yours that way in minutes.
The same loop also sets back a fix made by hand, because to Flux it looks exactly like that mistaken edit. This chapter’s incident is what happens when a team treats that as Flux being in the way, instead of putting the fix where it lasts: in Git.
Work through each lesson section; the chapter video, once it is published, gives the concept overview.
How Flux Deploys from Git
One GitRepository fetches the repository. Each Kustomization applies one folder of it - the network policies of develop, the backend in production, the Kyverno policies - on its own schedule, with prune: true.