Debug Running Kubernetes Pods with Ephemeral Containers: A Step‑by‑Step Guide
This article explains how to debug live Kubernetes pods using temporary (ephemeral) containers that share namespaces, covering cluster setup, creating workloads, diagnosing network problems, tracing processes, and accessing node shells without restarting the original pods.
Debugging a running container or pod is not as straightforward as debugging a regular process; this guide shows how to use temporary (ephemeral) containers that share the target pod's namespaces to debug business container processes.
Directly exec‑ing into a problematic pod is simple but has drawbacks: the pod may lack debugging tools, additional permissions require a pod restart, and adding tools to the image or raising container privileges introduces security risks.
Using Ephemeral Debug Containers
When kubectl exec is insufficient—e.g., the container has crashed, uses a distroless image, or lacks required privileges—ephemeral containers provide an interactive troubleshooting method without restarting the pod.
https://kubernetes.io/docs/concepts/workloads/pods/ephemeral-containers/
Ephemeral containers are added to an existing pod with a custom image and share the target container’s resources, including Linux network and process namespaces, shared volumes, and node access.
Creating a Kind Cluster
First, set up a Kubernetes 1.23 cluster using kind create cluster. Verify the cluster is running and accessible.
All operations are performed from the main kind node via docker exec -it <node> bash.
Creating a Simple Workload
Deploy a single‑replica Nginx deployment:
kubectl create deployment nginx --image=nginxDiagnosing Network Issues
Network debugging requires sharing the network namespace, which is automatically shared when an ephemeral container is attached.
Create the first ephemeral container using the nicolaka/netshoot image, which includes tools like tcpdump and strace:
kubectl debug -it pod-name --image=nicolaka/netshoot --command -- /bin/shVerify both containers share the same Linux namespace:
systemd-cgls -u kubelet-kubepods-besteffort.sliceCapture network packets from the Nginx container: tcpdump -n port 80 Send a request to the pod and observe the packets printed in the ephemeral container’s terminal.
curl http://pod-ip-addressTracing Processes with Ephemeral Containers
To trace processes, both containers must share the same process namespace and the ephemeral container needs the SYS_PTRACE capability.
Create the container with the --target flag:
kubectl debug -it <pod-name> --image=nicolaka/netshoot --target <container-name> -- bashBecause the container lacks SYS_PTRACE, we add the capability via a PATCH request to the Kubernetes API:
curl -v -XPATCH -H "Content-Type: application/json-patch+json" \
'http://127.0.0.1:8001/api/v1/namespaces/default/pods/nginx-8f458dc5b-wkvq4/ephemeralcontainers' \
--data-binary @- <<EOF
[{
"op": "add", "path": "/spec/ephemeralContainers/-",
"value": {
"command":[ "/bin/sh" ],
"stdin": true, "tty": true,
"image": "nicolaka/netshoot",
"name": "debug-strace",
"securityContext": {"capabilities": {"add": ["SYS_PTRACE"]}},
"targetContainerName": "nginx"
}
}]
EOFNow strace can be run inside the ephemeral container to trace Nginx processes.
Debugging Nodes via Ephemeral Containers
If SSH access to a node is unavailable, an ephemeral container can be used to debug the node:
kubectl debug node/<node-name> -it --image=<image-name>The container runs in the host’s IPC, network, and PID namespaces, and the node’s root filesystem is mounted at /host. To make the container’s root filesystem identical to the node’s, execute chroot /host inside the container.
Source: https://juejin.cn/post/7151657396967276580
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
