Lab 17 - Kustomize overlays
Lab 17 - Kustomize overlays
Goal: one base, two environments, no copy-paste. Chapter: Kustomize
The base
# 1 -
mkdir -p ~/kust/base ~/kust/overlays/dev ~/kust/overlays/prod
cd ~/kust
kubectl create deploy web --image nginx:1.27 --dry-run=client -o yaml > base/deployment.yaml
kubectl create svc clusterip web --tcp 80:80 --dry-run=client -o yaml > base/service.yaml
cat > base/kustomization.yaml <<'YAML'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
YAML
kubectl kustomize base
kubectl kustomize only renders - nothing is sent to the cluster.
Overlay: dev
# 2 -
cat > overlays/dev/kustomization.yaml <<'YAML'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: dev
namePrefix: dev-
labels:
- pairs: { env: dev }
YAML
kubectl kustomize overlays/dev | grep -E 'name:|namespace:|env:'
Overlay: prod
# 3 -
cat > overlays/prod/kustomization.yaml <<'YAML'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: prod
labels:
- pairs: { env: prod }
images:
- name: nginx
newTag: "1.28"
replicas:
- name: web
count: 3
configMapGenerator:
- name: web-cfg
literals:
- LOG_LEVEL=warn
patches:
- path: resources.yaml
YAML
cat > overlays/prod/resources.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
spec:
containers:
- name: nginx
resources:
requests: { cpu: "50m", memory: "64Mi" }
limits: { memory: "128Mi" }
envFrom:
- configMapRef:
name: web-cfg
YAML
kubectl kustomize overlays/prod
Find in the output: namespace prod, 3 replicas, image nginx:1.28, the
resources from the patch, and a ConfigMap named web-cfg-<hash> - with the
Deployment already pointing at the hashed name.
Apply both
# 4 -
kubectl create ns dev; kubectl create ns prod
kubectl apply -k overlays/dev
kubectl apply -k overlays/prod
kubectl get deploy,svc,cm -n dev
kubectl get deploy,svc,cm -n prod
# 5 - change config -> new hash -> automatic rollout
sed -i 's/LOG_LEVEL=warn/LOG_LEVEL=debug/' overlays/prod/kustomization.yaml
kubectl diff -k overlays/prod | grep -E '^[+-] ' | head
kubectl apply -k overlays/prod
kubectl rollout status deploy/web -n prod
With a plain ConfigMap you would need rollout restart (Lab 12). The hash
suffix changes the Pod template, so the Deployment rolls by itself.
Discovery
InfodiscoverTask A. Add the label team: shop to every object in both
environments by changing one file only.
Task B. Give dev 2 replicas, without touching the base.
Clean up
kubectl delete -k overlays/dev
kubectl delete -k overlays/prod
kubectl delete ns dev prod