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.

Cover: Kubernetes Kustomize examples: 12 you can copy

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 with kubectl apply -k <folder>.
  • Put the shared app in base/, and each environment's differences in overlays/.
  • Use the built-in fields first (images, replicas, namespace, labels). Write a patch only when no field does the job.
  • Don't add commonLabels to a running app. In our test it cut off the app's traffic. Use labels.

Find the example you need

I want to…

Use

Example

Apply several files together

resources

1

Restart Pods when config changes

configMapGenerator

2

Have separate dev and prod versions

an overlay

3

Change the image tag or replica count

images, replicas

4

Label everything

labels

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 target

8

Leave something out

$patch: delete

9

Make a Secret from a file

secretGenerator

10

Copy a value from one resource to another

replacements

11

Reuse an optional setting

components

12

The app and its folders

Shell
base/  kustomization.yaml  deployment.yaml  service.yamloverlays/  dev/kustomization.yaml  prod/kustomization.yaml  prod/resources.yamlcomponents/  debug/kustomization.yaml
One base folder holds the web Deployment and Service. The dev overlay adds the dev namespace and debug logging. The prod overlay adds the prod namespace, 3 replicas and a newer image. Each overlay produces its own finished 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:

YAML
# 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
YAML
# base/service.yamlapiVersion: v1kind: Servicemetadata:  name: webspec:  selector:    app: web  ports:  - port: 80

1. Apply several files together

List the files under resources. This is the base that every other example builds on:

YAML
# base/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- deployment.yaml- service.yamlconfigMapGenerator:- name: web-config  literals:  - LOG_LEVEL=info
Shell
kubectl kustomize base      # print the resultkubectl apply -k base       # apply it

2. 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:

YAML
kind: ConfigMapmetadata:  name: web-config-hf678c7m2b

Change 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:

YAML
# 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=warn
Shell
kubectl create namespace prodkubectl apply -k overlays/prod

You 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:

Shell
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: 3

Check the spelling. If no image matches name, Kustomize changes nothing and shows no error.

5. Add a label to everything

Shell
labels:- pairs:    env: prod

This 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:

YAML
# overlays/prod/resources.yamlapiVersion: apps/v1kind: Deploymentmetadata:  name: webspec:  template:    spec:      containers:      - name: web        resources:          requests:            cpu: 100m            memory: 128Mi          limits:            memory: 128Mi

Then add it to overlays/prod/kustomization.yaml:

Shell
patches:- path: resources.yaml

The 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:

YAML
# 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: 80

8. 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:

YAML
# 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: RuntimeDefault

A patch still needs a name, but with a target Kustomize ignores it.

9. Leave something out

This overlay uses the base without its Service:

YAML
# overlays/no-service/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basepatches:- patch: |-    $patch: delete    apiVersion: v1    kind: Service    metadata:      name: web

It 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

Shell
# overlays/login/login.env   (keep this file out of Git)USERNAME=adminPASSWORD=example-only
YAML
# overlays/login/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basesecretGenerator:- name: web-login  envs:  - login.env

The 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:

YAML
# 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: true

The 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:

YAML
# components/debug/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1alpha1kind: ComponentconfigMapGenerator:- name: web-config  behavior: merge  literals:  - LOG_LEVEL=debug
YAML
# overlays/dev/kustomization.yamlapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: devresources:- ../../basecomponents:- ../../components/debug

Check before you apply

Shell
kubectl kustomize overlays/prod   # the finished YAMLkubectl diff -k overlays/prod     # what would change (exit code 1 means something would)kubectl apply -k overlays/prod

Kustomize errors and how to fix them

We caused each of these on purpose:

What you see

Why

Fix

namespaces "prod" not found

namespace: doesn't create the namespace

Create it first

no matches for kind "Kustomization"

You used -f on the folder

Use -k. The other files in the folder may already have been applied, so check for them

must build at directory … file is not directory

You gave -k a file

Give it the folder

no resource matches strategic merge patch

The patch's kind or name isn't in the base

Use the base's name, without the prefix

behavior must be merge or replace

The base already generates that ConfigMap

Add behavior: merge

containers[0].image: Required value

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 target matches nothing

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:

Shell
The Deployment "web" is invalid: spec.selector: … field is immutable

But 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.

More from the blog