How to manage Kubernetes manifests across environments with Kustomize
Kustomize customizes manifests with a base plus overlays and no templating. Patch replicas, set name prefixes and namespaces per environment, generate hashed ConfigMaps, then apply with kubectl apply -k.
Managing manifests with Kustomize
Kustomize customizes raw Kubernetes manifests through a base plus overlays model, with no templating language. A base holds common resources; each overlay patches the base for a specific environment. Kustomize is built into kubectl, so no extra tooling is required.
Prerequisites
- Existing Kubernetes YAML manifests.
- kubectl (which bundles kustomize) installed.
Steps
1. Create a base
Place shared manifests in base/, for example deployment.yaml and service.yaml.
2. Add a kustomization file
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
3. Create environment overlays
# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: prod-
namespace: prod
resources:
- ../../base
4. Patch resources per environment
Increase replicas only in prod with a strategic merge patch:
patches:
- target:
kind: Deployment
name: myapp
patch: |
- op: replace
path: /spec/replicas
value: 5
5. Generate ConfigMaps
Generate config with a content hash so pods roll on change:
configMapGenerator:
- name: app-config
literals:
- LOG_LEVEL=info
6. Build and apply
kubectl kustomize overlays/prod
kubectl apply -k overlays/prod
kubectl kustomize prints the rendered output for review before applying.
Verification
Run kubectl kustomize overlays/prod and confirm the name prefix, namespace, and replica count reflect the overlay. Apply and verify with kubectl get deploy -n prod that the rendered objects exist as expected. A dev overlay should produce different values from the same base.
Next Steps
Use the same base across many overlays, add images transformers to set tags per environment, and feed Kustomize output into Argo CD so GitOps drives every environment from one base.
Prerequisites
- Existing Kubernetes manifests
- kubectl with built in kustomize
Steps
- 1Create a base
- 2Add a kustomization file
- 3Create environment overlays
- 4Patch resources per environment
- 5Generate ConfigMaps
- 6Build and apply