Skip to content

Kustomize

Manage multi-environment deployments using Kustomize overlays.

Time: ~10 minutes Difficulty: Intermediate

  • Base + overlay pattern for DRY configuration
  • Environment-specific patches (replicas, resources, env vars)
  • Kustomize name prefixes and namespace scoping
  • ConfigMap generation from files

Navigate to the demo directory:

Terminal window
cd demos/kustomize

Deploy the development overlay:

Terminal window
kubectl apply -k overlays/development/

Deploy the production overlay:

Terminal window
kubectl apply -k overlays/production/
Terminal window
# Development
kubectl get pods -n kustomize-dev
kubectl port-forward svc/dev-nginx-service 8081:80 -n kustomize-dev
# Production
kubectl get pods -n kustomize-prod
kubectl port-forward svc/prod-nginx-service 8082:80 -n kustomize-prod

Open http://localhost:8081 for the dev page (orange) and http://localhost:8082 for the prod page (green).

base/ # Shared configuration
deployment.yaml # 3 replicas, health checks, resource limits
service.yaml # ClusterIP on port 80
configmap.yaml # nginx config + landing page
kustomization.yaml # Ties it together
overlays/
development/ # Dev-specific overrides
kustomization.yaml # 2 replicas, dev- prefix, lower resources
deployment-patch.yaml # Smaller CPU/memory limits
ingress.yaml # nginx-dev.local hostname
index.html # Orange-themed landing page
production/ # Prod-specific overrides
kustomization.yaml # 5 replicas, prod- prefix, higher resources
deployment-patch.yaml # Larger CPU/memory limits
ingress.yaml # nginx-prod.local hostname
index.html # Green-themed landing page

Both environments share the same base manifests. Overlays only specify what differs: replica count, resource limits, namespace, and visual theme.

  1. Preview what Kustomize generates without applying:

    Terminal window
    kubectl kustomize overlays/development/
  2. Compare the two environments:

    Terminal window
    diff <(kubectl kustomize overlays/development/) \
    <(kubectl kustomize overlays/production/)
  3. Add a new environment (e.g., staging) by creating a new overlay directory:

    Terminal window
    cp -r overlays/development/ overlays/staging/

    Then edit kustomization.yaml to set a different namespace, prefix, and replica count.

Terminal window
kubectl delete namespace kustomize-dev kustomize-prod

See docs/deep-dive.md for a detailed explanation of the base+overlay model, configMapGenerator internals, strategic merge patches vs JSON patches, and how to build a staging overlay from scratch.

Move on to ArgoCD to automate deployments with GitOps.