Bases & Overlays
A base is a kustomization that describes the app the same way for every environment. An overlay is a kustomization that lists the base under resources and adds what's specific to one environment. Overlays don't copy the base; they reference it, so a fix in the base reaches every environment on the next build.
deploy/
├── base/
│ ├── kustomization.yaml
│ ├── deployment.yaml
│ ├── service.yaml
│ └── httproute.yaml
└── overlays/
├── staging/
│ ├── kustomization.yaml
│ └── route-host.yaml
└── production/
├── kustomization.yaml
└── route-host.yamllinkly's base and two overlays
The session builds this tree for linkly and renders both environments. The overlays use only fields from Kustomize Basics plus one small patch each (patches get their own lesson):
mkdir -p deploy/base deploy/overlays/staging deploy/overlays/production && cd deploy
cat > base/deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: linkly
spec:
replicas: 2
selector:
matchLabels: {app.kubernetes.io/name: linkly}
template:
metadata:
labels: {app.kubernetes.io/name: linkly}
spec:
containers:
- name: linkly
image: ghcr.io/acme/linkly:1.4.0
ports: [{name: http, containerPort: 8080}]
resources:
requests: {cpu: 100m, memory: 64Mi}
limits: {memory: 128Mi}
EOF
cat > base/service.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: linkly
spec:
selector: {app.kubernetes.io/name: linkly}
ports: [{name: http, port: 80, targetPort: http}]
EOF
cat > base/httproute.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: linkly
spec:
parentRefs: [{name: public, namespace: gateway-system}]
hostnames: [lnk.example.com]
rules:
- backendRefs: [{name: linkly, port: 80}]
EOF
cat > base/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
labels:
- pairs: {app.kubernetes.io/part-of: linkly}
resources: [deployment.yaml, service.yaml, httproute.yaml]
EOF
cat > overlays/staging/route-host.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: linkly
spec:
hostnames: [lnk.staging.example.com]
EOF
cat > overlays/staging/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: linkly-staging
resources: [../../base]
images:
- name: ghcr.io/acme/linkly
newTag: 1.5.0-rc.2
replicas:
- name: linkly
count: 1
patches:
- path: route-host.yaml
EOF
cat > overlays/production/route-host.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: linkly
spec:
hostnames: [lnk.example.com, go.example.com]
EOF
cat > overlays/production/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: linkly-prod
resources: [../../base]
replicas:
- name: linkly
count: 4
patches:
- path: route-host.yaml
EOF
for env in staging production; do
echo "=== $env"
kustomize build overlays/$env | kubeconform -strict -summary
done
echo "=== staging vs production"
diff <(kustomize build overlays/staging) <(kustomize build overlays/production)=== staging
Summary: 3 resources found parsing stdin - Valid: 3, Invalid: 0, Errors: 0, Skipped: 0
=== production
Summary: 3 resources found parsing stdin - Valid: 3, Invalid: 0, Errors: 0, Skipped: 0
=== staging vs production
7c7
< namespace: linkly-staging
---
> namespace: linkly-prod
22c22
< namespace: linkly-staging
---
> namespace: linkly-prod
24c24
< replicas: 1
---
> replicas: 4
34c34
< - image: ghcr.io/acme/linkly:1.5.0-rc.2
---
> - image: ghcr.io/acme/linkly:1.4.0
52c52
< namespace: linkly-staging
---
> namespace: linkly-prod
55c55,56
< - lnk.staging.example.com
---
> - lnk.example.com
> - go.example.com
The diff is the whole point: the two environments differ in exactly the lines the overlays set, and reviewing an environment change means reviewing that diff. Note how the patch replaced the hostnames list wholesale (lists in strategic-merge patches are replaced unless Kubernetes defines a merge key for them, as it does for containers by name).
What goes where
| Put it in the base | Put it in an overlay |
|---|---|
| object structure, ports, probes, security context | namespace |
| sensible default resources | resource sizes per environment |
| labels shared by all environments | environment labels (env: staging) |
| image name (and a default tag) | image tag or digest per environment |
| the HTTPRoute and its backend | hostnames, parent Gateway |
| replica counts, HPA bounds |
A useful rule: if a value is the same in every overlay, move it into the base; if the base has a value every overlay overrides, it's a placeholder and should say so.
Layering and sharing
- An overlay can be the base of another overlay:
overlays/production-eulists../productionand changes only the region-specific bits. Keep it to two or three levels, or nobody can tell where a value comes from. - One overlay can include several bases (
resources: [../../base, ../../monitoring]). - Bases can be remote:
https://github.com/acme/platform-manifests//linkly/base?ref=v1.4.0. Always pin a tag or commit withref; an unpinned remote base changes under your feet. Remote bases need the network at build time, which also makes builds slower and harder to reproduce, so many teams vendor them instead. - Kustomize refuses to read files outside the kustomization's directory tree unless they're listed as resources of another kustomization.
--load-restrictor LoadRestrictionsNonelifts that, but it's a smell.
Environments as directories vs branches
Kustomize assumes one directory per environment on one branch: promotion is a commit that changes the production overlay (often just its image tag), and the diff shows exactly what will change. Keeping an environment per Git branch looks similar but makes promotion a merge between long-lived branches, where unrelated changes travel along. The planned GitOps course builds on the directory layout.
Test yourself
What does an overlay's resources: [../../base] mean?
- Copy the base files into the overlay
- Build the base kustomization and use its output as input to this one
- Delete the base
- Merge the two directories on disk
Every overlay sets resources.limits.memory: 256Mi. What should you do?
- Nothing
- Move the value into the base
- Add a third overlay
- Use a remote base
Why pin remote bases with ?ref=?
- Kustomize requires it
- Without it the base follows the default branch and can change without any change in your repo
- It makes builds faster
- To enable caching