Kustomize: Manage Kubernetes Manifests with Bases and Overlays
Kustomize lets you reuse common Kubernetes YAML while adding environment-specific changes as overlays. A `kustomization.yaml` file declares the source resources, transformations, and patches; Kustomize renders the final manifests, which you can inspect with `kubectl kustomize` or apply with `kubectl apply -k`. Unlike Helm, it does not package charts or track a release history for each installation. The key difference is not only templating, but also which system tracks and reconciles deployed state.
This guide covers the Kustomize functionality built into kubectl, as described by the Kubernetes documentation. Built-in functionality can vary with the kubectl version, so check kubectl version and the matching documentation. Run the apply steps only in a dedicated test cluster and namespace you are authorized to use.
What Kustomize does and does not do
If you copy Kubernetes manifests to create separate development, staging, and production configurations, common changes must be repeated. The copies can drift, making it unclear which file is the source of truth. Kustomize keeps shared resources in a base and lets each environment’s overlay include that base and add only the differences. The rendered output remains ordinary Kubernetes resource YAML.
A kustomization.yaml file declares which files or directories to combine, which shared labels or namespace to add, and which patches or image changes to apply. Kustomize builds the output locally, so rendering itself does not require cluster access. However, verifying API support, authorization, admission policies, scheduling, or application readiness requires separate checks against a cluster.
Kustomize transforms declarative resources; it is not a deployment history database or a continuously running reconciliation controller. kubectl apply -k sends the rendered resources to the API at that time. It does not keep watching for manual changes and automatically restore the desired state later.
How bases and overlays fit together
A base is a directory containing a kustomization.yaml and shared resources. An overlay references the base under resources and adds settings such as a namespace, replica count, or image tag that differs by environment. The base does not need to know about its overlays, so multiple environments can reuse it.
A small project might look like this:
app/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── dev/
│ ├── kustomization.yaml
│ └── replicas-patch.yaml
└── prod/
├── kustomization.yaml
└── replicas-patch.yamlThe base Deployment defines the application’s shared image, port, and selector. This abbreviated example starts with two replicas:
# base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80The Service selects the Pods with the same label:
# base/service.yaml
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80# base/kustomization.yaml
resources:
- deployment.yaml
- service.yamlThe development overlay can include the base, set a dedicated namespace, and apply a replica patch:
# overlays/dev/kustomization.yaml
namespace: app-dev
resources:
- ../../base
patches:
- path: replicas-patch.yaml# overlays/dev/replicas-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1A production overlay can use the same base but specify its own namespace, replica count, and approved image version. Recording only the differences avoids copying every resource. Common changes can remain consistent, while environment-specific settings are easier to review. The overlay must point to the intended base, and a patch’s apiVersion, kind, and metadata.name must match its target.
Patches, image changes, and generated resources
The patches field changes selected fields in an existing resource. Small patches that each do one thing make it easier to review whether an environment changes replicas, resource limits, or labels. Kustomize supports strategic merge and JSON 6902 patches, but strategic merge does not behave identically for every resource and field. If a precise field change is needed or merge behavior is unclear, consider a JSON patch with an explicit target and inspect the rendered result.
The images field can change an image’s name, registry, or tag. Instead of hard-coding a development image in a shared base, an environment can supply an approved image reference. A mutable tag such as latest can resolve to different image contents when rendered or deployed again, reducing reproducibility. Choose a fixed version or digest according to your deployment policy.
configMapGenerator and secretGenerator can build ConfigMaps or Secrets from files or literals. When an input file changes, a content-based hash in the generated name can change too; Kustomize updates references to that generated resource. This can surface a configuration change in a Pod template and prompt a rollout. Secret generation does not encrypt the data or configure encryption for cluster storage. Keep sensitive values out of source repositories, rendered output, and logs, and choose a separate secret-management approach where needed.
Render, compare, and apply safely
You need a compatible kubectl and access to the project directory. Applying also requires the correct cluster context and namespace plus sufficient RBAC permissions. The following separates local inspection from commands that change cluster state.
kubectl config current-context
# Check the intended test cluster and namespace first; create app-dev only if it is absent
kubectl get namespace app-dev
kubectl create namespace app-dev
kubectl version
kubectl kustomize overlays/dev
kubectl diff -k overlays/dev
kubectl apply -k overlays/dev
kubectl get deployment,service -n app-dev
kubectl describe deployment web -n app-devCheck `kubectl config current-context` and `kubectl get namespace app-dev`. If the namespace is missing, create it only in an authorized test cluster; skip the create command if it already exists. Check the client version with `kubectl version`.
Run `kubectl kustomize overlays/dev` to render the final YAML. Review the namespace, image, replica count, matching labels and selectors, ports, and security settings. If unexpected resources appear, do not apply them; fix the overlay’s `resources` list or patch target.
Run `kubectl diff -k overlays/dev` to inspect the difference between current cluster state and the proposed resources. This does not apply changes. If it cannot connect or is denied, check kubeconfig context, access rights, and API connectivity.
Only in an approved test environment, run `kubectl apply -k overlays/dev`. Confirm the expected Deployment and Service with `kubectl get`. If the Deployment or Pods are not ready, inspect `kubectl describe`, events, and Pod logs.
Successful local rendering does not mean the cluster will accept the resources or that the application is ready. Unsupported API versions, rejected fields, insufficient permissions, image-pull failures, or capacity limits may appear during server-side apply or runtime. Likewise, a successful apply response alone does not prove application readiness.
If something goes wrong after applying, render the same overlay again and confirm the target context and namespace. To correct an unintended setting, review the corrected overlay and apply it to restore the intended configuration. Deletion may affect data or dependent resources. Consider kubectl delete -k overlays/dev only after inspecting the managed resources in a dedicated test environment. Do not casually delete a broad production overlay.
When to choose Kustomize or Helm
Kustomize fits when you want to keep ordinary Kubernetes manifests and explicitly patch differences between environments. Its advantage is that reviewers can inspect YAML fragments and transformations without learning a separate template language. Helm provides more direct support when you need chart packaging, parameterized values, and a release lifecycle with revisions, state, and rollback workflows.
You do not have to choose only one. Kustomize can also render Helm charts, but with kubectl kustomize you must verify options such as --enable-helm and the environment requirements. Make ownership of the resulting resources clear. If Helm, Kustomize, and a GitOps controller all manage the same Deployment, changes can overwrite one another or create field-ownership conflicts. Define one authoritative write path for each resource.
Consider Kustomize when you want to track shared resources and small environment-specific differences in files. Consider Helm when distributing reusable chart packages and tracking a release lifecycle are central requirements. With either tool, inspect the final manifests, review a diff before applying, and prepare a recovery plan for changes to persistent data.
For a comparison with Helm’s charts and release lifecycle, see Managing Kubernetes charts and releases with Helm.
References
[Declarative management of Kubernetes objects using Kustomize](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/) — official guide to bases, overlays, resources, patches, generators, and `kubectl apply -k`
[`kubectl kustomize` command reference](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_kustomize/) — build behavior and options
[kubectl quick reference](https://kubernetes.io/docs/reference/kubectl/quick-reference/) — apply and diff commands
[Good practices for Kubernetes Secrets](https://kubernetes.io/docs/concepts/security/secrets-good-practices/) — Base64 is not encryption; at-rest encryption and access control require separate configuration