Debug K8s in Docker in Earthly
FleetingThe following is the answer I posted to help someone do this, I keep this in here because I think it might be helpful for my future self as well.
Hi.
I understand that you are doing k8s in docker, right?
I maintain a tool called “clk k8s” that is meant to bootstrap a k8s cluster in docker, with all the stuff I generally need, like the ingress controller, certificat manager, reloader etc.
When I need to investigate an issue with the cluster run in docker in earthly, this is what I do. I hope it will help you.
First, find the id of the runc container running in earthly.
$ docker exec ${earthly_cont_name} buildkit-runc list
earthly will run a different runc container per RUN, so wait until you got into the WITH DOCKER before doing that.
In case you have only one earthly job running, you can use the following shortcut
$ runc_cont_id=$(docker exec ${earthly_cont_name} buildkit-runc list -q)
Then, enter that runc container with
$ docker exec -ti ${earthly_cont_name} buildkit-runc exec -t ${runc_cont_id} bash
This entered the container that ran k8s in docker, so from here you can run kubectl to connect to your cluster.
Of course, this can be aliased
$ alias runner="docker exec -ti ${earthly_cont_name} buildkit-runc exec -t ${runc_cont_id}"
So that you can call
$ runner kubectl get pods
Or even better
$ alias iktl="docker exec ${earthly_cont_name} buildkit-runc exec ${runc_cont_id} kubectl"
So that you can call
$ iktl get pods
The good thing with the iktl is that you can use kubectl bash completions with it
$ source <(kubectl completion bash) ; complete -o default -F __start_kubectl iktl
$ iktl get pod [TAB]
An you get the completion on pods.