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 feature
and it's not suitable for production. As stated in Ephemeral Containers documentation.Tests
I have tested this on
Kubeadm
with Kubernetes 1.19. Feature Gates flag was set in config files likeapiserver
,scheduler
andcontroller-manager
---feature-gates=EphemeralContainers=true
. Alsokubelet
was modified. As this isalpha
it 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,
attach
was made by using-i
flag. 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
Deployment
I have usednginx
pod.$ kubectl run nginx --image=nginx
.Next you have to create
.json
file with below configuration. Please changemetadata.name
to your pod name.IMPORTANT
You have to apply it using
kubectl replace --raw
Otherwise, you will get error:
After applying proper command:
Use
kubectl describe <podname>
to verify ifEphemeralContainer
was created.Using this method you will be able to use
kubectl attach
Conclusion
Ephemeral Container
is 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.