Kustomize for Kubernetes Configuration Management
Build and review Kubernetes overlays with Kustomize. Learn when to use bases, patches, and generators, and validate rendered manifests before applying them.
Kustomize applies overlays and patches to Kubernetes YAML without a template language. This guide shows a version-pinned base and production overlay, how to inspect the rendered manifest, and what to check in CI before applying it. It also explains generated ConfigMap and Secret names, namespace-scoped identity, and the limits of SecretGenerator.
Kustomize for Kubernetes Configuration Management
Kustomize changes Kubernetes manifests for different environments without adding a template language. A base holds the shared resources; an overlay declares the differences. The build still produces ordinary YAML, so reviewers can inspect what will be sent to the API server.
These examples use standalone Kustomize v5.8.3 and kubectl v1.37.1 with Kubernetes v1.37 APIs. They are separate tools with separate versions. kubectl kustomize uses the Kustomize version bundled with that kubectl binary; check it with kubectl version --client instead of assuming it matches your standalone Kustomize installation. See the Kustomize releases and kubectl reference before changing the versions used in a pipeline.
When Kustomize fits
Kustomize works well when environments share most of their manifests and differ in fields such as replica count, image tag, namespace, labels, or resource requests. A reviewer can read the patch or overlay and then inspect the rendered YAML.
Helm is often a better fit when you publish a reusable package with a values interface or need conditional templates. Kustomize does not evaluate Go templates or expose chart values. A common split is Helm for vendor charts and Kustomize for YAML the team owns. In either case, inspect the rendered manifests before applying them.
Base, overlay, and rendered output
A base lists the resources shared by environments. An overlay references that base and describes the changes for one target. Use resources, the current field, to include another Kustomization directory.
Example directory:
app/
├── base/
│ ├── kustomization.yaml
│ └── deployment.yaml
└── overlays/
└── production/
└── kustomization.yaml
app/base/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
app/base/deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app.kubernetes.io/name: web
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: web
template:
metadata:
labels:
app.kubernetes.io/name: web
spec:
containers:
- name: nginx
image: nginx:1.31.8-alpine
ports:
- containerPort: 80
app/overlays/production/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: production
namePrefix: prod-
replicas:
- name: web
count: 3
images:
- name: nginx
newTag: 1.31.9-alpine
From the repository root, render the overlay and inspect the result:
kubectl version --client
kubectl kustomize ./app/overlays/production
For this input, the Deployment portion of the output is:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/name: web
name: prod-web
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: web
template:
metadata:
labels:
app.kubernetes.io/name: web
spec:
containers:
- image: nginx:1.31.9-alpine
name: nginx
ports:
- containerPort: 80
The name prefix, namespace, replica count, and image tag are all visible in the result. In a real overlay, inspect every resource, including generated ConfigMaps and Secrets, before applying it. You can also render with the pinned standalone binary by running kustomize build ./app/overlays/production; use the same renderer in the review and deployment path so the output does not vary by tool version.
For a cluster-authenticated review, compare the overlay with the live resources before applying it:
kubectl diff -k ./app/overlays/production
kubectl apply -k ./app/overlays/production
kubectl diff needs API-server access. Exit status 0 means no difference, 1 means it found changes, and a status greater than 1 indicates an error. A diff is a review aid; it does not decide whether the application can safely use the change. See the kubectl diff reference.
Patches and reusable components
Use the patches field to change selected resources. A patch can be strategic-merge YAML or JSON Patch. Targets can match fields such as API group, version, kind, name, namespace, labels, and annotations. Keep the target narrow enough that an overlay does not change other resources by accident.
For example, this patch changes the replica count of one Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 5
Add it to the overlay:
patches:
- path: patches/production-replicas.yaml
target:
group: apps
version: v1
kind: Deployment
name: web
A component is an optional Kustomize package of resources and transformations that an overlay can include. The component API version is kustomize.config.k8s.io/v1alpha1; verify support in the pinned Kustomize version before adopting components across teams.
components/monitoring/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- service-monitor.yaml
Use it from an overlay:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
components:
- ../../components/monitoring
Components are useful when the same optional resource or patch belongs in several overlays. A small number of clear overlays is easier to review than a deep chain of environment, region, cluster, and tenant layers.
ConfigMap and Secret generators
A configMapGenerator can build a ConfigMap from a file, literals, or an environment file:
configMapGenerator:
- name: app-settings
files:
- application.properties
By default, generated ConfigMaps and Secrets have a content hash appended to their names. When the input changes, Kustomize renders a different name. It also updates recognized references in the rendered Pod template, so applying the new template can roll the workload. A Secret or ConfigMap reference inside a custom resource may not be recognized automatically; add a transformer configuration with a nameReference rule for that field.
Generator behavior only controls how a generated resource interacts with an existing resource of the same identity. The supported values are:
| Behavior | Meaning |
|---|---|
create |
Create the generated resource; fail if one with the same identity already exists. This is the default. |
replace |
Replace an existing resource with the same identity. |
merge |
Merge the generated data with an existing resource of the same identity. |
behavior does not disable or control the content hash. retention is not a valid behavior. If you set generatorOptions.disableNameSuffixHash: true, changing the input no longer changes the name; the application or deployment process must then trigger any required restart itself.
SecretGenerator is not a secret store
This overlay expects a password file at secrets/db-password.txt. A deployment process supplies that ignored file to the workspace; it must not be committed to Git.
secretGenerator:
- name: database-credentials
files:
- password=secrets/db-password.txt
The hash in the generated Secret’s name is not encryption. The rendered YAML contains the Secret data, encoded as base64, so builds, logs, artifacts, and pull-request output can expose it. Keep the input and rendered output out of source control and ordinary logs. Kubernetes Secret storage also needs appropriate access controls and encryption-at-rest configuration for the cluster.
When rotating this Secret, change the protected input, render again, and review the new Secret name and the references Kustomize updated. Apply it through the approved secret deployment path, then confirm the workload restarted and can use the new credential. Keep the old Secret until no workload or rollback path needs it. Basic kubectl apply -k does not automatically remove every old generated object; pruning and retention depend on the tool that owns deployment. Configure cleanup deliberately, after checking references and rollback requirements.
The Kubernetes SecretGenerator guide describes the generator workflow. For real credentials, also review the ConfigMaps and Secrets guide and your organization’s secrets pipeline.
Names, namespaces, and collisions
A Kubernetes resource is identified by its API group, kind/resource type, namespace when the resource is namespaced, and name. The API version is not a separate identity. Two Deployments named api can exist in different namespaces. A cluster-scoped resource, such as a StorageClass, has no namespace, so its name must be unique across the cluster. See the Kubernetes object names documentation.
Kustomize rejects duplicate resource identities within a single build. Separate teams can still apply the same namespaced kind and name into the same namespace; they are addressing the same live object and can overwrite or conflict with one another’s changes. Use namespace boundaries, clear ownership, labels, and a team-aware naming convention. namePrefix can reduce accidental overlap, but it is not a collision-proof ownership system.
Kustomize resolves resource paths relative to each kustomization.yaml. Keep bases and components inside the repository and use relative paths such as ../../base. Run builds from the repository root, and check that every referenced path exists. Absolute filesystem paths make the Kustomization machine-specific and are a poor fit for shared overlays.
Common build failures
Patch does not change the intended object
A patch that targets only kind: Deployment may match several Deployments. Add the API group and a unique name or label selector, then inspect the rendered output to confirm which object changed.
A build contains the same resource twice
Kustomize reports duplicate resource identities when the same object is included through both the base and overlay. Put a new resource in one place, or patch the base resource. A count of kind: lines is not a reliable duplicate check because several valid resources can share a kind.
A base or component path is missing
Run the build from a known repository location and verify the relative path from the overlay’s kustomization.yaml. For example:
pwd
kubectl kustomize ./app/overlays/production
Do not solve a path error with an absolute path outside the repository; fix the relative reference or the command’s working directory.
CI review sequence
Pin Kustomize, kubectl, kubeconform, conftest, and the policy bundle used in CI. Render once with the selected pinned Kustomize build, then validate that exact output against the target Kubernetes version and your policy rules.
mkdir -p build
kustomize version
kubectl version --client
kustomize build ./app/overlays/production > build/production.yaml
kubeconform -strict -summary -kubernetes-version 1.37.1 build/production.yaml
conftest test build/production.yaml
Include schemas for any custom resources when running schema validation. Keep the policy bundle and tool versions pinned so tooling changes do not silently change the result.
The deployment job needs cluster credentials for the diff. Review the output from the same Kustomize version used to build the manifest, then apply only after a person or approved deployment controller accepts it:
kubectl diff -f build/production.yaml
kubectl apply -f build/production.yaml
If your deployment process uses kubectl apply -k, use the same pinned kubectl binary to render, diff, and apply the overlay. kubectl diff is not a local schema check and cannot run without access to the target API server. The Kubernetes Kustomize guide covers the built-in commands.
Kustomize and Helm
| Kustomize | Helm | |
|---|---|---|
| Input | Kubernetes YAML, bases, overlays, and patches | Chart templates and values |
| Rendered result | Kubernetes manifests | Kubernetes manifests |
| Common fit | Environment differences in manifests your team maintains | Reusable packages with values and chart releases |
| Conditional logic | Limited; use patches or separate overlays | Available through chart templates |
Kustomize keeps the source close to the manifest the API receives. Helm provides a package and values interface that may fit third-party software or a reusable platform chart better. Teams can use both. See the Helm charts guide for chart behavior and GitOps guide for deployment ownership and pruning.
Further reading
- Kubernetes for the core concepts.
- Container image management for image tags and registries.
- Kustomize documentation
- Kubernetes SIG Kustomize project
Conclusion
The rendered YAML shows the names, namespaces, images, and generated references that the API server will receive. Keep the renderer pinned so CI validates the same output the deployment step applies. Treat generated Secret input and output as sensitive, and retire old copies only after consumers and rollback no longer need them.
Category
Related Posts
GitOps: Declarative Deployments with ArgoCD and Flux
Compare Argo CD and Flux for Kubernetes GitOps, including reconciliation, dependency ordering, pruning safeguards, and secret references.
ConfigMaps and Secrets: Managing Application Configuration in Kubernetes
Learn how Kubernetes ConfigMaps and Secrets work, how updates reach pods, and how to control Secret access and protect data at rest.
GitOps: Infrastructure as Code with Git for Microservices
Learn the GitOps operating model, reconciliation loop, repository patterns, secret boundaries, and trade-offs for Kubernetes deployments.