Sub topic 10
Limitations and cleanup
This chapter assumes you can attach an ephemeral container with kubectl debug, and that you know pods, namespaces, and kubectl get, describe, and delete. It covers what ephemeral containers cannot do and how to remove what a debugging session leaves behind.
What an ephemeral container cannot do
An ephemeral container is a limited kind of container. The API rejects or ignores features that only make sense for long-running workloads.
| Limitation | What it means in practice |
|---|---|
| Cannot be removed or edited | Once added, the entry stays in `spec.ephemeralContainers` until the pod is deleted. |
| Cannot be restarted | The container never restarts. If your shell exits, it is finished. |
| No ports, probes, or resources | You cannot set `ports`, `livenessProbe`, `readinessProbe`, or `resources`. |
| Added only through a subresource | `kubectl edit pod` and `kubectl apply` cannot add one. Only the `ephemeralcontainers` subresource can. |
| Not supported on static pods | Pods managed directly by the kubelet from a manifest file cannot receive ephemeral containers. |
| Names must be unique | A name used in the pod, including by a finished ephemeral container, cannot be reused. |
The missing resources field matters. The debug container shares the pod's resource envelope, but it has no request or limit of its own. A heavy tool such as tcpdump on a busy interface can compete with your application. Pod-level resource accounting is still evolving, so check your cluster's behavior before running heavy tools in production pods.
The --target flag also depends on the container runtime. If the runtime does not support sharing a process namespace, the debug container starts but cannot see the application's processes. Newer kubectl versions print a warning in that case. Check for it, because the failure is easy to miss.
What remains after a session
When you type exit, the shell stops and the ephemeral container terminates. Its definition remains. You can confirm this:
kubectl get pod web-7d4b9c-abcde \
-o jsonpath='{range .status.ephemeralContainerStatuses[*]}{.name}{"\t"}{.state}{"\n"}{end}'The output shows each debug container with a terminated state. The entries are harmless. They use no CPU or memory after exit, but they appear in kubectl describe and in the pod's stored spec.
If you run kubectl debug again with the same pod, kubectl generates a new container name such as debugger-xxxxx. Each session adds another entry. A pod you debug many times accumulates a list of terminated debug containers.
Removing them means recreating the pod. For a pod owned by a Deployment, that is a normal rollout:
kubectl delete pod web-7d4b9c-abcdeThe ReplicaSet creates a replacement without any ephemeral containers. For a pod with no controller, deleting it destroys it for good, so only do this when you can recreate it from its manifest.
Cleaning up copies and node debuggers
The other debugging techniques create real pods. Those pods are not removed automatically.
A pod copy made with --copy-to lives until you delete it:
kubectl delete pod web-debugBy default, the copy does not keep the original's labels, so a Service selector will not send traffic to it. If you add --keep-labels, the copy can receive live requests. Leave that flag off unless you intend it.
A node debugging session creates a pod named node-debugger-<node>-<suffix>. It stays behind after you exit, usually in Completed state. Find and delete it:
kubectl get pods -n default | grep node-debugger
kubectl delete pod node-debugger-worker-1-x7k2pReplace the name with the one from your own output. The node debug pod mounts the node's filesystem at /host, so do not leave one running. Also delete the pod if the session ends abnormally, for example when your terminal disconnects.
Cleanup decision flow
Common errors
- Trying to edit the pod to remove the container. The API refuses changes to
spec.ephemeralContainersthrough a normal update. Delete the pod instead. - Debugging a pod that is about to be replaced. If a Deployment rollout is in progress, the pod you attached to may disappear and take your session with it. Check
kubectl get podsbefore you start. - Forgetting copies. Several
web-debugcopies left in a namespace use real resources and can confuse the next person. Give copies a clear name, and delete them as soon as you are done. - Assuming the container is gone after
exit. It has stopped but is still listed in the pod, as shown above.
Exercise
You debugged a pod from a Deployment three times with kubectl debug. kubectl describe now lists three terminated debug containers, and you want a clean pod. What do you do, and why can't you remove the entries directly?
Show answer
Delete the pod with kubectl delete pod NAME. The Deployment's ReplicaSet creates a replacement with no ephemeral containers. You can't remove the entries directly because ephemeral containers cannot be edited or removed after they are added. They stay in the pod spec until the pod is deleted.
Exercise
After a kubectl debug node/worker-1 session, which resource is left in the cluster, and how do you find and remove it?
Show answer
A pod named node-debugger-worker-1-<suffix> is left behind. List it with kubectl get pods | grep node-debugger and remove it with kubectl delete pod <name>. It mounts the node's filesystem at /host, so it should not be left in the cluster.