Sub topic 11
Choosing the right technique
This chapter assumes you have worked through the earlier chapters. You should know kubectl exec and kubectl logs. You should also know how kubectl debug adds an ephemeral container, makes a pod copy with --copy-to, and opens a node session. The goal here is to pick the least invasive technique that can answer your question.
Start with the cheapest tools
Most failures can be narrowed down before you touch the pod. Run these first, because they change nothing:
kubectl get pod web-7d4b9c-abcde -o wide
kubectl describe pod web-7d4b9c-abcde
kubectl logs web-7d4b9c-abcde -c app --previousdescribe shows events such as failed image pulls, failed probes, and OOM kills. logs --previous shows output from the last crashed container. If these explain the failure, you do not need a debug session.
The four techniques
| Technique | Use it when | Effect on the pod |
|---|---|---|
| `kubectl exec` | The image has a shell and the tools you need, and the container is running | None |
| Ephemeral container | The container is running but the image has no shell or tools | Adds a container to the live pod. It cannot be removed. |
| Pod copy (`--copy-to`) | The container crashes, or you must change its image or command | Creates a second pod. The original is untouched. |
| Node debugging | The cause looks node-level, such as kubelet, disk, or runtime problems | Starts a new pod on the node. The workload pod is untouched. |
Decision flow
The diagram follows the questions to ask in order.
Worked scenarios
A distroless service returns errors
The pod is running, but exec fails with "executable file not found". The failure only happens in the live pod, so you want to observe it there. Use an ephemeral container:
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=appInside, ps aux and ls /proc/1/root/ let you inspect the application's processes and files.
A pod is in CrashLoopBackOff
The container exits before you can attach. An ephemeral container can still be added, but the process you want to inspect is gone each time. Make a copy and replace the command with a shell:
kubectl debug web-7d4b9c-abcde -it --copy-to=web-debug --container=app -- shFrom that shell you can run the original start command by hand and read the error. Delete the copy when you finish:
kubectl delete pod web-debugSeveral pods on one node fail together
If many unrelated pods on the same node fail, the node is the likely cause. Start a node session and look at its logs:
kubectl debug node/worker-1 -it --image=busybox:1.36
ls /host/var/logA network problem needs privileged tools
Packet capture needs extra capabilities. Choose the profile that grants only what you need:
kubectl debug -it web-7d4b9c-abcde --image=nicolaka/netshoot --target=app --profile=netadminCommon mistakes
- Reaching for a copy first. A copy may not reproduce the failure. It gets a new name and IP, it is not behind the same Service, and it loses state held in the original. Prefer an ephemeral container when the live pod is the evidence.
- Debugging a copy in production traffic paths. A copy that keeps the original's labels can match a Service selector and receive real requests. Check the labels before you start.
- Forgetting that ephemeral containers stay. They remain in the pod spec until the pod is deleted. Note this in your incident log.
- Using a broad profile by default. Pick the narrowest profile that works, and confirm your RBAC allows
pods/ephemeralcontainers. - Skipping the read-only steps. Events and previous logs solve many cases in seconds.
Summary
Use exec when the image has what you need. Use an ephemeral container when the image is minimal but the container runs. Use a copy when the container crashes or you must change it. Use node debugging when the evidence is on the node.
Exercise
A pod runs a distroless image and returns wrong results for one request type. It is not crashing. You want to inspect its live processes without restarting it. Which technique do you choose, and which flag lets you see the application's processes?
Show answer
Use an ephemeral container with kubectl debug -it POD --image=busybox:1.36 --target=app. The --target flag shares the application container's process namespace (when the runtime supports it), so ps aux shows its processes and /proc/<pid>/root shows its filesystem.
Exercise
A pod is in CrashLoopBackOff because the app exits at startup with no useful log line. Which technique do you choose, and why is an ephemeral container a poor fit?
Show answer
Use a pod copy, for example kubectl debug POD -it --copy-to=web-debug --container=app -- sh. This replaces the command with a shell so the container stays up and you can run the start command by hand. An ephemeral container is a poor fit because the target process keeps exiting, so there is nothing stable to inspect. The copy also leaves the original pod unchanged.