kubectl 1.37 reference · Access and Configuration

kubectl diff Command (kubectl)

Show what kubectl apply would change in the cluster, as a diff against the live objects, before you apply it.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl diff -f FILE | -k DIR

Parameters

ParameterDescription
-f FILE / -k DIR Manifests or Kustomize directory to compare.
--server-side Use server-side apply semantics.

Examples

Preview a change
kubectl create namespace rc-ref-diff > /dev/null
trap 'kubectl delete namespace rc-ref-diff --wait=false > /dev/null' EXIT
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata: {name: app}\ndata: {LOG_LEVEL: info}\n' > cm.yaml
kubectl -n rc-ref-diff apply -f cm.yaml > /dev/null
sed -i 's/info/debug/' cm.yaml
kubectl -n rc-ref-diff diff -f cm.yaml | grep -E '^[-+]  LOG_LEVEL' || true

Output

-  LOG_LEVEL: info
+  LOG_LEVEL: debug

Run this on your computer — see “Get Started” for setup.

Exit code 0 means nothing would change
kubectl create namespace rc-ref-diff2 > /dev/null
trap 'kubectl delete namespace rc-ref-diff2 --wait=false > /dev/null' EXIT
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata: {name: app}\ndata: {LOG_LEVEL: info}\n' > cm.yaml
kubectl -n rc-ref-diff2 apply -f cm.yaml > /dev/null
kubectl -n rc-ref-diff2 diff -f cm.yaml && echo 'no differences (exit code 0)'

Output

no differences (exit code 0)

Run this on your computer — see “Get Started” for setup.

kubectl diff exits with 1 when there are differences, which makes it useful in CI checks for drift.

Was this page helpful?