All guides

Kustomize overlays: one base for many environments

Kustomize customizes plain, template-free YAML and leaves the original files untouched. An overlay refers to a base and applies its own customization on top, and one base can serve several overlays.

Hands typing on a laptop that shows code

A base and its overlays

The Kubernetes documentation defines both terms. A base is a directory whose kustomization.yaml lists a set of resources and the customization that goes with them. An overlay is also a directory with a kustomization.yaml. Its file names other kustomization directories, and those are its bases. A base knows nothing about the overlays that use it, and several overlays can share one base.

The example below uses this layout. Every build result quoted comes from running it with kubectl kustomize.

Text

my-app/
  base/
    kustomization.yaml
    deployment.yaml
    service.yaml
  overlays/
    staging/
      kustomization.yaml
    production/
      kustomization.yaml
      resources-patch.yaml
  components/
    debug-logging/
      kustomization.yaml

The base

YAML

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml
configMapGenerator:
  - name: my-app-config
    literals:
      - LOG_LEVEL=info

The base lists a Deployment and a Service named my-app and generates a ConfigMap. The Deployment reads my-app-config through envFrom. kubectl kustomize base builds the base on its own.

The production overlay

YAML

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-app-production
namePrefix: prod-
resources:
  - ../../base
images:
  - name: registry.example.com/my-app
    newTag: 1.4.2
configMapGenerator:
  - name: my-app-config
    behavior: merge
    literals:
      - LOG_LEVEL=warn
patches:
  - path: resources-patch.yaml
    target:
      kind: Deployment
      name: my-app

The patch file is a strategic merge patch, a partial Deployment:

YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 4
  template:
    spec:
      containers:
        - name: my-app
          resources:
            limits:
              memory: 512Mi

The names, the namespace, the tag and the limits are this example's choices. Replace them with your own.

Built with kubectl kustomize overlays/production, the overlay gives a Deployment named prod-my-app in the my-app-production namespace, with four replicas, the memory limit and the image registry.example.com/my-app:1.4.2. The ConfigMap holds LOG_LEVEL: warn and is named prod-my-app-config- followed by a hash. The Deployment refers to it by that generated name.

The target names the Deployment my-app, as the base does. According to the patches reference, a patch may address a resource by any name the resource had earlier. To put another version in production, change newTag.

The fields to know

  • resources: each entry is a path to a file, or a path or URL that refers to another kustomization directory.
  • patches: a list of patches applied in the order given. Each one is a strategic merge patch or a JSON6902 patch, a file or an inline string. Its target picks resources using these fields: group, version, kind, name, namespace, labelSelector and annotationSelector.
  • images: modifies the name, tag or digest of an image with no patch.
  • namePrefix: adds a prefix to the names of all resources, generated ConfigMaps and Secrets included.
  • configMapGenerator: generates a ConfigMap from files or literals. A content hash is appended to the name, so a new ConfigMap is generated when the contents change.
  • namespace and labels: set a namespace and labels on all resources.

The reference marks bases, patchesStrategicMerge, patchesJson6902 and vars as deprecated. It says they will never be removed from the v1beta1 Kustomization API and will not be included in v1. Their replacements are resources, patches for both patch fields, and replacements. It marks commonLabels as deprecated in favor of labels. kustomize edit fix converts these fields. For vars the reference calls the conversion experimental and recommends running it in a clean Git repository.

Build, diff and apply commands

Shell

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

kubectl kustomize shows the resources built from a directory that contains a kustomization file. kubectl diff -k compares them with the state the cluster would be in if they were applied, and kubectl apply -k applies them. The standalone command for a build is kustomize build overlays/production.

Components for optional features

As of v3.7.0 Kustomize supports components. The documentation describes a component as a special type of kustomization that defines reusable pieces of configuration logic, to be included from multiple overlays. Its use case is an application with optional features, where each overlay enables a subset.

YAML

# components/debug-logging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
configMapGenerator:
  - name: my-app-config
    behavior: merge
    literals:
      - LOG_LEVEL=debug
---
# overlays/staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base
components:
  - ../../components/debug-logging

Built, the staging overlay's ConfigMap holds LOG_LEVEL: debug.

Checks before you rely on it

  1. The embedded version. kubectl embeds Kustomize, and kubectl version --client prints the embedded version. On the machine that ran these builds, kubectl v1.35.0 printed Kustomize Version: v5.7.1. Check yours against the fields you use.
  2. Remote bases. A resources entry can be a URL with ?ref= and a tag. The Argo CD best practices warn that a remote base at HEAD can change meaning with no change in your own repository, and advise a Git tag or a commit SHA.
  3. Deprecated fields. Run kustomize edit fix on kustomizations that still use them.
  4. Builds in CI. The builds above ran with no cluster connection. Run kubectl kustomize for every overlay on each pull request.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides