kubectl diff and Dry-Run Options
You'll learn how to safely preview configuration changes before they affect your Kubernetes cluster.
What Are Dry-Run and Diff?
Think of these as your "safety net" commands. Before applying changes to your cluster, you can preview exactly what will happen—like reviewing a contract before signing or using spell-check before sending an important email. These tools help you catch mistakes and understand impacts without touching the running system.
kubectl --dry-run
The --dry-run flag simulates applying a change without actually executing it. There are two modes:
--dry-run=client: Validates locally without contacting the API server--dry-run=server: Sends the request to the API server for validation but doesn't persist changes
kubectl apply -f deployment.yaml --dry-run=server
This shows you what would be created or modified, revealing errors before they become problems.
kubectl diff
The kubectl diff command shows you the exact differences between your current cluster state and what you're about to apply:
kubectl diff -f updated-deployment.yaml
Output displays additions (lines starting with +), deletions (-), and unchanged sections—just like code review diffs. This is invaluable when updating existing resources, letting you see precisely which fields will change.
When to Use Each
Use --dry-run=client for quick syntax validation. Use --dry-run=server when you need full API validation. Use diff when modifying existing resources to see the actual changes line-by-line. Combine them for maximum confidence: diff first to review changes, then dry-run to validate.
Key Takeaway: Always preview changes with kubectl diff and --dry-run options before applying configurations—they're your safeguard against unintended cluster modifications.