Sub topic 14
Repository layout and use with GitOps tools
This chapter assumes you can write a base and an overlay, use patches, and render with kubectl kustomize. It covers how to lay out a repository and how GitOps tools consume it.
A layout that scales
Keep one base and one overlay per environment. Put optional features in a components directory.
deploy/
base/
deployment.yaml
service.yaml
kustomization.yaml
components/
metrics/
kustomization.yaml
overlays/
staging/
kustomization.yaml
production/
kustomization.yaml
resources-patch.yamlEach directory that Kustomize builds has its own kustomization.yaml. An overlay refers to the base with a relative path, such as ../../base. The environment name is the directory name, so a reader can see all differences between staging and production by comparing two small directories.
A few rules keep the layout healthy:
- Keep the base valid on its own. Anyone should be able to run
kubectl kustomize baseand get working manifests. - Put only differences in overlays. If an overlay repeats most of the base, the shared part belongs in the base.
- Avoid overlays that build on other overlays. A chain such as
productiononstagingonbasemakes it hard to know where a value comes from. - Keep the application source and the
deploy/directory in one repository when one team owns both. Use a separate configuration repository when many services share one release process or when you want deployments to have their own review rules.
How a GitOps tool uses it
In GitOps, the Git repository is the source of truth for what runs in the cluster. A controller in the cluster watches the repository and applies changes. Argo CD and Flux both understand Kustomize, so you do not run kubectl apply -k yourself.
You point each environment at its overlay path. With Argo CD, the path field of an Application selects the directory:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: web-production
namespace: argocd
spec:
project: default
source:
repoURL: https://git.example.com/team/web-config.git
targetRevision: main
path: deploy/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: trueArgo CD detects the kustomization.yaml in that path and renders it. With Flux, a Kustomization resource from kustomize.toolkit.fluxcd.io does the same job. Do not confuse it with the file of the same name. The Flux resource tells the controller where to look and how often to sync. The kustomization.yaml file describes the transformations.
Releasing by changing the overlay
The images field fits GitOps well. To release version 1.2.4, a pipeline changes newTag in the production overlay and commits. The commit is the release record, and reverting it is the rollback. A CI job can make the edit with:
cd deploy/overlays/production
kustomize edit set image registry.example.com/web=registry.example.com/web:1.2.4
git commit -am "Release web 1.2.4 to production"Common errors
- The controller renders something different from your laptop. The Kustomize version bundled in the controller can differ from your local one. Pin or check the version when output differs.
- Remote bases that move. A base referenced by a URL without a tag or commit changes whenever the remote branch does. Pin to a tag or commit SHA.
- Files outside the overlay directory. Kustomize refuses to load files outside the kustomization root by default. Reference other directories as bases or components, not as plain file paths.
- Secrets in Git. Generators with literal secret values store them in plain text. Use an encrypted-secret approach, such as SOPS or an external secrets operator, for real credentials.
Exercise
A team wants production to be a copy of staging with only the replica count changed. One engineer proposes making overlays/production build on ../staging. Why might you keep both overlays building directly on ../../base instead?
Show answer
Chained overlays hide where each value comes from, and a change to staging then silently changes production. With both overlays on the base, each overlay lists only its own differences, and a change to staging cannot affect production.