Create Docker swarm lab with multipass
24.2.2024

Installation
First follow this link to install multipass on your machine.
Create 6 VMs with multipass
Out of this 6 VMs, 3 will be the manager nodes and 3 will be the worker nodes.
The reason 3 of them will be manager is because of high avalability (HA). This means that one or more manager nodes can fail and the swarm will still be operational.
Here is how to create the VMs:
# Create the manager nodes
multipass launch docker --name mgr1
multipass launch docker --name mgr2
multipass launch docker --name mgr3
# create the worker nodes
multipass launch docker --name wrk1
multipass launch docker --name wrk2
multipass launch docker --name wrk3
Then you can list the VMs with multipass list
and you should see something like this:
multipass list
By default a proxmox container is running on these docker VMs, so we need to stop it and delete it (you don’t have to do it, but i find it unnecessary).
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
Initialize the swarm
Now that we have our VMs up and running, we can initialize the swarm. First we need to ssh into the manager nodes and run the following command:
multipass shell mgr1
And then run the following command:
docker swarm init
Important note: for production environments you should use the
--advertise-addr
and--listen-addr
flag to specify the address of the manager node. but while we are in a lab environment, we can skip this step.
When you run docker swarm init
you will get a token that you can use to join the worker nodes to the swarm. It will look something like this:
docker swarm join --token SWMTKN-1-07ctjicfr8zhvekjf7oub7z2tlf2jrt960z0lj7z58am3cfpvf-1io4zpjffv5ispnlw5oppkb07 10.236.189.100:2377
Now copy this token and logout from mgr1
node and ssh into wrk1
, wrk2
and wrk3
and run the command you copied.
docker swarm join --token SWMTKN-1-07ctjicfr8zhvekjf7oub7z2tlf2jrt960z0lj7z58am3cfpvf-1io4zpjffv5ispnlw5oppkb07 10.236.189.100:2377
Now if you go back to mgr1
and run docker node ls
you should see all the nodes in the swarm.
Note: the
LEADER
column indicates which node is the leader of the swarm. Also note that * on ID column indicates that on which node you are currently executing command from.
Now while you are still in mgr1
run the following command to get token for joining the other manager nodes.
docker swarm join-token manager
You will get a result like this:
Now copy the command and run it on mgr2
and mgr3
and you should see all the manager nodes in the swarm.
Conclusion
Basically after this you can run any docker swarm command and it will be executed on all the nodes in the swarm. This is a very basic setup and there are many more things you can do with docker swarm like creating services, scaling services, etc. But this is a good starting point to get your hands dirty with docker swarm.