The goal is to create a Docker swarm cluster with 3 nodes for under 200 euro’s, but it still has to be capable to run a nginx proxy, couple of static websites, management UI for Docker, home automation and pi-hole. Because the limit is set to 200 euro’s the choice of system was very easy: Raspberry Pi 3 B+. The Raspberry Pi costs around 40 euro’s in the Netherlands so the first 120 euro’s is already gone. The other 80 euro’s we will use for powersupply, sd cards. And I will cheat a bit because I had an unused 5ports ethernet switch laying in a darkcorner.

Requirements

   - 3x Raspberry Pi 3 B+
   - 3x 2.5A micro-usb powersupply
   - 3x 8GB micro sdhc cards
   - 1x 5 ports 10/100/1000Mbit switch
   - 4x 0.5mtr CAT6 UTP patch capable

OS: HypriotOS

To run Docker swarm on a Raspberry Pi we need to have an operating system flashed on the sdhc cards. HypriotOS is a small raspbian jessie image with docker (17.10.0-ce) pre-installed. Another advantage of HypriotOS is that you can do a headless installation. HypriotOS can use cloud-init to preseed some basic configurations like hostname, username, password, installed extra packages and even the settings for your WiFI is you like to use it. This is all done by editing /boot/user-data file.

For more information about HypriotOS visit their website. If you need a manual for the flashing of the sdhc card please visit the getting started page.

The setup

   +-----------+
   | manager01 | ----------
   +-----------+           \
                            \                      
    +----------+           +--------+           +----------+
    | worker01 | --------- | switch | --------- | internet |
    +----------+           +--------+           +----------+
                            /
    +----------+           /
    | worker02 | ----------
    +----------+

Network:
manager01 -> manager01.cluster.haraldvdl.nl -> 192.168.99.100
worker01  -> worker01.cluster.haraldvdl.nl  -> 192.168.99.101
worker02  -> worker02.cluster.haraldvdl.nl  -> 192.168.99.102

Configuration of the manager

Docker swarm first node is by default the manager node. For Docker swarm a minimum of one manager is required. The manager node is also capable of running Docker service. On the other hand, a worker node can’t do manager tasks. You have to promote the worker to a manager node.

# configuration of the manager node.
ssh -l pirate manager01.cluster.haraldvdl.nl
docker swarm init --listen-addr 192.168.99..100 \
  --advertise-addr 192.168.99.100

# if you ever forget the worker token you can find it with
docker swarm join-token worker

Configurattion of the worker

The worker nodes are the best scalable of this type of cluster. Buy a Pi, flash sdhc card, boot Pi and run a command. And your new extra worker node is online.

# Configuration of the worker node
ssh -l pirate worker01.cluster.haraldvdl.nl
docker swarm join --token <join-token> 192.168.99.100:2377

Optional Docker Services

The following steps are not needed, but will give some advantage over the manual way of running docker command for creating stacks an services that will run in docker swarm.

Portainer

Portainer is a docker management UI for managing docker hosts and/or docker swarm clusters. To deploy this services you need to run a single command on the manager node in your docker swarm cluster.

# setting up portainer service for docker swarm
ssh -l pirate manager01.cluster.haraldvdl.nl
docker service create --name portainer --publish 9000:9000 \
  --constraint 'node.role == manager' \
  --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \
  portainer/portainer -H unix:///var/run/docker.sock