Kustomize Advanced Patterns Kubernetes
Advanced Kustomize patterns for Kubernetes configuration management. Strategic merge patches, JSON patches, components, replacements.
π‘ Quick Answer: Use Kustomize overlays for environment-specific configuration (dev/staging/prod), strategic merge patches for targeted YAML modifications, components for reusable cross-cutting concerns (monitoring sidecar, network policies), and replacements (not vars) for cross-resource references.
The Problem
Managing Kubernetes YAML across multiple environments leads to copy-paste divergence. Dev, staging, and prod have slightly different configs β replicas, resources, image tags β but 90% of the YAML is identical. Kustomize provides structured overlays without templating.
The Solution
Directory Structure
βββ base/
β βββ kustomization.yaml
β βββ deployment.yaml
β βββ service.yaml
β βββ configmap.yaml
βββ components/
β βββ monitoring/
β β βββ kustomization.yaml
β βββ network-policy/
β βββ kustomization.yaml
βββ overlays/
βββ dev/
β βββ kustomization.yaml
βββ staging/
β βββ kustomization.yaml
βββ production/
βββ kustomization.yaml
βββ replicas-patch.yaml
βββ resources-patch.yamlBase
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- configmap.yamlProduction Overlay
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
components:
- ../../components/monitoring
- ../../components/network-policy
namespace: production
namePrefix: prod-
labels:
- pairs:
env: production
images:
- name: registry.example.com/app
newTag: v2.1.0
replicas:
- name: app
count: 5
patches:
- path: resources-patch.yamlStrategic Merge Patch
# overlays/production/resources-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
template:
spec:
containers:
- name: app
resources:
requests:
cpu: "1"
memory: 1Gi
limits:
memory: 2GiComponent (Reusable Cross-Cutting Concern)
# components/monitoring/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
patches:
- patch: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: not-important
spec:
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9090"JSON Patch (Precise Surgery)
# Remove a specific container env var
patches:
- target:
kind: Deployment
name: app
patch: |
- op: remove
path: /spec/template/spec/containers/0/env/2
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: LOG_LEVEL
value: warngraph TD
BASE[Base YAML<br/>Shared config] --> DEV[Dev Overlay<br/>1 replica, debug]
BASE --> STAGING[Staging Overlay<br/>2 replicas, staging tag]
BASE --> PROD[Production Overlay<br/>5 replicas, prod tag]
COMP_MON[Component: Monitoring<br/>Prometheus annotations] --> DEV & STAGING & PROD
COMP_NET[Component: NetworkPolicy<br/>Default deny] --> STAGING & PRODCommon Issues
Patch not applying β field not found
Strategic merge patches match by name in the metadata. Ensure the resource name in the patch matches the base resource exactly.
namePrefix breaks Service selectors
Use labels instead of namePrefix for selector-based matching. Kustomize updates selectors automatically when using the labels transformer.
Best Practices
- Overlays for environments β base + dev/staging/prod overlays
- Components for cross-cutting concerns β monitoring, security, observability
- Strategic merge for additions β add/modify fields naturally
- JSON patches for precision β remove fields, insert at specific positions
replacementsovervarsβ vars are deprecated, replacements are the future
Key Takeaways
- Kustomize provides structured configuration management without templating
- Base + overlays pattern: 90% shared YAML, environment-specific patches
- Components are reusable across overlays β monitoring, network policies, security
- Strategic merge patches for natural YAML modifications; JSON patches for precision
- Built into kubectl (
kubectl apply -k) β no extra tools needed

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
