Use it to inspect a running pod's network, files and processes.
Debugging with kubectl exec and Ephemeral Containers
kubectl exec runs a command in a running container; kubectl debug adds an ephemeral debug container (with tools) to a pod or creates a debug copy, even for distroless images.
1. kubectl exec -it <pod> -c <c> -- sh.
2. No shell → kubectl debug -it <pod> --image=nicolaka/netshoot --target=<c>.
3. Network: curl, nslookup, ss from the debug container.
4. Node debugging: kubectl debug node/<n> -it --image=busybox.
5. Clean up debug pods.
exec/debug → container shares the pod's namespaces → tools run
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Use read-only commands first: ss, ip addr, cat config files, env. Confirm you are in the right container with hostname. Prefer copies (kubectl debug --copy-to) when testing changes.
Step 2 Common failures Narrow the fault
Distroless images have no shell, so exec fails. Changes made through exec are lost on restart and make the Pod differ from its spec. RBAC may allow get pods but not pods/exec.
Distroless images have no shell — use kubectl debug -it <pod> --image=busybox --target=<container>. Exec access is powerful — restrict with RBAC.
kubectl exec -it <pod> -c <c> -- sh— first check for this topickubectl get pods -o wide— pods, status, node, IPkubectl describe pod <pod>— events and conditionskubectl logs <pod> [-c <c>] [--previous]— container logs (incl. previous crash)kubectl get events --sort-by=.lastTimestamp— recent cluster events
ReferenceWhat it is1 section · depth on demand
How it works
exec starts a new process in the container's namespaces through the kubelet and runtime. kubectl debug adds an ephemeral container to the Pod spec; with --target it joins the process namespace so you can see the application's processes.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Choosing the right debugging method
| Situation | Method |
|---|---|
| Image has a shell and tools | kubectl exec -it <pod> -c <container> -- sh |
| Distroless or minimal image | kubectl debug -it <pod> --image=busybox:1.36 --target=<container> |
| Container crashes before you can attach | kubectl debug <pod> -it --copy-to=<pod>-dbg --container=<c> -- sh |
| Problem is on the node (network, disk, kubelet) | kubectl debug node/<node> -it --image=busybox:1.36 |
Ephemeral containers
An ephemeral container is added to a running Pod. With --target it joins the target container's process namespace, so ps shows the application and /proc/<pid>/root exposes its filesystem. It shares the Pod's network namespace, so ss -ltn, nslookup and curl localhost:<port> test exactly what the app sees. Ephemeral containers cannot be removed until the Pod is deleted and have no ports, probes or resource guarantees.
Worked network check from inside a distroless Pod
kubectl debug -it api-6d… --image=nicolaka/netshoot --target=api -- bash
ss -ltnp # is the app listening, and on which address?
nslookup db.shop # DNS from the Pod's resolver
nc -vz db.shop 5432 # TCP reachability through NetworkPolicy
ls /proc/1/root/etc/app # application files, read-only
Node debugging
kubectl debug node/… runs a privileged-style Pod in the host's namespaces with the host filesystem at /host; chroot /host gives a host shell. Delete the debug Pod afterwards (kubectl delete pod node-debugger-…).
RBAC needs pods/exec for exec and pods/ephemeralcontainers (update) for kubectl debug; grant them deliberately, since both give code execution in the Pod.
Common mistakes
- Running debug tools in production images.
ReferenceConfiguration & state1 section · depth on demand
Commands
kubectl exec -it web-7d9f -c web -- sh
kubectl debug -it web-7d9f --image=nicolaka/netshoot --target=web
kubectl debug node/worker1 -it --image=busybox # node shell (host at /host)
kubectl cp web-7d9f:/app/log.txt ./log.txt
Documented example, not captured output. Ephemeral containers help with distroless images that have no shell; --target shares the process namespace. Access to exec/debug should be restricted by RBAC — it is effectively shell access.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.