DEV Community

Cover image for 🎯 Scenario #10 β€” Use kubectl diff to Preview Changes Before Applying in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

🎯 Scenario #10 β€” Use kubectl diff to Preview Changes Before Applying in Kubernetes

This scenario teaches you how to see what will change in the cluster BEFORE applying YAML, similar to Git diff but for Kubernetes.

This is extremely useful in real DevOps workflows, especially CI/CD pipelines.


βœ… Step 1 β€” Install kubectl diff plugin (only if missing)

Most cloud environments (GKE, EKS, AKS, Cloud Shell) already have it.

Check:

kubectl diff --help
Enter fullscreen mode Exit fullscreen mode

If it shows help output, you're good.


πŸ—οΈ Step 2 β€” Create Initial Deployment

Create file:

# diff-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: diff-nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: diff-nginx
  template:
    metadata:
      labels:
        app: diff-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f diff-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Verify:

kubectl get deploy diff-nginx
kubectl get pods -l app=diff-nginx
Enter fullscreen mode Exit fullscreen mode

1


✏️ Step 3 β€” Modify the YAML (but DO NOT apply yet)

Edit the same file diff-deploy.yaml and change:

  • replicas: 2 β†’ 3
  • image: nginx:1.21 β†’ nginx:1.25

Updated file:

spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
Enter fullscreen mode Exit fullscreen mode

Save it.


πŸ” Step 4 β€” Preview Changes Using kubectl diff

Run:

kubectl diff -f diff-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

You will see something like:

--- /tmp/LIVE-#####
+++ /tmp/MERGED-#####
@@ -1,6 +1,6 @@
   replicas: 2
+  replicas: 3
...
- image: nginx:1.21
+ image: nginx:1.25
Enter fullscreen mode Exit fullscreen mode

3

This means:

  • Kubernetes will update replicas from 2 β†’ 3
  • It will update image from 1.21 β†’ 1.25

β›” No changes are applied yet β€” only preview.


πŸš€ Step 5 β€” Apply the Changes

Once satisfied:

kubectl apply -f diff-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Check rollout:

kubectl rollout status deployment/diff-nginx
Enter fullscreen mode Exit fullscreen mode

Check pods:

kubectl get pods -l app=diff-nginx -o wide
Enter fullscreen mode Exit fullscreen mode

4


πŸ§ͺ Step 6 β€” Modify Again and Test More Changes

Let’s try one more change for deeper practice.

Edit again:

replicas: 3 β†’ 1
Enter fullscreen mode Exit fullscreen mode

Preview:

kubectl diff -f diff-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

You will see:

replicas: 3
β†’ replicas: 1
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f diff-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

5


🌟 Thanks for reading! If this post added value, a like ❀️, follow, or share would encourage me to keep creating more content.


β€” Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | πŸ” Security | ⚑ Automation
πŸ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)