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
Syntax
kubectl diff -f FILE | -k DIR
Parameters
| Parameter | Description |
|---|---|
-f FILE / -k DIR |
Manifests or Kustomize directory to compare. |
--server-side |
Use server-side apply semantics. |
Examples
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.
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.
Related
kubectl apply· Create or update resources from files, directories or Kustomize folders declaratively; the standard way to deploy manifests.- kubectl Essentials · tutorial