Skip to main content
Running workloadsLesson 14 of 14

Debugging a Running Pod with kubectl debug

A Pod misbehaves, and its image has no shell. Follow kubectl exec → ephemeral container → Pod copy → node debugging Pod and see what each one can reach, what it needs, and what it leaves behind.

Commandkubectl exec -it checkout -- sh
Resultsh: not found in $PATH
Runs inapp container
Pod checkout · ns shopNode worker-1YoukubectlAPI serverpods/ephemeralcontainersappdistroless · no shelldebuggerephemeral · busyboxcheckout-debugcopy · command shkubeletstarts containersnode-debuggerhost PID · net · IPCHost filesystemmounted at /hostexec shexecsh?
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Why kubectl exec fails on distroless images
  • Ephemeral containers and --target for the process namespace
  • Copying a crashing Pod with --copy-to and a new command
  • Node debugging Pods with the host filesystem at /host
  • RBAC for pods/ephemeralcontainers and what to clean up

Which way to debug​

SituationUseWhat it can seeWhat stays behind
Container runs and has a shellkubectl exec -it <pod> -- shEverything inside that containerNothing
Container runs, image has no shell (distroless)kubectl debug -it <pod> --image=busybox:1.36 --target=<container>Pod network; with --target, the container's processes and /proc/<pid>/rootA Terminated ephemeral container until the Pod is replaced
Container crashes at startupkubectl logs <pod> --previous, then kubectl debug <pod> -it --copy-to=<name> --container=<c> -- shA copy of the Pod with a new command or image (--set-image)The copy Pod, until you delete it
Need to call the app from your laptopkubectl port-forward pod/<pod> 8080:8080One port of the Pod's networkNothing after you stop it
Node problem (disk, network, processes)kubectl debug node/<node> -it --image=ubuntuHost PID, network and IPC namespaces; host files at /hostThe node-debugger Pod, until you delete it
Forbidden ... pods/ephemeralcontainersGrant patch, update on pods/ephemeralcontainers——

Try it yourself​

kubectl run demo --image=registry.k8s.io/pause:3.10 --restart=Never # an image with no shell
kubectl exec -it demo -- sh # fails: no sh in the image
kubectl debug -it demo --image=busybox:1.36 --target=demo # ephemeral container; try: ps
kubectl get pod demo -o jsonpath='{.spec.ephemeralContainers[*].name}{"\n"}' # it stays in the spec
kubectl debug demo -it --copy-to=demo-debug --image=busybox:1.36 --share-processes # copy with a debug container
kubectl debug node/$(kubectl get pod demo -o jsonpath='{.spec.nodeName}') -it --image=ubuntu # try: ls /host
kubectl auth can-i patch pods/ephemeralcontainers # do you have the permission?
kubectl get pods | grep -E 'debug|node-debugger' # leftovers
kubectl delete pod demo demo-debug # clean up (and the node-debugger-... Pod)

Check yourself

  1. kubectl exec -it web -- sh fails with "executable file not found in $PATH". The container is running. What is the best next step?
  2. You finished debugging and want to remove the ephemeral container you added. What happens?
  3. A Pod is in CrashLoopBackOff and its logs do not explain why. Why is kubectl debug --copy-to with -- sh useful here?

Further reading: Debug Running Pods · Ephemeral Containers · Debugging Kubernetes Nodes With Kubectl · Reading status in three layers and the troubleshooting order · Node NotReady and component failures (CKA)