It's 3 a.m. The pod is crash-looping. You run kubectl exec -it my-app -- sh and get back OCI runtime exec failed: exec: "sh": executable file not found in $PATH. You try curl localhost:8080/health, and the container just stares back at you. Welcome to distroless.
If you read the last article and switched your images to distroless, you probably felt pretty smug about it. Smaller image, fewer CVEs, no package manager for attackers to play with. Then the first production incident arrived, and you realized your debugging toolbox was also smaller. Congratulations, you traded convenience for security, and now you have to be cleverer about getting into the box.
The good news is that you rarely need to get inside the container. You need to get into its namespaces, and Kubernetes gives you a few ways to do that.
1. The Right Tool: kubectl debug with an Ephemeral Container
An ephemeral container is a temporary container you attach to a running pod. It doesn't restart the pod, it doesn't touch your main container spec, and it stops when you exit. Ephemeral containers became stable in Kubernetes 1.25, so on any reasonably modern cluster you can use them without feature flags.
The trick that makes this useful is --target:
kubectl debug -it my-pod --image=busybox:1.36 --target=app
Without --target, your debug container gets its own process namespace, and you can't see what your app is doing. With --target=app, it shares the process namespace of the app container. That means you can run ps aux and see your application's processes. You can also look at /proc/<pid>/fd to see open file descriptors, or read the environment with /proc/<pid>/environ.
I'll be honest: that last one made me a little uncomfortable the first time I tried it. Environment variables are where secrets tend to live. Anyone who can run kubectl debug on that pod can read them, so lock down who has that permission through RBAC.
Also, process namespace sharing depends on your container runtime, so if --target doesn't behave the way you expect, check that first.
2. Need Network Tools? Bring the Swiss Army Knife
Busybox is fine for poking at processes, but when the question is "why can't this pod reach the database," you want real networking tools. That's what nicolaka/netshoot is for. It's a container packed with curl, dig, nslookup, tcpdump, ss, ip, and iperf.
kubectl debug -it my-pod --image=nicolaka/netshoot --target=app
Once you're inside, the usual checks become easy:
-
curl -v http://localhost:8080/healthto see whether the app responds at all -
dig my-service.my-namespace.svc.cluster.localto check DNS -
ss -tlnpto see what's actually listening -
tcpdump -i any port 5432when you want to watch packets with your own eyes
Because the debug container shares the pod's network namespace, these commands see exactly what your app sees. That's the whole point.
For Docker outside Kubernetes, you can do the same trick by attaching to the network namespace of a running container:
docker run -it --rm --net container:my-app nicolaka/netshoot
Quick tip for production: netshoot's tags move around, so pin a digest when you can. The project's own docs recommend nicolaka/netshoot@sha256:... for debug sessions. A reproducible tool beats a surprise at 3 a.m.
3. Last Resort: Go Straight to the Node
Sometimes the problem isn't the pod. It's the node, the container runtime, or something that only makes sense from the host's point of view. For that, you can debug the node itself:
kubectl debug node/my-node-name -it --image=nicolaka/netshoot
This starts a privileged pod that can see the node's namespaces. From there, you can use nsenter to step into the namespaces of any container running on that node. You first need the container's PID, which you can get from the node's container runtime tooling (for example, crictl ps and crictl inspect). Then you enter its network namespace:
nsenter -t <pid> -n ss -tlnp
I'd treat this as the nuclear option. A privileged pod with host-level access is exactly the kind of thing security teams lose sleep over. Use it when you have to, make sure someone is watching, and delete the debug pod when you're done. Don't leave it running "just in case," because it won't stay small or polite.
4. A Quick Decision Guide
-
Need to see processes, files, or env vars? Use
kubectl debug --targetwithbusybox. -
Need to test networking or DNS? Use
kubectl debug --targetwithnetshoot. -
Need host-level visibility or the container is unreachable? Debug the node and use
nsenter. -
Running plain Docker? Use
docker run --net container:<name>withnetshoot.
Keep the Image Small
Distroless didn't take away your debugging. It just moved it outside the container, where it belongs. Ephemeral containers give you a temporary, targeted view into a running pod, netshoot gives you the network tools you're used to, and node-level nsenter covers the cases where none of that is enough.
And if you're wondering what to do about that missing curl in your production image, the answer is: keep it out. Let the debug container bring it along when you need it, and leave the image as small as the first article told you to.
Top comments (0)