Introduction
A basic tutorial already exists on Raspbian France, and we'll necessarily repeat some of it since we're performing the same installations. However, we'll add extra information and, most importantly, our own hands-on experience.
This tutorial is intended for an ARM architecture (Raspberry), but it also applies to a more standard architecture. However, there may be differences that we'll try to note along the way.
You just ordered your 3 Raspberry Pi 3 B boards and you're eager to build a Swarm cluster. Rest assured, it also works with 2 Raspberry Pis, just less fun!
Installing Raspbian (Debian for Raspberry)
First, you'll need to set up your Raspberry Pis. Personally, we use NOOB in the Network version. This means the files will be downloaded directly from the internet to perform the installation. Once the NOOB zip file is downloaded, unzip the received file and copy-paste the files onto your memory card.
- You use an Ethernet cable with direct internet access.
- You use WiFi with direct internet access. NOOB is compatible with both connection methods.
Now, insert the memory card into your Raspberry Pi, plug in the Ethernet cable, keyboard, mouse and screen — if you plan to use WiFi there's nothing special to do — plug your Raspberry Pi into the mains and off you go!
Then follow the graphical interface; if you run into any difficulties during installation, follow this tutorial from Framboise314.fr
Installing Docker
Our architecture:
| Host | IP |
|---|---|
| RaspiManager | 192.168.0.1 / 24 |
| RaspiWorker1 | 192.168.0.2 / 24 |
| RaspiWorker2 | 192.168.0.3 / 24 |
The ports to use if you have a firewall are:
- TCP port 2377 for cluster management and node communication.
- TCP and UDP port 7946 for communication discovery.
- UDP port 4789 for container port traffic between nodes.
Repeat this step 1 on all three of your Raspberry Pis:
----- Step 1 Start:
To install Docker, there's only one script to use, provided by Docker:
curl -sSL https://get.docker.com | sh
Once the script has run successfully, it prompts you to run a command to add Docker permissions to your user:
sudo usermod -aG docker pi
Log out of your user session ("CTRL + D"), then log back in. Check that you have the right permissions
id pi
The group that should have been added is simply called:
Docker
Now you can type:
docker version
You should get:
Client:
Version: 17.11.0-ce
API version: 1.34
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov 20 19:01:07 2017
OS/Arch: linux/arm
Server:
Version: 17.11.0-ce
API version: 1.34 (minimum version 1.12)
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov 20 18:54:12 2017
OS/Arch: linux/arm
Experimental: false
----- Step 1 END:
Docker is now installed; on hub.docker.com you'll find all the official images to set up your first microservices. Make sure to use ARM-architecture images, or you're in for some surprises :)
Installing Swarm
Now you're going to say "finally, here we go." Well, I'll tell you that Swarm is already among us ... Yes indeed, you've just completed the tutorial that installs Swarm, since it's built into Docker.
But one very, very complicated step remains ... initializing Swarm with a single command. Go to your Raspberry Manager, the one that will be the conductor of your Raspberry cluster. Type this command:
docker swarm init
You'll then see:
Swarm initialized: current node (8m5cmwoujfbq9apwlvnr) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token
SWMTKN-1-1taxwfdawf58vyvaafzyyaxasfm5s4tydfrxsjgijtc-esplpc4bu4txguy5phwdkczb1
192.168.0.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager'
and follow the instructions.
Then, as the message says, just copy-paste the command it gives you (careful, don't copy the one shown here, each one is unique):
docker swarm join --token
SWMTKN-1-1taxwfdawf58vyvaafzyyaxasfm5s4tydfrxsjgijtc-esplpc4bu4txguy5phwdkczb1
192.168.0.1:2377
You'll get:
This node joined a swarm as a worker.
Repeat this step on all your workers, then type the following command:
sudo docker node ls
You'll see your machines appear in the cluster.
Now try installing the Visualizer graphical interface to see your containers.
docker service create \
--name=viz \
--publish=8080:8080/tcp \
--constraint=node.role==manager \
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
alexellis2/visualizer-arm
Be careful, the constraint is essential here since this service must run on the manager.
Now open a browser and enter your manager:8080 IP address, so here 192.168.0.1:8080. You'll see your whole cluster with a simple, minimalist graphical interface.
From now on, every time you start a container, it will be placed on one of your workers or the manager depending on availability. If one of the members goes down, the service running on it will move to your new machine. In upcoming tutorials, we'll show you the difficulties we ran into and the best practices we've identified. We'll also write an article on Docker-Compose 3.2.
Warning — bug identified on Raspberry Pi with DOCKER version 17.11.0-ce
When instantiating a service, you may see the following message:
starting container failed: cgroups: memory cgroup not supported on this system: unknown
First, update the firmware on all your Raspberry Pis:
sudo apt update && sudo apt dist-upgrade && rpi-update
Restart your Raspberry Pi; if that doesn't fix the problem, do the following:
Edit this file:sudo vi /boot/cmdline.txt
Before "elevator=deadline" add this:cgroup_enable=memory
Leave only a single space before and after.
Run a Sudo reboot
Everything should now work.
Thanks for reading, see you soon.

