Sub topic 11
Previewing and diffing changes before applying
This chapter assumes you can write a kustomization.yaml, you know the base and overlay layout, and you have kubectl access to a cluster. A Kustomize overlay can change many objects at once. Before you run kubectl apply -k, check what it will do.
Three commands, three questions
Each command answers a different question.
| Command | Question it answers | Touches the cluster? |
|---|---|---|
| `kubectl kustomize DIR` | What YAML does this overlay produce? | No |
| `kubectl diff -k DIR` | How does that YAML differ from what is running? | Reads only |
| `kubectl apply -k DIR` | Make the cluster match the YAML. | Yes, writes |
# 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/productionStep 1: read the rendered output
kubectl kustomize overlays/production prints every object after all transformers, patches and generators have run. Nothing is sent to the cluster. Read it for the things you meant to change:
- The
namespaceisproductionon every namespaced object. - Names carry the
prod-prefix. - The Deployment has
replicas: 5and the image tag1.2.3. - The container has the resource requests and limits from your patch.
- The ConfigMap name ends in a hash suffix, and the Deployment refers to that exact name.
To search for one object, pipe the output through a filter:
kubectl kustomize overlays/production | grep -n -A3 "kind: Deployment"If a patch did not apply, it is easy to see here. The field is simply missing from the output.
Step 2: diff against the cluster
kubectl diff -k overlays/production sends the rendered objects to the API server as a server-side dry run. It then prints a unified diff of each object against its live version. Lines starting with - are in the cluster now. Lines starting with + will be there after the apply.
Pay attention to these results:
- A new object shows up as a diff with only
+lines. - A changed hash suffix on a generated ConfigMap shows a new ConfigMap and a changed
configMapRefin the Deployment. This is expected and triggers a rollout. - An empty diff means the cluster already matches.
kubectl diff exits with code 0 when there is no difference and 1 when there is one. Exit codes above 1 mean the command itself failed. This lets you use it in a script or a CI job.
kubectl diff -k overlays/production > /tmp/prod.diff
status=$?
if [ "$status" -gt 1 ]; then
echo "diff failed" >&2
exit "$status"
fiCommon errors
- Diffing the wrong context.
kubectl diffcompares against whatever cluster your current context points to. Check it withkubectl config current-contextfirst. - Reading the base instead of the overlay.
kubectl kustomize baseshows the base alone. Pass the overlay directory to see the final result. - Missing namespace.
kubectl diffcan fail if the target namespace does not exist yet. Include a Namespace resource in your overlay or create it first. - Skipping the diff for generated Secrets. The diff output can reveal secret values. Do not paste it into a ticket or a CI log without checking.
Exercise
You change replicas in overlays/production from 5 to 3 and run kubectl diff -k overlays/production. The command prints a diff and exits with code 1. Did something go wrong?
Show answer
No. Exit code 1 means the diff found differences, which is expected here, because the live Deployment has 5 replicas and the rendered one has 3. Only exit codes above 1 indicate that the command failed. Nothing has been applied yet.
Exercise
Which command would you use to check that your JSON 6902 patch matched the Deployment, without contacting the cluster?
Show answer
kubectl kustomize overlays/production. It renders the final YAML locally, so you can confirm the patched field appears with the new value.