kubectl 1.37 reference · Debugging

kubectl debug Command (kubectl)

Debug pods and nodes with ephemeral containers or copies, even when the image has no shell or tools.

kubectl 1.37

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

Syntax

kubectl debug POD -it --image=IMAGE [--target=CONTAINER] | node/NAME -it --image=IMAGE

Parameters

ParameterDescription
--image IMAGE Image with debugging tools.
--target CONTAINER Share the process namespace of this container.
--copy-to NAME Debug a copy of the pod instead.

Examples

An ephemeral container sharing processes
kubectl create namespace rc-ref-dbg > /dev/null
trap 'kubectl delete namespace rc-ref-dbg --wait=false > /dev/null' EXIT
kubectl -n rc-ref-dbg run app --image=redis:8-alpine > /dev/null
kubectl -n rc-ref-dbg wait --for=condition=Ready pod/app --timeout=120s > /dev/null
kubectl -n rc-ref-dbg debug app --image=busybox:1.37 --target=app --container=dbg -- sh -c 'ps -o args | grep -m1 [r]edis-server' > /dev/null 2>&1
kubectl -n rc-ref-dbg wait --for=jsonpath='{.status.ephemeralContainerStatuses[0].state.terminated.reason}'=Completed pod/app --timeout=120s > /dev/null
echo "process seen from the debug container: $(kubectl -n rc-ref-dbg logs app -c dbg)"

Output

process seen from the debug container: redis-server *:6379

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

Ephemeral containers stay in the pod spec
kubectl create namespace rc-ref-dbg2 > /dev/null
trap 'kubectl delete namespace rc-ref-dbg2 --wait=false > /dev/null' EXIT
kubectl -n rc-ref-dbg2 run app --image=redis:8-alpine > /dev/null
kubectl -n rc-ref-dbg2 wait --for=condition=Ready pod/app --timeout=120s > /dev/null
kubectl -n rc-ref-dbg2 debug app --image=busybox:1.37 --target=app --container=dbg -- true > /dev/null 2>&1
kubectl -n rc-ref-dbg2 get pod app -o jsonpath='ephemeral containers: {.spec.ephemeralContainers[*].name}{"\n"}'

Output

ephemeral containers: dbg

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

Was this page helpful?