On the newest JavaLand convention, I attended a chat by my good buddy Matthias Häussler. Within the speak, he made me conscious of the kubectl debug
command, launched in Kubernetes 1.25:
Ephemeral containers are helpful for interactive troubleshooting when
kubectl exec
is inadequate as a result of a container has crashed or a container picture doesn’t embrace debugging utilities, reminiscent of with distroless photos.You should utilize the
kubectl
debug command so as to add ephemeral containers to a working Pod.
Let’s see the way it works by working a Distroless container:
kubectl run node --image=gcr.io/distroless/nodejs18-debian11:newest --command -- /nodejs/bin/node -e "whereas(true) { console.log('whats up') }"
The container begins an infinite NodeJS loop. We will examine the logs with the anticipated outcomes:
Think about that we have to examine what is going on contained in the container.
kubectl exec -it node -- sh
As a result of the container has no shell, the next error occurs:
OCI runtime exec failed: exec failed: unable to start out container course of: exec: "sh": executable file not present in $PATH: unknown command terminated with exit code 126
We will use use kubectl debug
magic to attain it anyway:
kubectl debug -it
--image=bash (1)
--target=node (2)
node (3)
1 | Picture to connect. As we would like a shell, we’re utilizing bash |
2 | Identify of the container to connect to |
3 | For some purpose I don’t perceive, we should repeat it |
The result’s exactly what we anticipate:
Focusing on container "node". If you happen to do not see processes from this container it might be as a result of the container runtime does not help this characteristic. Defaulting debug container identify to debugger-tkkdf. If you happen to do not see a command immediate, strive urgent enter. bash-5.2#
We will now use the shell to sort no matter command we would like:
The end result confirms that we “share” the identical container:
PID USER TIME COMMAND 1 root 12:18 /nodejs/bin/node -e whereas(true) { console.log('whats up') } 27 root 0:00 bash 33 root 0:00 ps
After we end the session, we will reattach it to the container by following the directions:
bash-5.2# Session ended, the ephemeral container is not going to be restarted however could also be reattached utilizing 'kubectl connect node -c debugger-tkkdf -i -t' whether it is nonetheless working