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

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.
my-app/
base/
kustomization.yaml
deployment.yaml
service.yaml
overlays/
staging/
kustomization.yaml
production/
kustomization.yaml
resources-patch.yaml
components/
debug-logging/
kustomization.yamlThe base
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
configMapGenerator:
- name: my-app-config
literals:
- LOG_LEVEL=infoThe 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
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-appThe patch file is a strategic merge patch, a partial Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 4
template:
spec:
containers:
- name: my-app
resources:
limits:
memory: 512MiThe 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. Itstargetpicks resources using these fields: group, version, kind, name, namespace,labelSelectorandannotationSelector.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.namespaceandlabels: 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
kubectl kustomize overlays/production
kubectl diff -k overlays/production
kubectl apply -k overlays/productionkubectl 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.
# 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-loggingBuilt, the staging overlay's ConfigMap holds LOG_LEVEL: debug.
Checks before you rely on it
- The embedded version. kubectl embeds Kustomize, and
kubectl version --clientprints the embedded version. On the machine that ran these builds, kubectl v1.35.0 printedKustomize Version: v5.7.1. Check yours against the fields you use. - Remote bases. A
resourcesentry can be a URL with?ref=and a tag. The Argo CD best practices warn that a remote base atHEADcan change meaning with no change in your own repository, and advise a Git tag or a commit SHA. - Deprecated fields. Run
kustomize edit fixon kustomizations that still use them. - Builds in CI. The builds above ran with no cluster connection. Run
kubectl kustomizefor every overlay on each pull request.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Helm or Kustomize: which one for which job
GitOps · 4 min read
Templating or patching, packaging, rollback and when to use the two tools together.

Promote a release between environments with GitOps
GitOps · 4 min read
A directory for each environment, promotion as one commit, automation, gates and rollback by revert.

Helm charts: rules for charts you can maintain
GitOps · 4 min read
Documented values, a schema, shared helpers, CI checks and clean versioning for Helm charts.