Kubernetes · Chapter 25 of 28
Kustomize
By the end of this chapter you can keep one set of plain manifests as a base, build staging and production from it with overlays, change names, labels, images and replica counts without editing the base, generate a ConfigMap whose change rolls the pods, patch any field, and see exactly what an overlay builds before you apply it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Packaging and operating
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
From November, every change to Jhatpat's catalog goes to staging first, where Bhola's team tries it on two test dark stores, and only then to production. The two differ in five things: the namespace, the number of pods (2 in staging, 4 in production on Dhanteras week), the image tag (staging tries the new one first), the CPU and memory, and a few lines of the catalog's settings file.
With plain manifests, that is two copies of every file, and the copies drift: in October Tushar fixed a readiness probe in the staging copy and forgot production. The chart from chapter 24 solves the copying, but only for the fields its author turned into values: when production needed a memory limit, someone had to edit the template first, and every template is YAML wrapped in {{ }} that you have to render to read. Ammar wants the manifests to stay plain YAML that anyone can read and kubectl apply, with each environment's differences written down next to them.
Kustomize does that, and it is built into kubectl: a base of plain manifests, and overlays that change them on the way to the cluster.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
overlays/production: 4 replicas and catalog 2.0
Build production from the base, without changing the base.
1. Open overlays/production/kustomization.yaml. It starts from ../../base.
2. Put every object in the namespace ch25-production.
3. Add the label env=production to every object. It must reach the selectors and the pod templates too.
4. Give the Deployment catalog 4 replicas, and the image jhatpat/catalog the tag 2.0.
5. Apply the folder with kubectl apply -k overlays/production.
To pass: you applied the overlay with kubectl apply -k, and the command worked. In ch25-production, the Deployment catalog and the Service catalog have env=production, in their selectors too. 4 catalog pods of 2.0 are ready. base/ stays as it is: catalog 1.2, 2 replicas, no resources.
- Task 2
overlays/production: a configMapGenerator that rolls the pods
Make the ConfigMap with a generator, and see a change roll out new pods.
1. In the production kustomization, add a configMapGenerator named catalog-config.
2. Make it from the file catalog.yaml in that folder (key catalog.yaml). Apply the overlay.
3. Add the line festival: dhanteras to overlays/production/catalog.yaml.
4. Apply the overlay again.
To pass: ch25-production has two generated catalog-config-<hash> ConfigMaps: one without the festival line and one with it. The Deployment catalog mounts the one with it, and all its pods that use it are ready. The order matters: apply once without the line, then again with it.
- Task 3
overlays/production/resources.yaml: a patch for resources
Give catalog its resources in production only, with a patch.
1. Complete overlays/production/resources.yaml. It changes the container catalog of the Deployment catalog.
2. The container requests cpu 250m and memory 256Mi, and has a memory limit of 512Mi.
3. List resources.yaml under patches in the production kustomization.
4. Apply the overlay.
To pass: the overlay builds catalog with those resources, the Deployment in ch25-production has them, and its pods with them are ready. base/ still has no resources.
- Task 4 · interview question
Interview: Helm or Kustomize for Jhatpat's environments
Interview question: for Jhatpat's staging and production, would you use Helm or Kustomize, and why?
1. Ammar will ask: "Staging and production differ in five fields. Helm chart or Kustomize overlays, and why?"
2. Write your answer in two or three sentences of your own. Name both tools and pick one.
3. Save it in answer.txt, below the notes.
To pass: answer.txt names both Helm and Kustomize, says what Kustomize does, and says what Helm does. Lines that start with # do not count.
What you will learn
- A base and a kustomization.yaml: the list of files that belong together.
- Overlays: namespace, namePrefix, labels, images and replicas.
- kubectl apply -k, kubectl kustomize, kustomize build and kubectl diff -k.
- configMapGenerator: a ConfigMap with a hash in its name, and why that rolls the pods.
- Patches: change any field of a base object from the overlay.
More in Packaging and operating
Build "Kustomize" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.