GitOps and delivery
Flux CD v2
1.What is Flux ?
Flux watches a Git repo (or OCI image or bucket) and keeps the cluster matching it, forever, by itself.
You push to Git, Flux applies it. No kubectl apply from laptops.
- CNCF Graduated (Nov 2022)
- Latest : Flux v2.9.6
- Helm, Kustomize and plain YAML all work
- Runs in the
flux-systemNamespace
Sources say where (Git, OCI, Helm). Appliers say what (Kustomization, HelmRelease).
2.Why GitOps ?
GitOps = Git is the single source of truth, declarative, pulled automatically, continuously reconciled.
- Pull model : CI needs no cluster credentials
- Drift : live edits get reverted by Flux
- Rollback : just
git revert - Many clusters : one repo, different paths
- Reconcile runs every
spec.interval - Also runs on a webhook (Receiver)
prune: true: delete in Git, deleted in cluster- Per-Kustomization
serviceAccountNamelimits a team kubectl get fluxcd -Ashows all- Flagger, a sister project, adds canary and blue/green
3.The Controllers (Toolkit)
Small controllers, each owns its CRDs. Composable like Lego.
Dashed ones are optional.
1.The Reconcile Loop
Drift gets undone at the next loop. A failure keeps the last good state and retries.
- Fetch source
- Build (kustomize / helm template)
- Diff vs live (dry-run SSA)
- Apply, only if drift or new revision
- Health check, set status Ready
- Wait interval, loop
2.Getting Started
flux bootstrapinstalls controllers in flux-system- Define a source (GitRepository, OCIRepository, HelmRepository)
- Define what to apply (Kustomization or HelmRelease)
- Push to Git
- Flux fetches, applies, runs health checks
- Drift and deletions fixed at next interval
- notification-controller tells Slack or Git
- Use
flux create source git ... --exportto make YAML you commit. - After bootstrap,
flux checkshould show every controller Ready.flux get all -Alists the rest.
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata: {name: app, namespace: flux-system}
spec:
interval: 1m
url: https://github.com/example/app
ref: {branch: main}
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata: {name: app, namespace: flux-system}
spec:
interval: 10m
path: ./deploy
prune: true
wait: true
timeout: 3m
sourceRef: {kind: GitRepository, name: app}3.CRD Cheat Sheet
| Kind | apiVersion | Controller | What it does |
|---|---|---|---|
| GitRepository | source.toolkit.fluxcd.io/v1 | source | Git branch, tag, semver or commit |
| OCIRepository | source.toolkit.fluxcd.io/v1 | source | OCI artifact as source |
| HelmRepository | source.toolkit.fluxcd.io/v1 | source | Helm chart index (http or oci) |
| Bucket | source.toolkit.fluxcd.io/v1 | source | S3-compatible bucket |
| Kustomization | kustomize.toolkit.fluxcd.io/v1 | kustomize | Apply a path from a source |
| HelmRelease | helm.toolkit.fluxcd.io/v2 | helm | Declares a Helm release |
| Provider / Alert | notification.toolkit.fluxcd.io/v1 | notification | Where and which events to send |
| ImagePolicy | image.toolkit.fluxcd.io/v1 | image-reflector | Choose latest tag |
Check versions with kubectl api-resources | grep toolkit
1.Image Automation
Flux can write new image tags back to Git. Then the normal pull flow deploys them.
flux bootstrap ... --components-extra2.flux Commands
| Command | Description | Example |
|---|---|---|
flux check --pre | Check the cluster meets requirements (K8s v1.34.1+) | flux check |
flux bootstrap github | Install controllers, commit to Git, self-manage | flux bootstrap github --owner=loren --repo=fleet --path=clusters/prod |
flux get all -A | Status of every Flux object | flux get kustomizations |
flux reconcile kustomization <name> | Sync now | flux reconcile kustomization flux-system --with-source |
flux logs --level=error -A | Controller logs | flux logs -f --kind=Kustomization --name=apps |
flux trace deployment/<name> | Who owns this object | flux trace deployment/podinfo -n default |
flux suspend / resume kustomization | Pause for a live hotfix | flux suspend kustomization apps |
flux diff kustomization <name> | Preview changes vs live | flux diff kustomization apps --path=./apps |
3.Flux vs Argo CD vs CI Push
- Two things named Kustomization (Flux vs
kustomize.)config. k8s. io - v1beta2 / v2beta2 removed in v2.8, run
flux migrate - Hotfix ?
flux suspendfirst, or the next loop reverts it prune: truedeletes what you drop from Git- Encrypt secrets (SOPS or External Secrets)
dependsOnneedswaitto really wait- Interval minimum is 60 seconds
- Stuck Ready=False ? Read the message
- Repo layout, health, drift, multi-tenancy : Flux deep dive
1.Flux Operator in brief
- An operator from ControlPlane that installs and upgrades Flux from one
FluxInstancenamedflux, instead offlux bootstrap. distribution.version: "2.9.x"floats patches only. Pin the minor on purpose.- A
FluxReportholds the observed state. AGPL-3.0, pre-GA (0.x). More : Flux deep dive.
2.The Flux Web UI
In the operator since v0.37.0, on by default, plain HTTP on port 9080 (Service flux-operator, leader pod only).
- ShowsFlux objects, workloads, logs, events, graph, history
- DoesReconcile, suspend, resume, download, restart, run job, delete pod
- NeverEdits manifests : Git is the way in
3.Login modes and roles
| Mode | Runs as | Actions |
|---|---|---|
| None | Operator SA (cluster-admin) | Off |
| Anonymous | One shared identity | By RBAC |
| OAuth2 (OIDC) | Each user, via CEL | By RBAC |
- flux-web-user
get, list, watchon*/*: Secrets too - flux-web-adminSame, plus
reconcile, suspend, resume, download, restart
valuesFrom Secrets in clear. RBAC has no "except" : list core minus secrets plus every other group, and keep get on ResourceSets.4.How a request is authorized
- No session : 401, then
/oauth2/authorizeanswers 303,Locationon the IdP (code + PKCE). - Code to
/oauth2/callback, ID token verified. - CEL
validationspass; claims become user and groups (both empty : refused). - Calls go as the operator SA, with impersonation.
- RBAC answers for the user. Buttons first run a SelfSubjectAccessReview.
Impersonatedalso needsget+patch,FineGrained(v0.51.0) only the verb.
A removed group keeps access until the ID token expires. Keycloak : Full group path off, so groups read team, not /team, as in RBAC. See OIDC auth; Argo CD has its own policy.
5.Exposing it safely
- No auth :
port-forwardonly. Else its own host : HTTPRoute toflux-operator:9080, TLS at the Gateway. - FluxInstance
cluster.networkPolicy: true(default) admits onlyflux-systempods.install.yamlhas no web policy, so admit the proxy pods to 9080 (NetworkPolicy). The chart's policy admits all namespaces. - Config holds
clientSecret: a SOPS Secret, hot-reloaded.
6.Commands
| Command | Does |
|---|---|
kubectl -n flux-system port-forward svc/flux-operator 9080:9080 | Open the UI on localhost:9080, nothing exposed |
flux-operator create secret web-config ui --base-url=... --issuer-url=... --client-id=... --client-secret-stdin --export | Config Secret as YAML (OIDC by default). Encrypt it |
kubectl auth can-i list secrets --as=jane --as-group=viewers | What a UI user may do (should be no) |