Pcornel
Browse topics
Options
Recent
Favorites
Containers · Technical reference

Debugging with kubectl exec and Ephemeral Containers

Direct answer

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.

When to use this

Use it to inspect a running pod's network, files and processes.

First 30 seconds

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.

Operational verification

exec/debug → container shares the pod's namespaces → tools run

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Docker / containerd / Podman / Kubernetes / OCI-compatible platforms
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformDocker / containerd / Podman / Kubernetes / OCI-compatible platforms
Scope / prerequisiteUse it to inspect a running pod's network, files and processes.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Distroless images have no shell — use kubectl debug -it <pod> --image=busybox --target=<container>. Exec access is powerful — restrict with RBAC.

Key checks
  • kubectl exec -it <pod> -c <c> -- sh — first check for this topic
  • kubectl get pods -o wide — pods, status, node, IP
  • kubectl describe pod <pod> — events and conditions
  • kubectl 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

SituationMethod
Image has a shell and toolskubectl exec -it <pod> -c <container> -- sh
Distroless or minimal imagekubectl debug -it <pod> --image=busybox:1.36 --target=<container>
Container crashes before you can attachkubectl 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
debugging with kubectl exec and ephemeral containerskubectl execkubectl debug ephemeral container
Confirmation

Confirm action

Continue?