Sub topic 3
Rendering with kubectl kustomize and applying with kubectl apply -k
This chapter assumes you can read YAML, run kubectl against a cluster, and have a directory with a kustomization.yaml that lists a Deployment and a Service under resources. It covers how to turn that directory into final manifests and send them to the cluster.
What a build does
A build reads kustomization.yaml, loads every listed resource, applies the transformations you declared, and prints one combined YAML stream to standard output. It does not contact the cluster, and it does not change your files. The output is ordinary Kubernetes YAML, so you can read it, save it, or pipe it to other tools.
Two ways to build
kubectl kustomize DIR runs the Kustomize library built into kubectl. The standalone kustomize build DIR command does the same job with its own release cycle.
The built-in copy is often older than the latest standalone release. If a field works with kustomize build but fails or is ignored in kubectl kustomize, a version gap is the likely cause. Run kubectl version and kustomize version to compare.
Render, diff, apply
# Render the production overlay without touching the cluster
kubectl kustomize overlays/production
# Show what would change in the cluster
kubectl diff -k overlays/production
# Apply the rendered resources
kubectl apply -k overlays/productionEach command takes a directory, not a file. Kustomize looks inside that directory for kustomization.yaml, kustomization.yml, or Kustomization.
kubectl apply -k performs the same build and then applies the result. You get the same objects as with kubectl kustomize DIR | kubectl apply -f -, in one step.
kubectl diff -k compares the rendered output with the live objects. It exits with 0 when nothing differs, 1 when differences exist, and a higher code on error. Scripts and CI jobs can use that exit code as a gate.
Reading the output
Check these points in the rendered YAML before you apply it:
- Resources appear in a fixed order, with Namespaces first, whatever order you listed them in.
- If you use a
configMapGenerator, the ConfigMap name ends in a hash suffix, and the Deployment's references to it use the same suffix. - Your input files stay unchanged. Only the output carries the transformations.
Because the output is plain text, you can redirect it with kubectl kustomize overlays/production > rendered.yaml. Saving it lets you review it in a pull request or compare two builds with diff.
Common errors
| Symptom | Likely cause |
|---|---|
| `unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization'` | The path points to the wrong directory, or the file name is misspelled. |
| `accumulating resources ... no such file or directory` | A path under `resources` is wrong. Paths are relative to the kustomization file. |
| Changes not showing up | You ran `kubectl apply -f` instead of `-k`. |
| A field is rejected as unknown | The embedded Kustomize in `kubectl` is older than the one the field needs. |
A safe routine
- Run
kubectl kustomizeon the overlay and read the output. - Run
kubectl diff -kand check that only the expected objects change. - Run
kubectl apply -k.
Exercise
A teammate runs kubectl apply -f overlays/production and reports that the namespace, name prefix, and image tag from the overlay are all missing. What went wrong, and what is the correct command?
Show answer
-f applies the files as they are and never runs a Kustomize build, so no transformations are applied. The correct command is kubectl apply -k overlays/production. They can first check the result with kubectl kustomize overlays/production.
Exercise
Your pipeline must fail when the rendered production overlay differs from the cluster. Which command, and which exit code, does it check?
Show answer
kubectl diff -k overlays/production. It exits with 1 when differences exist, 0 when there are none, and a code above 1 when the command itself fails.