Skip to content

Kustomize basics — overlays without templating

Layer environment overrides over a single base with Kustomize — bases, overlays, patches, generators, and the Helm-vs-Kustomize choice.

Kustomize layers per-environment overrides over a shared base of Kubernetes manifests without templates. It ships with kubectl (-k) and is the default tool for “the same Deployment but with different replica counts, images, and env vars across dev/staging/prod.” This article covers the base/overlay pattern, the patch styles, the ConfigMap and Secret generators, and where Kustomize fits compared to Helm.

How to verify

What Kustomize would produce for a given overlay:

kubectl kustomize overlays/prod
kubectl diff -k overlays/prod
kubectl apply -k overlays/prod

kubectl kustomize renders the final YAML to stdout — review it the same way you’d review helm template. kubectl diff -k shows what would change in the cluster if you applied. kubectl apply -k does the apply, with all the usual apply semantics (label-based pruning if you pass --prune).

For repository hygiene, validate the kustomization itself:

kustomize build overlays/prod | kubeconform -strict -summary

What’s happening

A kustomization.yaml is a manifest that describes “take these inputs, apply these transformations, output the rendered YAML.” It is not a template — no {{ .Values }} interpolation. Inputs are existing YAML files (resources), bases (other kustomizations), and generators (ConfigMaps and Secrets generated from literals or files). Transformations are patches (strategic-merge or JSON-patch), images (find image: foo:1 and rewrite it to foo:2), name prefixes, labels and annotations applied across all resources, namespace overrides, and replica counts.

The typical layout is base/ (canonical Deployment, Service, etc.) and overlays/<env>/ (each with its own kustomization that pulls in the base and applies env-specific patches). Overlays can include other overlays; chains stay flat in practice.

ConfigMapGenerator and SecretGenerator produce ConfigMaps and Secrets whose names include a content hash (e.g. app-config-h7k9...). When the content changes, the name changes, the Deployment that references the ConfigMap updates its reference, and the rolling update triggers automatically. This solves the “I updated the ConfigMap but the Pods don’t notice” problem that plagues raw kubectl apply.

Kustomize and Helm overlap but solve different problems. Helm has a templating language and a package model — useful for distributing applications to others. Kustomize is for your own internal layering: same app, different environments, no template syntax in the YAML. Many teams use both: Helm for upstream charts, Kustomize for the per-environment overrides on top.

The procedure

  1. Lay out a base. A canonical Deployment, Service, and ConfigMap source file:

    base/
      kustomization.yaml
      deployment.yaml
      service.yaml
      configmap.yaml
    overlays/
      staging/kustomization.yaml
      prod/kustomization.yaml
    # base/kustomization.yaml
    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    resources:
      - deployment.yaml
      - service.yaml
    commonLabels:
      app: web
  2. Add a production overlay that overrides image, replicas, and one env var:

    # overlays/prod/kustomization.yaml
    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    namespace: team-payments
    resources:
      - ../../base
    images:
      - name: registry.example.com/web
        newTag: "1.4.3"
    replicas:
      - name: web
        count: 6
    patches:
      - target: { kind: Deployment, name: web }
        patch: |-
          - op: replace
            path: /spec/template/spec/containers/0/resources/requests/cpu
            value: "500m"
          - op: replace
            path: /spec/template/spec/containers/0/env/0/value
            value: "info"
  3. Strategic-merge vs JSON-patch. Strategic merge is friendlier for whole-object overrides; JSON-patch is precise when you need to surgically change one element of a list. Use strategic where you can; JSON-patch when you must.

    # strategic merge — same shape as the original
    patches:
      - path: patches/deployment-prod-cpu.yaml
        target: { kind: Deployment, name: web }
    # patches/deployment-prod-cpu.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata: { name: web }
    spec:
      template:
        spec:
          containers:
            - name: web
              resources:
                requests: { cpu: "500m" }
  4. Use ConfigMapGenerator to roll Pods automatically when config changes:

    # overlays/prod/kustomization.yaml
    configMapGenerator:
      - name: app-config
        literals:
          - LOG_LEVEL=info
          - MAX_CONN=200
        files:
          - nginx.conf=files/nginx.conf
    generatorOptions:
      disableNameSuffixHash: false

    The Deployment references app-config by base name; Kustomize rewrites the reference to the hashed name on every build.

  5. Compose overlays from multiple bases. A team with shared Postgres + shared Redis bases:

    resources:
      - ../../bases/postgres
      - ../../bases/redis
      - ../../apps/payments
  6. Apply to the cluster. With pruning, removing a resource from kustomization deletes it from the cluster on next apply:

    kubectl apply -k overlays/prod --prune -l app.kubernetes.io/part-of=payments

Common pitfalls

  • Forgetting to add a new file to resources: in the kustomization means the file is silently ignored — kubectl kustomize outputs nothing for it.
  • commonLabels mutate every resource’s labels — including the Service selector, which can break existing Pods that don’t carry the label yet; roll labels in carefully.
  • Strategic-merge on lists requires a name field to match entries — patches without the right key end up appending duplicates instead of merging.
  • Hashed ConfigMap names break references from things Kustomize doesn’t process (e.g. an Ingress annotation pointing to a ConfigMap name) — disable the hash for those or rewrite the reference explicitly.
  • The Kustomize built into kubectl lags the standalone CLI — for new features (replacements, helm chart inflation) install the latest standalone kustomize and call it from CI.

We operate Kustomize for clients with a single base per app, env-specific overlays in Git, generators that roll Pods on config change, and a render-then-apply workflow in CI that never relies on cluster-side magic. See /en/services/managed-operations/.