CKAD — Kustomize Basics

1 minute read

What Is Kustomize?

Kustomize lets you customize Kubernetes YAML without templates. Instead of Go template syntax (like Helm), it uses overlays and patches to modify base resources. Kustomize is built into kubectl — no extra installation needed.

How Kustomize Works

The core idea is base + overlay:

base/                    ← Original resources
├── deployment.yaml
├── service.yaml
└── kustomization.yaml

overlays/
├── dev/                 ← Dev-specific patches
│   ├── kustomization.yaml
│   └── patch-replicas.yaml
└── prod/                ← Prod-specific patches
    ├── kustomization.yaml
    └── patch-replicas.yaml
  • Base contains your standard resource definitions
  • Overlays contain environment-specific modifications
  • Each directory has a kustomization.yaml that tells Kustomize what to include and how to modify it

Base Resources

# base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: api
        image: myapp:v1
        ports:
        - containerPort: 8080
# base/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: api
spec:
  selector:
    app: api
  ports:
  - port: 80
    targetPort: 8080
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml

The kustomization.yaml in the base simply lists the resources to include.

Dev Overlay

# overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: dev-
namespace: development
patches:
- target:
    kind: Deployment
    name: api
  patch: |
    - op: replace
      path: /spec/replicas
      value: 1

This overlay:

  • References the base resources (../../base)
  • Adds dev- prefix to all resource names
  • Sets namespace to development
  • Patches replicas to 1

Production Overlay

# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: prod-
namespace: production
patches:
- target:
    kind: Deployment
    name: api
  patch: |
    - op: replace
      path: /spec/replicas
      value: 5
    - op: replace
      path: /spec/template/spec/containers/0/image
      value: myapp:v2

This overlay scales to 5 replicas and uses a different image version for production.

Using Kustomize with kubectl

# Preview the customized output (dev)
kubectl kustomize overlays/dev/

# Apply the dev overlay
kubectl apply -k overlays/dev/

# Apply the production overlay
kubectl apply -k overlays/prod/

# Delete resources managed by kustomize
kubectl delete -k overlays/dev/

Key commands: kubectl kustomize <dir> previews the output. kubectl apply -k <dir> applies it. The -k flag tells kubectl to use Kustomize.


Next: Kustomize Transformations

Updated: