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
Syntax
kubectl debug POD -it --image=IMAGE [--target=CONTAINER] | node/NAME -it --image=IMAGE
Parameters
| Parameter | Description |
|---|---|
--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
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.
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.
Related
kubectl exec· Run a command in a container of a running pod, for example a shell, a database client or a quick check.kubectl logs· Print a container's logs from a pod, deployment or job; follow them, limit lines, or read the previous crashed container.- Troubleshooting Pods · tutorial