I create a standard nginx pod and I want to run a sidecar container, but it's not working for me in Kubernetes 1.18 - 1.20.
# kubectl alpha debug nginx --image=busybox --target=nginx
Defaulting debug container name to debugger-6wlb5.
# kubectl attach nginx -c debugger-6wlb5
If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container debugger-6wlb5 not found in pod nginx_default
I have gates feature enabled in apiserver, scheduler & controller-manager. Describe on pod doesn't show errors or something:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4jhz2 (ro)
Ephemeral Containers:
debugger-6wlb5:
Image: busybox
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Conditions:
Type Status
What do I miss here?
Background
Unfortunately this is still
alfa featureand it's not suitable for production. As stated in Ephemeral Containers documentation.Tests
I have tested this on
Kubeadmwith Kubernetes 1.19. Feature Gates flag was set in config files likeapiserver,schedulerandcontroller-manager---feature-gates=EphemeralContainers=true. Alsokubeletwas modified. As this isalphait needs specific steps to make it work.Option 1
This method is described in Debug Running Pods - Debugging with an ephemeral debug container.
Describe pod
In example above,
attachwas made by using-iflag. It's stated in Copying a Pod while adding a new container.Side Note
There second part of quotation is that
If your session becomes disconnected you can reattach using kubectl attach.however it applies to another method, notEphemeral Containers.Option 2
This option and all prerequisite are described in Ephemeral Containers — the future of Kubernetes workload debugging article. Instead of
DeploymentI have usednginxpod.$ kubectl run nginx --image=nginx.Next you have to create
.jsonfile with below configuration. Please changemetadata.nameto your pod name.IMPORTANT
You have to apply it using
kubectl replace --rawOtherwise, you will get error:
After applying proper command:
Use
kubectl describe <podname>to verify ifEphemeralContainerwas created.Using this method you will be able to use
kubectl attachConclusion
Ephemeral Containeris a special type of container that runs temporarily in an existing Pod to accomplish user-initiated actions such as troubleshooting. One you will terminate session, you will not be able to connect again.You will get error:
It might be changed in the future but based on my tests, you can connect to this container only once.