Sub topic 12
Previewing and validating changes
This chapter assumes you can write a Deployment manifest, know how labels and selectors connect objects, and have kubectl configured against a cluster. It covers four tools that let you check a change before it reaches running Pods: kubectl explain, server-side dry run, kubectl diff, and a final status check.
Why preview first
kubectl apply changes live objects at once. A wrong field name, a bad indentation level, or an unintended image tag can trigger a rollout that replaces working Pods. Previewing catches most of these mistakes while the change is still only a file on your machine.
The safe workflow
The commands below run in the order you would use them. They assume deployment.yaml holds the web Deployment in the demo namespace.
kubectl explain deployment.spec.strategy
kubectl apply -f deployment.yaml --dry-run=server
kubectl diff -f deployment.yaml
kubectl apply -f deployment.yaml
kubectl get deployment web -n demoLook up fields with kubectl explain
kubectl explain prints the schema of a resource from the cluster's own API documentation. Use a dotted path to go deeper:
kubectl explain deployment.spec.strategy
kubectl explain deployment.spec.template.spec.containers --recursiveThe output shows each field's type and description. Because the data comes from your cluster, it matches the API version that cluster serves. Check it when you are unsure of a field name or whether a value is a string, a number, or a list.
Validate with a server-side dry run
--dry-run=server sends the manifest to the API server. The server runs the same checks as a real request: schema validation, defaulting, and admission controllers. It then discards the result instead of saving it.
kubectl apply -f deployment.yaml --dry-run=serverA successful run prints deployment.apps/web configured (server dry run). A failure prints the same error a real apply would.
--dry-run=client also exists. It only checks the file locally and builds the object without asking the server, so it misses admission and many schema errors. Prefer server when you have cluster access.
See the difference with kubectl diff
kubectl diff compares your file with the live object and prints a unified diff. It sends the manifest through a server-side dry run, so defaults and admission changes appear in the comparison.
kubectl diff -f deployment.yamlLines starting with - are in the cluster now. Lines starting with + would be added. The exit code is 0 for no difference, 1 for differences, and above 1 for an error. That makes diff usable as a check in scripts.
Check the result
After applying, confirm the outcome. kubectl get deployment web -n demo shows how many replicas are ready. For a rollout in progress, kubectl rollout status deployment/web -n demo waits until it finishes or fails.
What previews cannot catch
A dry run proves the API server accepts the object. It does not prove the app works. An image tag that does not exist passes validation and fails later with ImagePullBackOff. A selector that matches no Pods is also valid. Verify these after applying.
| Check | Catches | Misses |
|---|---|---|
| `kubectl explain` | Wrong field names or types, before writing | Anything about live state |
| `--dry-run=client` | Malformed YAML, local schema errors | Admission and server rules |
| `--dry-run=server` | Schema, defaults, admission rejections | Missing images, failing probes |
| `kubectl diff` | Unintended edits to live objects | Runtime behavior |
Exercise
You edited deployment.yaml and want to know exactly which fields would change in the cluster, without modifying anything. Which command do you run, and how can a script tell whether changes exist?
Show answer
Run kubectl diff -f deployment.yaml. It changes nothing. Its exit code is 0 when the live object matches the file and 1 when there are differences, so a script can branch on the exit code.
Exercise
Why is --dry-run=server a better check than --dry-run=client before applying to a shared cluster?
Show answer
The server run goes through the API server's schema validation, defaulting, and admission controllers, so it reports the same rejections a real apply would. The client run only checks the file locally and misses those.