Lesson 135 · Section 11: Helm & Kustomize
Lesson 135: Kustomize basics: one set of manifests, changed without templates
How Kustomize changes plain manifests without templates: a kustomization.yaml lists resources and changes, kubectl applies it with -k, transformers set namespace, prefixes, labels and images, and generators make ConfigMaps that roll out by themselves. · 18 min read · 2 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
The problem Kustomize solves
The same application often runs in several places: a dev and a prod namespace, or three clusters. The
manifests are nearly identical: another namespace, a name prefix, one more label, a different image tag, more
replicas in production. Copying the folder for each place means every fix has to be made several times, and the
copies drift apart.
Helm solves this with templates. Kustomize takes a different route: your manifests stay plain, valid YAML that you could apply directly, and a separate file describes the changes to make to them. No placeholders, no template language. It has been built into kubectl since 1.14, so it is always there on an exam machine.
Parts:
- kustomization.yaml (what + changes)
- deployment.yaml (service.yaml)
- Generators (ConfigMap, Secret)
- Transformers (ns, labels, images)
- Plain YAML (kubectl kustomize)
- kube-apiserver (kubectl apply -k)
Connections:
- kustomization.yaml to Generators
- deployment.yaml to Transformers
- Generators to Transformers
- Transformers to Plain YAML
- Plain YAML to kube-apiserver: apply -k
A first kustomization
app/
├── kustomization.yaml
├── deployment.yaml
└── service.yaml
deployment.yaml and service.yaml are ordinary manifests, a Deployment web running nginx:1.27 and a
Service web that selects app: web. The kustomization:
# app/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
kubectl kustomize app/ # prints both manifests, unchanged so far
kubectl apply -k app/ # applies them
resources may list files, folders that contain their own kustomization.yaml, or URLs of remote
kustomizations. Every file must be listed: Kustomize does not pick up files in the folder by itself.
kubectl and the standalone binary
| Command | Does |
|---|---|
kubectl kustomize DIR | Print the built YAML (same as kustomize build DIR) |
kubectl apply -k DIR | Build and apply |
kubectl diff -k DIR | Build and compare with the live objects |
kubectl get -k DIR | Show the live objects the kustomization describes |
kubectl delete -k DIR | Build and delete those objects |
kubectl version --client shows the Kustomize version built into kubectl (Kustomize Version: v5.7.1 with
kubectl 1.37). The standalone kustomize binary adds editing commands such as kustomize edit set image, but
everything in the CKA can be done with kubectl and a text editor.
Transformers: change every object at once
Parts:
- namespace (metadata.namespace)
- namePrefix (names + references)
- labels (metadata (+ selectors))
- images (container image)
- Deployment web (prod-web)
- Service web (prod-web)
Connections:
- namespace to Deployment web
- namePrefix to Deployment web
- labels to Service web
- images to Deployment web
- namePrefix to Service web
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
namespace: shop # sets metadata.namespace on every namespaced object
namePrefix: prod- # web -> prod-web, and every reference to it
nameSuffix: -v2 # web -> prod-web-v2
labels:
- pairs:
team: payments
includeSelectors: false # metadata only (the default)
includeTemplates: true # also the Pod template
commonAnnotations:
owner: platform-team
images:
- name: nginx # matches the image name in the manifests
newTag: "1.29" # nginx:1.27 -> nginx:1.29
- name: busybox
newName: registry.local/busybox
kubectl kustomize . | grep -E '^ name:|namespace:|image:'
name: prod-web-v2
namespace: shop
name: prod-web-v2
namespace: shop
image: nginx:1.29
What makes these more than search-and-replace is that Kustomize knows which fields refer to other objects.
With namePrefix, a Deployment that mounts ConfigMap app-config is changed to mount prod-app-config, and a
RoleBinding's roleRef follows the renamed Role.
Labels and selectors
| Field | Adds the labels to | Use when |
|---|---|---|
labels with defaults | metadata.labels only | Tagging objects (team, owner) |
labels with includeTemplates: true | Also Pod templates | You want the label on the Pods too |
labels with includeSelectors: true | Also selectors and templates | Separating two copies of one app in the same namespace |
commonLabels (deprecated) | Everything, selectors included | Old kustomizations; prefer labels |
Generators: ConfigMaps and Secrets that roll out by themselves
Parts:
- app.properties (edited)
- app-config-7k8f2 (old content)
- app-config-g4h9t (new content)
- Deployment (reference updated)
- New Pods (rolling update)
Connections:
- app.properties to app-config-7k8f2
- app.properties to app-config-g4h9t
- app-config-g4h9t to Deployment
- Deployment to New Pods
configMapGenerator:
- name: app-config
files:
- app.properties # key app.properties, content of the file
literals:
- LOG_LEVEL=info
secretGenerator:
- name: db
literals:
- password=s3cr3t
type: Opaque
kubectl kustomize . | grep -A1 -E 'kind: (ConfigMap|Secret)'
kind: ConfigMap
metadata:
name: app-config-7k8f2m9b4c
kind: Secret
metadata:
name: db-5tg8bh2k6d
The suffix is a hash of the content. Every reference to app-config in the loaded manifests (a volume, an
envFrom, a configMapKeyRef) is rewritten to the hashed name. Edit app.properties, apply again, and a ConfigMap
with a new name appears; the Deployment's Pod template now points to it, so the Deployment rolls out new Pods with
the new configuration. Lesson 50 showed this "versioned configuration" habit by hand; the generator does it for you.
| Option | Effect |
|---|---|
files, literals, envs | Where the data comes from, as with kubectl create configmap |
behavior: merge / replace | In an overlay, change a ConfigMap generated by the base (lesson 204) |
options.disableNameSuffixHash: true | Keep the plain name (and lose the automatic rollout) |
generatorOptions (top level) | The same options for every generator in the file |
Example · Deploy the same app to a namespace with a new tag
Task: "the manifests in /opt/app deploy web. Deploy them to namespace staging, with every object name
starting stg-, image nginx:1.29, without editing the manifests."
cd /opt/app
cat kustomization.yaml 2>/dev/null || ls
If there is no kustomization yet, create one next to the manifests:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
namespace: staging
namePrefix: stg-
images:
- name: nginx
newTag: "1.29"
kubectl create namespace staging
kubectl kustomize . # check names, namespace, image
kubectl apply -k .
kubectl -n staging get deploy,svc
The namespace field sets the namespace on the objects, but it does not create the Namespace: create it first,
or add a namespace.yaml to resources.
Tricks
Common mistakes
- A file in the folder that is not listed in
resources: it is silently left out. kubectl apply -f DIRon a kustomization folder: kubectl applies the raw files and fails onkustomization.yaml, which is not a Kubernetes object. Use-k.- Expecting
namespace:to create the Namespace. - Adding selector labels to an applied Deployment and hitting
field is immutable. - An
imagesentry whosenamedoes not match the image in the manifest (nginxvsdocker.io/nginx): nothing changes. - Referring to a generated ConfigMap from a manifest outside the kustomization: only loaded resources get the hashed name.
Recap
kustomization.yamllistsresourcesand the changes to make; the manifests stay plain YAML.kubectl kustomize DIRto look,kubectl apply|diff|get|delete -k DIRto act.namespace,namePrefix/nameSuffix,labels,commonAnnotationsandimageschange every object, and references follow renamed objects.configMapGenerator/secretGeneratoradd a content hash to the name, so changed configuration rolls out automatically.- Next lesson: patches for changes that apply to one object, and overlays for several environments.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Repair a Kustomize overlay that no longer builds, then write the production overlay from the same base.
Build · Level 2 · standard · target 20 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.