Categories
Technology

Bootstrapping Docker Swarm Part 1: Installing Docker and Enabling Docker Swarm

This is part of a multi-part series on getting Docker Swarm up and running. You might want to start with the original post called Bootstrapping Docker Swarm.

Installing Docker

I did my work on Debian 10 “Buster” but for the most part what I’m describing will work on any operating system. That said, this part is specific to Debian based operating systems like Debian or Ubuntu.

To install Docker with the package manager first we add the the Docker repository to all of our systems and then we install the Docker components:

apt-get update && apt-get install software-properties-common apt-transport-https gnupg2 curl ca-certificates -y --no-install-recommends
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
apt-get install docker-ce docker-ce-cli containerd.io docker-compose -y --no-install-recommends

After installation you might want to configure Docker by creating /etc/docker/daemon.json and adding options to it. For example, to change the default docker0 network you could do this:

{
    "bip": "10.60.0.1/24",
    "storage-driver": "overlay2"
}

Enabling Docker Swarm

Initializing Docker Swarm is super easy. We’re going to do it on node01 by running this command:

docker swarm init --default-addr-pool 10.61.0.0/16 --default-addr-pool-mask-length 27

This will initialize Docker Swarm and assign containers networks out of 10.61.0.0/16. Each container that requests a generic network will get a /27 network out of that pool. This should be good enough for most situations.

Running docker swarm init will return a token that we want to then use on the rest of the nodes to join them to the swarm. You can do that by running this command on the rest of the hosts:

docker swarm join --token <TOKEN> 10.0.0.21:2377

Since 10.0.0.21 is the IP address of node01, this should work. We want to promote node02 and node03 to be controllers and we can do that by running this command on node01:

docker node promote node02 node03

Next we want to assign some labels to our hosts to indicate what they do and to help assign containers to them. We are going to run these commands on node01 to assign labels to the rest of the nodes:

docker node update --label-add worker=true node01
docker node update --label-add worker=true node02
docker node update --label-add worker=true node03
docker node update --label-add lb=true lb01
docker node update --label-add lb=true lb02
docker node update --label-add infra=true infra

And finally, when Docker Swarm is initialized a new network bridge is added called docker_gwbridge. It probably has a network that you might not want because it overlaps with maybe another network address that you’re already using. We’re going to replace it right now with a new network address.

docker network disconnect -f docker_gwbridge gateway_ingress-sbox
docker network rm docker_gwbridge
docker network create --subnet=10.60.2.0/24 --gateway=10.60.2.1 -o com.docker.network.bridge.enable_icc=false -o com.docker.network.bridge.name=docker_gwbridge docker_gwbridge

This will disconnect the hidden internal container called gateway_ingress-sbox, remove the bridge, and then create a new bridge with the network on it that we want.

Now, before we can continue, reboot all of the hosts.

Next Steps

In this part we got Docker installed and configured in Swarm mode. It doesn’t run anything yet but we have configured it a bit to match our liking. In the next part we will configure syslog as a container so that you can collect logs from your Swarm in one location.

There are still a lot more steps! Follow on to read the rest of the steps.