Kubernetes Labs · Lab 5 of 9
Environments with Kustomize: One Base, a Dev and a Prod Overlay
Turn copy-pasted manifests into a Kustomize base plus dev and prod overlays that change only what differs: image tag, replicas, resources, configuration and a disruption budget. Render, diff and apply both, then roll out a config change through a hashed ConfigMap.
- Environment
- Kubernetes
- Level
- Intermediate
- Time
- About 40 min
- Cost
- Free
You will build
A shop-k8s repository layout that deploys the same app to shop-dev and shop-prod with different settings,
ready to be driven by CI or GitOps.
Before you start
Real apps run in several environments. Copying the YAML per environment works until the copies drift apart.
Kustomize, built into kubectl, keeps one base with the shared manifests and a small overlay per
environment with only the differences. kubectl kustomize renders the result; kubectl apply -k applies it. Nothing
is templated: overlays are patches on plain YAML, so what you review is what runs.
Helm is the other common tool. It packages apps as charts with templates and values, which suits software you distribute to others. Kustomize suits your own apps. Many teams use both: Helm for third-party charts, Kustomize for their services.
Every output below was captured from a real run on kind 0.33 with Kubernetes 1.37. Pod names, IP addresses, ages and cluster ports are generated, so yours will differ; everything else should match.
Project structure
shop-k8s/
├── base/
│ ├── kustomization.yaml # resources + ConfigMap generator
│ ├── deployment.yaml
│ └── service.yaml
└── overlays/
├── dev/
│ ├── kustomization.yaml # namespace shop-dev, image v1.12.0, debug logging
│ └── namespace.yaml
└── prod/
├── kustomization.yaml # namespace shop-prod, image v1.11.0, 3 replicas, resources patch, PDB
├── namespace.yaml
├── resources.yaml
└── pdb.yaml
Steps
-
1The base
The base has no namespace, no image tag and one replica: those belong to environments. The
configMapGeneratorbuildsweb-configfrom literals and appends a hash of its content to the name.base/kustomization.yaml and base/deployment.yaml apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization labels: - pairs: app.kubernetes.io/name: web includeSelectors: false resources: - deployment.yaml - service.yaml configMapGenerator: - name: web-config literals: - LOG_LEVEL=info - FEATURE_SEARCH=true --- # base/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 1 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: whoami image: traefik/whoami ports: - name: http containerPort: 80 envFrom: - configMapRef: name: web-config readinessProbe: httpGet: path: /health port: http resources: requests: cpu: 50m memory: 32Mi limits: memory: 64Mi -
2The dev overlay
Dev runs one replica of the newer version with debug logging.
behavior: mergechanges one key of the base ConfigMap and keeps the rest. The finished overlay also turnsFEATURE_SEARCHoff; a later step shows that change rolling out.overlays/dev/kustomization.yaml apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namespace: shop-dev labels: - pairs: environment: dev includeSelectors: false resources: - namespace.yaml - ../../base images: - name: traefik/whoami newTag: v1.12.0 # dev tries the new version first configMapGenerator: - name: web-config behavior: merge literals: - LOG_LEVEL=debug - FEATURE_SEARCH=false -
3The prod overlay
Prod pins the version that passed dev, runs three replicas, raises resources with a patch and adds a PodDisruptionBudget that only prod needs.
overlays/prod/kustomization.yaml and resources.yaml apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namespace: shop-prod labels: - pairs: environment: prod includeSelectors: false resources: - namespace.yaml - ../../base - pdb.yaml images: - name: traefik/whoami newTag: v1.11.0 # prod stays on the version that passed dev replicas: - name: web count: 3 patches: - path: resources.yaml --- # overlays/prod/resources.yaml apiVersion: apps/v1 kind: Deployment metadata: name: web spec: template: spec: containers: - name: whoami resources: requests: cpu: 100m memory: 64Mi limits: memory: 128Mi -
4Render the dev overlay
Rendering never touches the cluster, so you can run it in CI and review the exact output in a pull request. Note the namespace, the label, the hashed ConfigMap name and the image tag filled in.
Run cd shop-k8s kubectl kustomize overlays/devExpected output
apiVersion: v1 kind: Namespace metadata: labels: environment: dev name: shop-dev --- apiVersion: v1 data: FEATURE_SEARCH: "true" LOG_LEVEL: debug kind: ConfigMap metadata: labels: app.kubernetes.io/name: web environment: dev name: web-config-78bmbgdh4g namespace: shop-dev --- ... apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: web environment: dev name: web namespace: shop-dev spec: replicas: 1 ... - envFrom: - configMapRef: name: web-config-78bmbgdh4g image: traefik/whoami:v1.12.0 ...Hint
This output is from before the
FEATURE_SEARCH=falseline was added; with the finished file the ConfigMap hash differs. -
5Diff dev against prod
Everything that differs between environments, and nothing else:
Run diff <(kubectl kustomize overlays/dev) <(kubectl kustomize overlays/prod)Expected output
5,6c5,6 < environment: dev < name: shop-dev --- > environment: prod > name: shop-prod 11c11 < LOG_LEVEL: debug --- > LOG_LEVEL: info ... 45c45 < replicas: 1 --- > replicas: 3 57,58c57,58 < name: web-config-78bmbgdh4g < image: traefik/whoami:v1.12.0 --- > name: web-config-56gb47c595 > image: traefik/whoami:v1.11.0 ... > minAvailable: 2 > selector: > matchLabels: > app: web
-
6Apply both environments
Run kubectl apply -k overlays/dev kubectl apply -k overlays/prod kubectl -n shop-prod rollout status deployment/web kubectl get deployments -A -l app.kubernetes.io/name=web -o custom-columns=NAMESPACE:.metadata.namespace,READY:.status.readyReplicas,IMAGE:.spec.template.spec.containers[0].imageExpected output
namespace/shop-dev created configmap/web-config-78bmbgdh4g created service/web created deployment.apps/web created namespace/shop-prod created configmap/web-config-56gb47c595 created service/web created deployment.apps/web created poddisruptionbudget.policy/web created ... deployment "web" successfully rolled out NAMESPACE READY IMAGE shop-dev 1 traefik/whoami:v1.12.0 shop-prod 3 traefik/whoami:v1.11.0
Check:Two namespaces, two versions, from one base.
-
7Change dev's configuration and roll it out
Add
- FEATURE_SEARCH=falseunderliteralsinoverlays/dev/kustomization.yaml(as in the finished file). The content changes, so the hash and the ConfigMap's name change, so the Deployment's pod template changes, so Kubernetes does a normal rolling update.kubectl diffshows it before you apply.Why Kustomize puts a hash in ConfigMap names Run kubectl diff -k overlays/dev | grep -E '^[-+] ' | grep -v generation kubectl apply -k overlays/dev kubectl -n shop-dev rollout status deployment/web kubectl -n shop-dev get configmapsExpected output
- name: web-config-78bmbgdh4g + name: web-config-h869868h5c + FEATURE_SEARCH: "false" + LOG_LEVEL: debug + creationTimestamp: "2026-11-26T19:31:18Z" + labels: + app.kubernetes.io/name: web + environment: dev + name: web-config-h869868h5c + namespace: shop-dev + uid: 09943a63-cbb7-409c-b280-329e346756b2 namespace/shop-dev unchanged configmap/web-config-h869868h5c created service/web unchanged deployment.apps/web configured Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination... deployment "web" successfully rolled out NAME DATA AGE kube-root-ca.crt 1 2s web-config-78bmbgdh4g 2 2s web-config-h869868h5c 2 1s
Check:A new ConfigMap and a rollout; the old ConfigMap is still there until something prunes it.
-
8Clean up
Run kubectl delete -k overlays/dev kubectl delete -k overlays/prodExpected output
namespace "shop-dev" deleted configmap "web-config-h869868h5c" deleted from shop-dev namespace service "web" deleted from shop-dev namespace deployment.apps "web" deleted from shop-dev namespace namespace "shop-prod" deleted configmap "web-config-56gb47c595" deleted from shop-prod namespace service "web" deleted from shop-prod namespace deployment.apps "web" deleted from shop-prod namespace poddisruptionbudget.policy "web" deleted from shop-prod namespace
In production
The layout is production-ready; what changes is who applies it and where the environments live.
What changes
| In this lab | In production |
|---|---|
| Both environments in one cluster, separated by namespace | Prod in its own cluster (often its own cloud account), dev and staging elsewhere |
kubectl apply -k from your laptop |
CI renders and validates on every pull request; Argo CD or Flux applies (lab 9) |
| Image tags edited by hand | CI updates the tag (kustomize edit set image) in a commit after building and scanning the image, ideally pinned by digest |
| Old ConfigMaps left behind | GitOps pruning removes what is no longer rendered |
| Two overlays | dev → staging → prod, promoted by merging the same change through each overlay |
Rules that keep overlays small
- The base must work on its own (
kubectl kustomize baserenders cleanly). - Overlays change values, not structure: image, replicas, resources, config, extra policies.
- If two overlays repeat the same patch, it belongs in the base or in a shared component.
- Never put secrets in a
configMapGeneratoror asecretGeneratorcommitted to Git.
Production checklist
- One base plus one overlay per environment, all in Git
- CI runs
kubectl kustomizeand a schema/policy check (kubeconform, Kyverno or Conftest) on every change - Changes are reviewed as rendered diffs, not only as overlay edits
- Prod pins image versions that already ran in lower environments
- Config changes produce new hashed ConfigMaps and normal rollouts
- A GitOps controller applies and prunes, so the cluster matches Git exactly
Learn the concepts
- Packaging with Helm and Kustomize · Kubernetes Tutorial
- Namespaces, Labels and Selectors · Kubernetes Tutorial
- Helm, Kustomize and GitOps with Argo CD · Course
Preparing for the CKA or CKS exam? Practice tests and mock exams →