Sub topic 4
Structuring a base and environment overlays
This chapter assumes you can read Kubernetes YAML, have used kubectl apply, and know what a Deployment, Service and ConfigMap are. It also assumes you have seen a basic kustomization.yaml that lists resources.
The idea
A base is a directory of plain, valid manifests plus a kustomization.yaml. An overlay is another directory with its own kustomization.yaml that points at the base and declares the differences for one environment. Kustomize loads the base, applies the overlay's changes, and prints the result. The base files are never edited or copied.
Directory layout
app/
├── base/
│ ├── kustomization.yaml
│ ├── deployment.yaml
│ └── service.yaml
└── overlays/
├── staging/
│ └── kustomization.yaml
└── production/
├── kustomization.yaml
└── resources-patch.yamlThe base Deployment is ordinary YAML with no placeholders. You could apply it on its own.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080The base kustomization.yaml lists what belongs to the base:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
configMapGenerator:
- name: web-config
literals:
- LOG_LEVEL=debugWriting an overlay
The overlay lists the base directory under resources. The path is relative to the overlay's own directory, so ../../base climbs out of overlays/production.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: production
namePrefix: prod-
replicas:
- name: web
count: 5
images:
- name: registry.example.com/web
newTag: 1.2.3
patches:
- path: resources-patch.yaml
target:
kind: Deployment
name: web
configMapGenerator:
- name: web-config
behavior: merge
literals:
- LOG_LEVEL=warnEach field does one job:
| Field | Effect in this overlay |
|---|---|
| `resources` | Pulls in the base and everything it declares |
| `namespace` | Sets the namespace on every namespaced resource |
| `namePrefix` | Prepends `prod-` to resource names |
| `replicas` | Sets the replica count of the Deployment named `web` |
| `images` | Changes the tag without editing the Deployment |
| `patches` | Merges the fields in `resources-patch.yaml` into the base Deployment |
| `configMapGenerator` with `behavior: merge` | Overrides `LOG_LEVEL` and keeps other base keys |
Note that the patch and replicas entry refer to the name web, which is the name in the base. Kustomize matches on the original name, and the prefix is added afterwards.
Building an overlay
Point the build at the overlay directory, not at the base:
kubectl kustomize overlays/productionThe output contains a Deployment named prod-web in the production namespace, with 5 replicas and image tag 1.2.3. The ConfigMap name carries a hash suffix, and the Deployment's reference to it is rewritten to match.
Common errors
- Building the wrong directory. Running the build on
baseshows the unmodified base. Always target the overlay. - Using the prefixed name in a patch. A patch for
prod-webfinds nothing. Useweb. - Missing
behavior: merge. Declaring a generator with the same name in an overlay without abehaviorfails with a conflict. Usemergeto add or override keys, orreplaceto discard the base values. - Wrong relative path.
resources: ../basefromoverlays/productionpoints at a nonexistentoverlays/base. Count the directory levels.
Exercise
You add overlays/staging/kustomization.yaml with resources: [../../base] and a patch targeting kind: Deployment, name: prod-web. The build output shows no change from the patch. Why, and how do you fix it?
Show answer
Patch targets match the resource's original name from the base, which is web. The prefix prod- belongs only to the production overlay, and it is applied after matching. Change the target name to web.