Dry Run

Difficulty: Beginner

Question

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?

Answer

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.

Client vs server dry run

Aspect--dry-run=client--dry-run=server
Talks to API serverOnly for discovery, not for the writeYes, full request
Schema and field validationMinimalFull, same as a real request
Defaults filled inNoYes (for example strategy, terminationGracePeriodSeconds)
Admission controllers and webhooksNot runRun, in dry-run mode
Catches RBAC, quota, PSA problemsNoYes
Writes to etcdNoNo

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.

Use case 1: generating YAML fast

--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.

Use case 2: validating changes before they hit production

--dry-run=server answers the question "would the cluster actually accept this?" It catches:

  • Misspelled or unknown fields (with server-side field validation, which is strict by default in recent kubectl)
  • Violations of Pod Security Admission or policy engines like Kyverno and Gatekeeper
  • ResourceQuota or LimitRange problems in the namespace
  • Missing permissions for your user

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.

Gotchas

  • Admission webhooks are called during a server dry run. A webhook must declare 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.
  • Client dry run cannot tell you whether a name already exists or whether a CRD kind is installed correctly; only server dry run can.
  • Helm has its own equivalents: helm template renders locally and helm install --dry-run renders against the cluster without installing.
Where each dry-run mode stops. Client dry run stops before the API server; server dry run runs everything except the write to etcd.
  1. kubectl builds the object: --dry-run=client prints the result here.
  2. Authentication and RBAC: Server dry run checks your permissions.
  3. Mutating admission and defaults: Defaults and webhook mutations are applied.
  4. Validation and validating admission: Schema, PSA, quota and policy checks run.
  5. Persist to etcd: Skipped for --dry-run=server.

Code examples

Generate manifests with client dry run

# 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.

Validate against the live cluster

# 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.

Key points

  • --dry-run accepts none (default), client or server
  • client: local only, great for generating YAML with -o yaml
  • server: full validation, defaulting and admission, nothing persisted
  • kubectl diff uses server-side dry run to show what would change
  • Webhooks must declare sideEffects None or NoneOnDryRun to support dry run

Common follow-up questions

  • What does kubectl diff do under the hood?
  • Why might a manifest pass client dry run but fail on apply?
  • How should an admission webhook handle dry-run requests?
  • What is server-side apply and how is it different from client-side apply?

Interview tips

  • Explain the difference in terms of which stage of the API request each mode reaches
  • Mention the YAML-generation trick; it shows hands-on kubectl experience
  • Bring up kubectl diff in CI as a practical safety measure

Concepts covered

--dry-run=client, --dry-run=server, kubectl diff, Admission Webhooks, YAML Generation

More Kubernetes & Orchestration questions