Setup docker swarm cluster

Setting up a swarm cluster is really easy. All you need is a minimum of two nodes with OS of your choosing and having docker-ce installed on them both. Since I am mostly using debian I will be following the steps for installing docker-ce for ubuntu/debian since both of them are using aptitude as package manager.

Firstly we need to make sure that no docker was previously installed on the servers. Therefore we do some cleaning first:

# apt-get remove docker docker-engine docker.io containerd runc 

Next step would be to install some packages that are needed to be able to easily install add the repository:

# apt-get update
# apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Now that everything is ready and we got no errors it is time to add the docker repository key and the repository information to the nodes:

# curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
    OK
# add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/debian \
    $(lsb_release -cs) stable"

Usually after adding the repository via add-apt-repository the OS will automatically reload the apt package list therefore the following step is optional:

< # apt-cache update />

# apt-cache update

After the package list was updated it is time to install the docker

# apt-get install -y docker-ce docker-ce-cli containerd.io 

Now that we have docker installed on both of the nodes it is time to set up the docker swarm environment. On the master node you will initialize the swam

# docker swarm init --advertise-addr <IP address of the node> --listen-addr <IP address of the node>:2377

In case your nodes have two network interfaces one public and one private I recommend using the IP address of the private interface. That way your docker swarm will be listening on the private IP addresses only and be protected from attacks from the internet. When the node will be initialized you will be provided with a join command that you will be using on the worker node to join the cluster.

# docker swarm join --token <some very long token> <IP address of the node>:2377

You will have to copy the command generated by the master node and you can paste it into the slave. Once done you can check the cluster by running the following command on the master node:

# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
8aq0aw2rjyfbai5hs9lwkbfeg *   master           Ready               Active              Leader              19.03.8
ah1jftle68d10urvb6omzvclk     worker           Ready               Active                                  19.03.6

Now your swarm cluster is up and ready to take on some awesome projects.