k3s in dockerd's namespaces
This enables a convenient development setup where images built locally with docker build
are available to the kubernetes cluster without needing to push the image to a registry from where kubernetes can pull it.
The version of Docker Desktop for macOS includes a kubernetes cluster that already does the above. This script enables similar on Linux hosts, and also works in macOS. More accurately, the nsenter
hack is necessary for running this on macOS and also happens to work on Linux hosts (where a simpler version without the hacks could work).
Tested on
- macOS 10.14
- Ubuntu 18.04 (failed with docker installed via snap; worked with docker installed from docker.com's packages)
- NixOS (if using firewall, requires allowing all traffic from
cni0
network)
- A privileged docker container is started in the host's pid namespace
- That namepsace is scanned for 'dockerd'
- The namspaces of that dockerd are entered, and a shell is executed
- Those namespaces are assumed to include commands: sh, env, and k3s (via docker volume)
$ ./kidn
...
$ kubectl --kubeconfig ./kubeconfig.yaml get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-b7464766c-76ll7 1/1 Running 0 20m
kube-system helm-install-traefik-dthwg 0/1 Completed 0 20m
kube-system svclb-traefik-4856w 2/2 Running 0 9m5s
kube-system traefik-56688c4464-zkwbh 1/1 Running 0 9m5s
$ docker build -t localimage .
$ kubectl --kubeconfig ./kubeconfig.yaml run test --restart=Never --image=localimage --image-pull-policy=Never
pod/test created
$ kubectl --kubeconfig ./kubeconfig.yaml logs test
it worked
On macOS, where the underlying Linux VM is normally not accessible, we can use the nsenter
hack to run vpnkit
directly, connecting port 80 on the mac to port 80 on the Linux VM (where svclb has directed traffic to traefik). This is the same technique used when exposing ports via docker run --publish...
.
$ ./kidn nsenter vpnkit-expose-port -proto tcp -host-ip 127.0.0.1 -host-port 80 -container-ip 127.0.0.1 -container-port 80