Run a K3S server in a docker container, and connect a K3S agent in another docker container

7/19/2021

I know k3d can do this magically via k3d cluster create myname --token MYTOKEN --agents 1, but I am trying to figure out how to do the most simple version of that 'manually'. I want to create a server something like:

 docker run -e K3S_TOKEN=MYTOKEN rancher/k3s:latest server

And connect an agent something like like:

 docker run -e K3S_TOKEN=MYTOKEN -e K3S_URL=https://localhost:6443 rancher/k3s:latest agent

Does anyone know what ports need to be forwarded here? How can I set this up? Nearly everything I try, the agent complains about port 6444 already in use, even if I disable as much as possible about the server with any combination of --no-deploy servicelb --disable-agent --no-deploy traefik

Feel free to disable literally everything other than the server and the agent, I'm trying to make this ultra ultra simple, but just butting my head against a wall at the moment. Thanks!

-- ricky116
k3d
k3s
kubernetes

1 Answer

7/19/2021

The containers must "see" each other. Docker isolates the networks by default, so "localhost" in your agent container is the agent container itself.

Possible solutions: Run both containers without network isolation using --net=host, map API port of the server to the host with --port and use the host IP in the agent container or use docker-compose.

A working example for docker-compose is described here: https://www.trion.de/news/2019/08/28/kubernetes-in-docker-mit-k3s.html

-- Thomas
Source: StackOverflow