Kustomize in depth

Intermediate · 25 min

Beyond base and overlay: hashed generators, both patch styles, labels without selector damage, components, image and replica overrides, and Helm charts.

Before thisHelm charts in depthConfigMaps

DevOps Journey · 25 min

Kustomize in depth

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by DevOps JourneyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Kustomize builds plain YAML from a kustomization: resources, then transformers (namespace, name prefix, labels, images, replicas), then patches. kubectl kustomize and kubectl apply -k use a built-in copy (v5.8.1 in kubectl 1.37).
  • configMapGenerator and secretGenerator add a content hash to the name and rewrite every reference to it. Changing a value changes the name, which rolls the Deployment.
  • Patches come in two styles: strategic merge (a partial object, merged by Kubernetes' merge keys) and JSON 6902 (explicit op/path/value), both under patches: with an optional target selector.
  • Use labels: with includeSelectors: false for labels that may change. The deprecated commonLabels also writes selectors, which are immutable on Deployments.
  • Components are reusable, optional slices of configuration (a debug mode, a sidecar, TLS) that several overlays can opt into, which plain bases can't express cleanly.
  • helmCharts renders a chart with helm template, then patches the output, but it's plain helm template: no release, no hook ordering, and hook and test resources become ordinary objects.

When base plus overlay stops being enough

The devops lesson introduced Kustomize as tracing paper over plain YAML: a base, an overlay per environment, a few patches. Real repositories outgrow that quickly. Config changes don’t restart Pods. A label added in one overlay breaks the next deploy. Staging and dev both want “debug mode” but not each other’s settings. And someone wants to install a vendor’s Helm chart and patch one field the chart forgot.

Kustomize has an answer for each, mostly in fields people never read about. Everything here was rendered with the Kustomize v5.8.1 built into kubectl v1.37.3 (kubectl version reports it).

How a build works

kubectl kustomize dir (or kustomize build) reads kustomization.yaml, loads its resources (files, directories, other kustomizations, remote Git URLs), runs generators, applies built-in transformers in a fixed order (namespace, prefixes and suffixes, labels and annotations, images, replicas), applies patches, then fixes up name references and prints YAML. Nothing touches a cluster until kubectl apply -k.

The base for this lesson:

# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: [deployment.yaml, service.yaml]
labels:
- pairs: {app.kubernetes.io/part-of: shop}
  includeSelectors: false
configMapGenerator:
- name: api-config
  literals: [LOG_LEVEL=info, CACHE_TTL=60]

And the production overlay:

# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: shop-prod
namePrefix: prod-
resources: [../../base]
images:
- name: registry.example.com/api
  newTag: 1.4.2
replicas:
- name: api
  count: 5
patches:
- target: {kind: Deployment, name: api}
  patch: |-
    - op: replace
      path: /spec/template/spec/containers/0/resources/limits/memory
      value: 1Gi
- patch: |-
    apiVersion: apps/v1
    kind: Deployment
    metadata: {name: api}
    spec:
      template:
        spec:
          topologySpreadConstraints:
          - maxSkew: 1
            topologyKey: topology.kubernetes.io/zone
            whenUnsatisfiable: ScheduleAnyway
            labelSelector: {matchLabels: {app: api}}
configMapGenerator:
- name: api-config
  behavior: merge
  literals: [CACHE_TTL=300]

The rendered Deployment, trimmed:

metadata:
  labels:
    app.kubernetes.io/part-of: shop
  name: prod-api
  namespace: shop-prod
spec:
  replicas: 5
  selector:
    matchLabels:
      app: api
  template:
    spec:
      containers:
      - envFrom:
        - configMapRef:
            name: prod-api-config-449kbbk94m
        image: registry.example.com/api:1.4.2
        resources:
          limits:
            memory: 1Gi
      topologySpreadConstraints:
      - ...

Generators and the hash suffix

The ConfigMap came out as prod-api-config-449kbbk94m with CACHE_TTL: "300" and LOG_LEVEL: info. The overlay’s behavior: merge combined its literal with the base’s. Kustomize appended a hash of the contents and rewrote the Deployment’s configMapRef to match, after also applying the prod- prefix.

The hash is the point. Pods read ConfigMaps at start, so editing one in place changes nothing until something restarts them. When I changed CACHE_TTL=300 to 600, the name became prod-api-config-ktc922g479 and so did the reference. A changed Pod template means a normal rolling update, and rolling back the Deployment brings back the old ConfigMap name too. Old generated ConfigMaps linger until you prune them, which Argo CD does automatically. secretGenerator works the same way, though the inputs then live in your repo or come from files you must keep out of Git.

Two kinds of patch

Both appear in the overlay above, under the single patches: field:

  • JSON 6902 (op, path, value) is exact and order-sensitive. containers/0 means the first container, whatever it’s called. It’s good for replacing one scalar or removing a field.
  • Strategic merge is a partial object merged the way kubectl apply merges, with Kubernetes’ merge keys. Lists like containers merge by name, so you can patch a container by name without knowing its index. It’s good for adding blocks like the topology spread above.

A target can select by kind, name, namespace, label or annotation selector, so one patch can hit every Deployment in a build. The old patchesStrategicMerge and patchesJson6902 fields are deprecated in favour of patches.

Labels and the selector trap

labels with includeSelectors: false added app.kubernetes.io/part-of: shop to metadata but left spec.selector alone. The older commonLabels writes the label into selectors as well. Selectors on Deployments, StatefulSets and DaemonSets are immutable, so adding or changing a commonLabels value on a running app makes the next apply fail. Use commonLabels only for labels that will never change, or not at all.

Components

An overlay is one environment. A component is a reusable optional piece:

# components/debug/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
configMapGenerator:
- name: api-config
  behavior: merge
  literals: [LOG_LEVEL=debug]

A staging overlay with components: [../../components/debug] rendered LOG_LEVEL: debug with CACHE_TTL: "60" from the base, as api-config-bk8dk6k2k4. Any overlay can opt in. Components can carry resources, patches and generators, and they apply after the overlay’s own resources, so they can patch them. Typical uses are debug mode, a monitoring sidecar, TLS, and cloud-specific storage classes.

Helm charts inside Kustomize

helmCharts lets you install a third-party chart and patch it:

helmCharts:
- name: notes
  releaseName: web
  valuesInline:
    replicaCount: 2
patches:
- target: {kind: Deployment}
  patch: |-
    - op: add
      path: /spec/template/spec/priorityClassName
      value: business-critical

It needs --enable-helm and a Helm binary. Rendering the previous lesson’s chart this way produced the Deployment with replicas: 2 and the added priorityClassName. It also produced a kind: Pod, the chart’s helm test Pod. Kustomize runs helm template, so hooks and tests come out as ordinary resources, with no release, no hook ordering and no helm rollback. Delete them with a $patch: delete patch, or render through Helm when hooks matter.

About the video

The embed is DevOps Journey’s “Kustomize: The Best Way to Manage Your Kubernetes Configs”, November 2022, 25 minutes. Its two chapters cover the theory (bases, overlays, patches, generators) and a complete walkthrough. Some of the fields it uses, such as commonLabels and patchesStrategicMerge, are now deprecated as described above. Nick Janetakis’s 2024 video in deeper shows the migration away from them, including the immutable-selector trap. I judged the embed from chapters and description only, since transcripts couldn’t be fetched. English was judged from its “English (US) original” audio track.

What was run: every rendered output above came from kubectl v1.37.3’s built-in Kustomize v5.8.1, including both hash names, the component, and the helmCharts build using a Helm 4.3 binary built from source. Nothing was applied to a cluster for this lesson. Field semantics are from the Kustomize reference, checked 2026-11-29.

Where this fits

Something has to run these builds continuously against the cluster. Next: Argo CD in practice.

Check yourself

  1. 01You change LOG_LEVEL in a configMapGenerator and apply. Why do the Pods restart, when editing a ConfigMap by hand wouldn't restart them?

    The generator's name carries a hash of its contents, so the new data gives a new ConfigMap name, and Kustomize rewrites the Deployment's reference. The Pod template changed, so the Deployment rolls out.

  2. 02An overlay adds commonLabels: {team: payments} to an existing app, and the apply fails on the Deployment. Why?

    commonLabels is applied to selectors too, and a Deployment's spec.selector is immutable. Use labels: with includeSelectors: false to add it to metadata (and templates if wanted) without touching selectors.

  3. 03When would you use a component instead of another overlay?

    When the same optional feature, such as debug logging or an extra sidecar, should be mixed into several overlays. An overlay is one environment, and a component is a reusable piece that any number of overlays include.

  4. 04You render a chart through Kustomize's helmCharts and a test Pod appears in the output. Why?

    Kustomize runs helm template, which includes hook and test manifests as plain resources. There's no Helm release to treat them specially, so they're applied like everything else unless you remove them with a patch.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark