Create Docker swarm lab with multipass
2024-02-24
Learn how to set up a Docker Swarm lab using Multipass. Ideal for developers and IT professionals alike, this guide provides the essentials to get your Docker Swarm environment up and running on your desktop quickly.
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:
Then you can list the VMs with multipass list
and you should see something like this:
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).
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:
And then run the following command:
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:
Now copy this token and logout from mgr1
node and ssh into wrk1
, wrk2
and wrk3
and run the command you copied.
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.
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.