CKAD · 5 December 2026 · 7 min read · Download as PDF
Kubernetes Kustomize examples: 12 you can copy
12 Kubernetes Kustomize examples to copy: dev and prod overlays, image tags, ConfigMaps that restart Pods, patches, Secrets and components. Plus the real error messages, and how to fix them.
These are 12 Kubernetes Kustomize examples you can copy, from a first kustomization.yaml to one patch that changes every Deployment. They all use the same small app, so you can follow along in one folder. Each one was run with kubectl 1.37, and the mistakes at the end show the real error messages. Kustomize is built into kubectl, so there is nothing to install.
TL;DR
- Preview any example with
kubectl kustomize <folder>. Apply it withkubectl apply -k <folder>. - Put the shared app in
base/, and each environment's differences inoverlays/. - Use the built-in fields first (
images,replicas,namespace,labels). Write a patch only when no field does the job. - Don't add
commonLabelsto a running app. In our test it cut off the app's traffic. Uselabels.
Find the example you need
I want to… | Use | Example |
|---|---|---|
Apply several files together |
| 1 |
Restart Pods when config changes |
| 2 |
Have separate dev and prod versions | an overlay | 3 |
Change the image tag or replica count |
| 4 |
Label everything |
| 5 |
Set CPU and memory | a merge patch | 6 |
Change one exact value | a JSON patch | 7 |
Change every Deployment at once | a patch with a | 8 |
Leave something out |
| 9 |
Make a Secret from a file |
| 10 |
Copy a value from one resource to another |
| 11 |
Reuse an optional setting |
| 12 |
The app and its folders
base/ kustomization.yaml deployment.yaml service.yamloverlays/ dev/kustomization.yaml prod/kustomization.yaml prod/resources.yamlcomponents/ debug/kustomization.yaml
Open this diagram in archhy, our sister product for architecture diagrams, and press Make a copy in archhy to edit your own.
The app is one nginx Deployment and a Service:
# base/deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: webspec: replicas: 1 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: web image: nginx:1.28 ports: - containerPort: 80 envFrom: - configMapRef: name: web-config# base/service.yamlapiVersion: v1kind: Servicemetadata: name: webspec: selector: app: web ports: - port: 801. Apply several files together
List the files under resources. This is the base that every other example builds on:
# base/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- deployment.yaml- service.yamlconfigMapGenerator:- name: web-config literals: - LOG_LEVEL=infokubectl kustomize base # print the resultkubectl apply -k base # apply it2. Restart Pods when the config changes
The configMapGenerator in that base makes a ConfigMap with a hash of its contents in the name, and points the Deployment at it:
kind: ConfigMapmetadata: name: web-config-hf678c7m2bChange LOG_LEVEL and the name changes, so the Deployment starts new Pods with the new value. The old ConfigMap stays in the cluster. That's useful: when we ran kubectl rollout undo, the Pods went back to the old value. The old ones build up, and kubectl delete -k doesn't remove them. Delete one only when you no longer need to roll back to it.
3. A production overlay
An overlay points at the base and lists only what's different. This one is used in examples 3 to 6:
# overlays/prod/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: prodnamePrefix: prod-labels:- pairs: env: prodresources:- ../../baseimages:- name: nginx newTag: 1.29-alpinereplicas:- name: web count: 3configMapGenerator:- name: web-config behavior: merge literals: - LOG_LEVEL=warnkubectl create namespace prodkubectl apply -k overlays/prodYou get a Deployment and a Service called prod-web in the prod namespace. behavior: merge changes the base's ConfigMap instead of making a second one.
4. Change the image tag and the replica count
These lines in the overlay do it, with no patch:
images:- name: nginx # the image name in the base, not the container name newTag: 1.29-alpinereplicas:- name: web # the Deployment's name in the base, before the prefix count: 3Check the spelling. If no image matches name, Kustomize changes nothing and shows no error.
5. Add a label to everything
labels:- pairs: env: prodThis labels the Deployment, the Service and the ConfigMap, and leaves selectors alone. To label the Pods too, add includeTemplates: true beside pairs.
6. Set CPU and memory with a merge patch
A merge patch (its full name is a strategic merge patch) is a piece of the resource, with only the fields you want to add or change. Save this one beside the prod overlay:
# overlays/prod/resources.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: webspec: template: spec: containers: - name: web resources: requests: cpu: 100m memory: 128Mi limits: memory: 128MiThen add it to overlays/prod/kustomization.yaml:
patches:- path: resources.yamlThe container's name must match the base. With a different name, Kustomize adds a second container instead.
7. Change one exact value with a JSON patch
A JSON patch names a path and what to do there. This one makes the Service listen on port 8080:
# overlays/port/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basepatches:- target: kind: Service name: web patch: |- - op: replace path: /spec/ports/0/port value: 8080 - op: add path: /spec/ports/0/targetPort value: 808. Change every Deployment at once
Give the patch a target with only a kind, and it applies to all of them. This adds a seccomp profile, a basic security setting, to every Deployment:
# overlays/secure/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basepatches:- target: kind: Deployment patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: any spec: template: spec: securityContext: seccompProfile: type: RuntimeDefaultA patch still needs a name, but with a target Kustomize ignores it.
9. Leave something out
This overlay uses the base without its Service:
# overlays/no-service/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basepatches:- patch: |- $patch: delete apiVersion: v1 kind: Service metadata: name: webIt only leaves the Service out of the output. If the Service is already in the cluster, delete it yourself.
10. Make a Secret from a file
# overlays/login/login.env (keep this file out of Git)USERNAME=adminPASSWORD=example-only# overlays/login/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basesecretGenerator:- name: web-login envs: - login.envThe values in the Secret are base64, which is encoding, not encryption. Anyone who can read the output can read the password.
11. Copy a value from one resource to another
The prefix changes the Service's name, and this example puts the new name in an environment variable:
# overlays/shop/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basenamePrefix: shop-replacements:- source: kind: Service name: web fieldPath: metadata.name targets: - select: kind: Deployment name: web fieldPaths: - spec.template.spec.containers.[name=web].env.[name=SERVICE_NAME].value options: create: trueThe container gets SERVICE_NAME=shop-web. create: true adds the variable if it isn't there yet.
12. Reuse an optional setting
A component is a piece you can switch on in any overlay. This one turns on debug logging:
# components/debug/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1alpha1kind: ComponentconfigMapGenerator:- name: web-config behavior: merge literals: - LOG_LEVEL=debug# overlays/dev/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: devresources:- ../../basecomponents:- ../../components/debugCheck before you apply
kubectl kustomize overlays/prod # the finished YAMLkubectl diff -k overlays/prod # what would change (exit code 1 means something would)kubectl apply -k overlays/prodKustomize errors and how to fix them
We caused each of these on purpose:
What you see | Why | Fix |
|---|---|---|
|
| Create it first |
| You used | Use |
| You gave | Give it the folder |
| The patch's | Use the base's name, without the prefix |
| The base already generates that ConfigMap | Add |
| The patch's container name is wrong, so it added a container. The preview looks fine; the cluster rejects it | Match the base's container name |
No error, but nothing changed | An image name or a patch | Check the spelling, then preview again |
The one that stops your traffic
We added the old commonLabels field to an app that was already running. The Deployment refused the change:
The Deployment "web" is invalid: spec.selector: … field is immutableBut the Service had already been updated to look for the new label. No Pod had it, so the Service had nowhere to send traffic, while the Pods still looked healthy. Applying the previous version fixed it. Adding the label with the labels field from example 5 worked, because it leaves selectors alone unless you set includeSelectors: true.
Common questions
Do I need to install Kustomize?
No. It's built into kubectl, and kubectl version --client shows which version you have. The separate kustomize tool is often newer, and adds commands such as kustomize edit.
Why does an older example show a warning?
Some fields are deprecated. They still work for now, but use resources instead of bases, patches instead of patchesStrategicMerge and patchesJson6902, labels instead of commonLabels, and replacements instead of vars.
Is Kustomize in the Kubernetes exams?
Yes. The Certified Kubernetes Application Developer (CKAD) exam lists it, and the Certified Kubernetes Administrator (CKA) exam asks you to use Helm and Kustomize to install cluster components.
Keep going
New to the idea? Start with Kustomize explained. Choosing a tool? Read Helm vs Kustomize. To practice on a real cluster, try the Gazeek CKAD Helm and Kustomize module.