Docker Raspberry PI Cluster

This Blog Post was originally published on the platform "writelier" (formerly "co-writers" and "200wordsaday"). Sadly the blogging platform was discontinued. I downloaded all my blog posts and prepared them to be republish them here.

As I'm currently on vacation, it is time to finish up some projects at home. Cleaning the Apartment, finishing the Terrarium, building a Raspberry PI Cluster.

Today I started working on the last one. As the installation of software takes time I always can do stuff in between. For example laundry. :)

Also, I want to document parts of my journey. Maybe I will craft an in-depth Medium article later. Without further ado, let's get started.

Preparation

For my setup, I'm using 3 Raspberry Pi's Model B of the second Generation. They are mounted in a generic case for 4 Pi's with a fan at the front and use a common power supply.

Flashing the Raspbian Lite Image

Currently, there are numerous ways to flash a raspberry pi image. I used the Raspberry Pi Imager, that is provided by raspberrypi.org directly. It is also available for all platforms.

Enabling SSH

SSH can be enabled through running the command:

raspi-config

and selecting "Interfaces" -> "SSH". It will make it easier to work with afterwards.

Changing the Hostname

I also changed the Hostname of all three raspberries to docker1 to docker 3. It is important to change both "/etc/hostname" and "/etc/hosts" File.

Updating

Although the images on the raspberry pi website are often updated, it is a good idea to update everything right away.

apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y

The "-y" Parameter sets every confirmation that might appear automatically to yes.

Docker Setup

Now it is finally time to start working with Docker.

Software Installation

Sadly there aren't packages inside the apt-universe for docker on the raspberry pi yet, so we have to use the provided installation script.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

The script will now install all required dependencies. Normally docker commands can only be run by the root user, to make them available for "normal"-Users (like pi) you can run the following command:

sudo usermod -aG docker pi

Swarm Setup

Now that we've got Docker running, it is time to join the separate raspberry pi's into a Docker-Swarm Cluster. It would also be possible to create a Kubernetes Cluster, but for my use-case, I don't want to have the whole overhead of setting up such a cluster.

On the first node (docker1) we are going to run the following command:

docker swarm init --advertise-addr 192.168.1.29

The IP address is used to tell Docker which interface to use. The command will print out another command which can be used to connect the worker nodes. It will look like the following:

docker swarm join --token SWMTKN-1-09tv8l7v6w4tgyavba92trelpcx7p56urjvc0an3fmzthfzrxr-aau5gnmflj528w1hxpxj7o9j1 192.168.1.29:2377

We run this new command on the other two nodes (docker2 & docker 3).

With the help of the command

docker node ls

we can see all nodes that are now connected.

The first Container to run

Now that the Cluster is running, we can start our first Container. All commands are run on the docker1-node. There is a really interesting project that visualizes the running containers inside the cluster on a fancy web-interface.

sudo 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:latest

This will create a container with the image alexellis2/visualizer-arm and open the Port 8080 to it. It might take a while to spin the container up, so you can check the state through:

sudo docker service ls

As soon as at says replicated 1/1 the container is up and running. In your web browser, you can now access the web interface through

http://192.168.1.29:8080/

And there you go. A running Docker Swarm Cluster to experiment with. :)