patternbashkubernetesTip
Kustomize overlays for environment-specific Kubernetes manifests
Viewed 0 times
kustomizeoverlaysbasepatchesgitopskubectl apply -kstrategic merge patchjson patchconfigmapgeneratorsecretgenerator
Problem
Plain YAML manifests need environment-specific differences (namespaces, resource limits, replica counts, image tags). Duplicating YAML files creates maintenance burden and drift.
Solution
Use Kustomize with a base + overlays structure. Each environment overlay only specifies what differs from the base.
k8s/
base/
deployment.yaml
service.yaml
kustomization.yaml
overlays/
staging/
kustomization.yaml # patches + namespace
production/
kustomization.yaml # patches + namespace + replica count# overlays/production/kustomization.yaml
resources:
- ../../base
namespace: production
patches:
- target:
kind: Deployment
name: myapp
patch: |
- op: replace
path: /spec/replicas
value: 5
images:
- name: myapp
newTag: v1.2.3# Apply production overlay
kubectl apply -k k8s/overlays/production
# Preview
kubectl kustomize k8s/overlays/productionWhy
Kustomize is built into kubectl (no plugin needed). It uses a patching approach rather than templating — base manifests remain valid YAML, and overlays apply strategic merge patches or JSON patches on top.
Gotchas
- Kustomize is built into kubectl 1.14+ via
kubectl apply -kandkubectl kustomize - namePrefix and nameSuffix in kustomization.yaml affect all resource names — useful for preventing collisions
- secretGenerator and configMapGenerator create hashed names by default, triggering rolling updates automatically on data changes
- Helm and Kustomize can be combined — use Helm to render a chart, then Kustomize to patch the output
Context
Managing environment-specific Kubernetes configuration without templating
Revisions (0)
No revisions yet.