Difficulty: Beginner
How do you test a kubectl command or manifest without actually creating or changing the resource, and what is the difference between client and server dry run?
Use the --dry-run flag. --dry-run=client only builds the object locally and prints what would be sent, while --dry-run=server sends the request to the API server, which runs full validation, defaulting and admission (including webhooks) but does not persist anything to etcd. Combine either with -o yaml to see the resulting object.
| Aspect | --dry-run=client | --dry-run=server |
|---|---|---|
| Talks to API server | Only for discovery, not for the write | Yes, full request |
| Schema and field validation | Minimal | Full, same as a real request |
| Defaults filled in | No | Yes (for example strategy, terminationGracePeriodSeconds) |
| Admission controllers and webhooks | Not run | Run, in dry-run mode |
| Catches RBAC, quota, PSA problems | No | Yes |
| Writes to etcd | No | No |
The third value, --dry-run=none, is the default and means a normal request. The old boolean form (--dry-run or --dry-run=true) is deprecated; always pass an explicit value.
--dry-run=client -o yaml is the quickest way to scaffold a manifest. Instead of writing a Deployment from memory, you let kubectl create generate it, save it to a file, and edit. This is a well-known time saver in the CKA and CKAD exams and in day-to-day work.
--dry-run=server answers the question "would the cluster actually accept this?" It catches:
kubectl diff -f file.yaml builds on server-side dry run: it computes what the object would look like after applying and shows a diff against the live object. Running kubectl diff in a CI pipeline before kubectl apply is a cheap safety net.
sideEffects: None or NoneOnDryRun in its configuration, otherwise the dry-run request is rejected. A well-written webhook checks the dryRun field and skips external side effects, such as creating a DNS record.helm template renders locally and helm install --dry-run renders against the cluster without installing.# Deployment skeleton
kubectl create deployment web --image=nginx:1.27 --replicas=3 \
--dry-run=client -o yaml > web-deploy.yaml
# Service for it
kubectl expose deployment web --port=80 --target-port=80 \
--dry-run=client -o yaml > web-svc.yaml
# Secret without putting the value in a file by hand
kubectl create secret generic db-creds \
--from-literal=password='s3cr3t' --dry-run=client -o yaml
kubectl create generators plus client dry run give you correct YAML in seconds, which you then edit and commit.
# Would the API server accept this, including webhooks and PSA?
kubectl apply -f web-deploy.yaml --dry-run=server
# What exactly would change compared to the live object?
kubectl diff -f web-deploy.yaml
# exit code 0 = no diff, 1 = diff found, >1 = error
Server dry run and kubectl diff both go through the real API path without persisting anything.
--dry-run=client, --dry-run=server, kubectl diff, Admission Webhooks, YAML Generation