Sub topic 6
Debugging a copy of the pod with --copy-to
This chapter assumes you know pods, containers and namespaces, and can use kubectl get, describe, logs and exec. You should also have read the earlier chapters on ephemeral containers and basic kubectl debug use. A cluster running Kubernetes 1.25 or later is recommended.
Why debug a copy
An ephemeral container is added to the live pod. That is not always what you want. Three cases come up often:
- The application container is crash-looping, so there is no running process to attach to or inspect.
- You want to change the container's image or command to test a theory, but you must not disturb production traffic.
- Ephemeral containers cannot be changed or removed once added, so you want a throwaway place to experiment.
kubectl debug --copy-to=NAME creates a new pod that is a copy of the original. You can change the copy freely. The original pod keeps running and keeps serving traffic.
How a copy is created
The copy is a plain pod, built from the original pod's spec. It has the new name you give it. It is not managed by the original's Deployment or ReplicaSet, so nothing will recreate it if it is deleted. Labels are copied by default, which has a side effect covered in the gotchas below.
Replacing the command of the application container
The most common use is to start the application container with a shell instead of its normal entrypoint. This works on a crash-looping pod because the shell keeps the container alive.
kubectl debug web-7d4b9c-abcde -it --copy-to=web-debug --container=app -- shHere --container=app selects which container to change. Everything after -- replaces that container's command. Inside the shell you can start the application by hand, read its configuration files and watch it fail. This only helps if the image contains a shell. For a distroless image, change the image as shown in the next section, or add a second container.
Changing the image
You can swap in a different image for a container in the copy. A typical choice is the debug variant of the same image, or a build with extra tooling:
kubectl debug web-7d4b9c-abcde -it --copy-to=web-debug --container=app --image=myregistry/web:debug -- shIf you pass --image together with --container, the named container in the copy gets that image. If the container name does not exist in the pod, kubectl debug adds a new container instead.
Adding a debug container with a shared process namespace
If the application image must stay as it is, add a debug container next to it and let the containers see each other's processes:
kubectl debug web-7d4b9c-abcde -it --copy-to=web-debug --share-processes --image=busybox:1.36--share-processes turns on process namespace sharing for the copy. All containers in the copy can then see each other's processes. From the debug shell, ps aux lists the application's processes, and /proc/<pid>/root exposes its filesystem. In a regular pod you usually cannot do this, because sharing is off by default. With a copy you can enable it without touching the original.
Cleaning up
A copy is a normal pod, and it does not go away when you exit the shell. Delete it when you finish:
kubectl get pods
kubectl delete pod web-debugYou can also pass --attach=false or leave the session and return later. To remove the copy automatically, check your kubectl version's help for --keep-annotations and related flags, and delete the pod yourself if unsure.
Gotchas
- Labels can route traffic to the copy. If the copy keeps the original's labels, a Service selector may match it, and real users may reach a pod you are modifying. Use
--keep-labels=false, which is the default behaviour in recent kubectl versions, or check withkubectl get pod web-debug --show-labels. - Probes can restart your experiment. If you replace the command with
sh, the original liveness probe may fail and kill the container. Probes are removed from the copy by default, but verify this withkubectl get pod web-debug -o yaml. - A copy may not reproduce the problem. Bugs that depend on load, node state or timing may not appear in the copy. It may also land on a different node, which matters if the fault is node-specific.
- Resources count. The copy uses cluster CPU and memory, and it counts against quotas in the namespace.
Summary of the flags
| Flag | Effect on the copy |
|---|---|
| `--copy-to=NAME` | Creates a new pod called NAME from the original |
| `--container=NAME` | Selects the container to modify |
| `--image=IMAGE` | Sets the image, or adds a debug container |
| `--share-processes` | Enables a shared process namespace |
| `-- CMD` | Replaces the command of the selected container |
Exercise
Your pod api-5f6c-xyz12 is in CrashLoopBackOff and its image has a shell. You want to start the application manually and watch it fail, without touching the original pod. Which command do you run, and what must you do afterwards?
Show answer
Run kubectl debug api-5f6c-xyz12 -it --copy-to=api-debug --container=app -- sh, replacing app with the real container name. The shell keeps the container alive, so you can start the application by hand. Afterwards, delete the copy with kubectl delete pod api-debug, because it is not removed automatically.
Exercise
Why might a debug copy receive production traffic, and how do you check for it?
Show answer
The copy can keep the original pod's labels, so a Service selector may match it. Run kubectl get pod web-debug --show-labels and compare the labels with the Service's selector. Remove or change the matching labels if they overlap.