Skip to content

kubectl debug and ephemeral containers — debug running, distroless, and crashlooping pods

Use a kubectl debug ephemeral container to get a shell and tools next to a container that has none — inspect running pods, distroless images, and crashloops without a rebuild.

When a running pod has no shell — a distroless or scratch image, or a hardened container stripped of every tool — you don’t have to rebuild it to look inside. Attach a debug container that shares the target’s process namespace: kubectl debug -it POD --image=busybox --target=CONTAINER -- sh. That drops you into a fresh busybox shell running alongside the broken container, seeing its processes and, through /proc/1/root, its filesystem — with no restart and no rescheduling of the workload.

What you’ll accomplish

  • Get an interactive shell and a full toolbox next to a container that ships with neither.
  • Inspect a running distroless or scratch pod without changing its image.
  • Debug a crashlooping pod by copying it into a modified pod that stays up.
  • Debug a node itself — its filesystem, kubelet, and container runtime — from a pod.
  • List and confirm the ephemeral containers you’ve attached.

Prerequisites

  • Kubernetes 1.25 or newer. Ephemeral containers reached GA in 1.25, so kubectl debug needs no feature gate on a current cluster.
  • The --target flag depends on process-namespace sharing in the container runtime. Both containerd and CRI-O support it; it’s the mechanism that lets the debug container see the target’s PID 1.
  • kubectl configured against the cluster, with RBAC that allows create and patch on the pods/ephemeralcontainers subresource (and get on pods).
  • A debug image with the tools you need. busybox covers basic shell work; nicolaka/netshoot bundles network tooling (curl, dig, tcpdump, ss, iproute2).

Be honest about the limits. An ephemeral container cannot be removed once added — it lives until the pod dies. It carries no resource requests or limits and no probes. And you cannot use it to restart the target container; it observes and pokes, it does not fix the original process in place.

How it works

Diagram — kubectl debug attaches an ephemeral container that shares the target pod's PID namespace, plus the copy-to branch for crashlooping and distroless pods

An ephemeral container is added to an already-running pod through the pods/ephemeralcontainers subresource. The API server patches the pod’s spec, the kubelet on that node starts the new container, and nothing else moves — the pod is not restarted and not rescheduled. The app container keeps running untouched.

With --target=CONTAINER, the debug container joins the target container’s PID namespace. From inside the debug shell you see the target’s processes in ps, and you can reach its root filesystem through /proc/1/root — which is how you inspect a distroless image that has no shell of its own. --profile selects a set of capabilities and settings for the debug container (for example --profile=general or --profile=sysadmin) when the default is too restrictive.

The copy form, kubectl debug POD --copy-to=NEWPOD, doesn’t touch the original. It builds a brand-new pod from the target’s spec, and lets you change it: swap the image with --set-image, override the command so the container stays up instead of crashing, or turn on --share-processes. That copy is what you reach for when the pod is crashlooping — there’s nothing stable to attach to on the original, so you debug a stopped-in-place clone.

The node form, kubectl debug node/NAME, runs a pod in the host’s namespaces with the node’s root filesystem mounted at /host. It’s how you reach a node’s kubelet, logs, and runtime without SSH.

Step-by-step

1. Attach a debug container to a live pod and get a shell. Point --target at the container you want to inspect. Use the container name from the pod spec, not the pod name.

kubectl debug -it web-7fdb6f9b56-2vp8x \
  --image=busybox \
  --target=web \
  -- sh

You’ll see kubectl create the ephemeral container and then an interactive prompt:

Targeting container "web". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
Defaulting debug container name to debugger-7q2xk.
If you don't see a command prompt, try pressing enter.
/ #

Because the debug container shares the target’s PID namespace, the target’s filesystem is reachable at /proc/1/root:

/ # ls /proc/1/root/etc

That lists the target container’s /etc, even though the target itself has no shell.

2. Use netshoot for network debugging. When the problem is DNS, connectivity, or a listening socket, swap busybox for netshoot, which carries the network toolbox:

kubectl debug -it web-7fdb6f9b56-2vp8x \
  --image=nicolaka/netshoot \
  --target=web \
  -- bash

From the shell, the network namespace is shared with the pod, so you test exactly what the app sees:

web-7fdb6f9b56-2vp8x:~# dig +short my-service.default.svc.cluster.local
web-7fdb6f9b56-2vp8x:~# curl -sS -o /dev/null -w '%{http_code}\n' http://localhost:8080/healthz

A resolved IP and a 200 confirm DNS and the local listener both work.

3. Debug a distroless or crashlooping pod by copying it. A crashlooping container never stays up long enough to attach to, so copy the pod and change what breaks it. Override the command to keep the container alive, then exec in:

kubectl debug web-7fdb6f9b56-2vp8x \
  --copy-to=web-debug \
  --container=web \
  --set-image=web=busybox \
  -- sleep 1d

kubectl reports the new pod:

pod/web-debug created

Now the copy runs a busybox that sleeps instead of the crashing entrypoint. Exec into it to inspect config, mounts, and environment at rest:

kubectl exec -it web-debug -- sh -c 'env | sort; ls -l /app'

For a distroless image you want to keep but can’t shell into, copy with --share-processes and add a debug container instead of replacing the image, so the original process runs next to a shell:

kubectl debug web-7fdb6f9b56-2vp8x \
  --copy-to=web-debug \
  --image=busybox \
  --share-processes \
  -- sleep 1d

4. Debug a node. Run a privileged pod in the node’s namespaces and chroot into its filesystem:

kubectl debug node/ip-10-0-1-23.ec2.internal -it --image=busybox

The node’s root is mounted at /host:

/ # chroot /host
# journalctl -u kubelet --no-pager | tail -20

You’re now running commands as if on the node, reading the kubelet’s journal.

5. List the ephemeral containers you’ve added. They show up under ephemeralContainerStatuses, not the normal container list:

kubectl get pod web-7fdb6f9b56-2vp8x \
  -o jsonpath='{range .status.ephemeralContainerStatuses[*]}{.name}{"\t"}{.state}{"\n"}{end}'

Expect a line per debug container with its state:

debugger-7q2xk	{"running":{"startedAt":"2026-07-26T14:03:11Z"}}

Validate it works

Three checks confirm the debug container is doing what you think.

Confirm it’s actually running — the status should show running, not waiting or terminated:

kubectl get pod web-7fdb6f9b56-2vp8x \
  -o jsonpath='{.status.ephemeralContainerStatuses[0].state}'
{"running":{"startedAt":"2026-07-26T14:03:11Z"}}

Confirm --target took effect — from inside the debug shell you should see the target’s PID 1, not just your own shell:

/ # ps -o pid,comm
  PID COMMAND
    1 web
   14 sh

Confirm the target’s filesystem is visible through its PID 1:

/ # cat /proc/1/root/etc/hostname

Seeing the target’s process as PID 1 and reading its files through /proc/1/root together prove the namespace sharing is real.

Troubleshooting

  • --target has no effect — you don’t see the target’s processes. kubectl warns about exactly this on attach. It means the container runtime doesn’t support the process-namespace sharing that --target needs, or the cluster is old. On a supported runtime (containerd, CRI-O) and Kubernetes 1.25+ it works; on an older or unusual CRI, fall back to --copy-to with --share-processes.
  • ephemeral containers are disabled or the subresource is unknown. This appears only on clusters older than 1.25 where the feature isn’t enabled. The fix is to upgrade; there’s no per-pod workaround on a cluster that lacks the API.
  • The shell command fails immediately. You pointed --image at the app’s own image (which has no shell) instead of a debug image. Use busybox or nicolaka/netshoot, not the container you’re debugging.
  • forbidden: User cannot create resource "pods/ephemeralcontainers". RBAC is blocking you. You need create and patch on pods/ephemeralcontainers in the namespace, granted through a Role or ClusterRole — separate from ordinary pods access.
  • The debug container never starts because the pod is Pending. There’s nothing to attach to — the pod isn’t scheduled yet, so this isn’t a debug-container problem at all. Fix the scheduling first; see pod Pending / unschedulable.

When this is the wrong approach

Ephemeral containers are for the moment your existing signals ran out, not the first move. Reach for logs, metrics, and traces first — proper observability answers most questions without anyone attaching to a live pod, and it keeps working when you’re asleep. Many production environments forbid interactive exec by policy for exactly that reason; respect it rather than route around it. If the pod won’t schedule at all, no debug container helps — that’s a scheduling problem, covered in the linked article. And if you find yourself attaching the same tools to the same pod repeatedly, you don’t want an ephemeral container, you want a real sidecar that’s part of the deployment.

For the broader diagnostic path — get, describe, logs, exec — before you reach ephemeral containers, see kubectl debugging. The authoritative reference is the Kubernetes guide on debugging a running pod.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.