You are currently viewing Docker Swarm in Raspberry Pi

Docker Swarm in Raspberry Pi

Docker is a pretty useful tool to deploy our applications in containers. In this post we will cover how to install Docker, and configure it in Swarm mode on some Raspberry Pi. Also, we will use Portainer (CE), which is a web interface for managing Docker.

Requirements

These are the requierements to aproach this post:

  • Raspberry Pi (x2 at least).
  • As many MicroSD cards as raspberries. The size will depend on your future plans with your Pi.
  • A MicroSD card reader.
  • An image burner program like Raspberry Pi Imager.
  • A network infrastucture to communicate the devices.
  • Docker basic knowledge.

Install Raspberry with Ubuntu

To do so, I recommend to follow this guide I’ve already posted. It’s really straight forward!

Install Docker

Docker has several ways to install it, but the easiest one is by executing the get.docker.com SH file. This will install and configure all the needed packages that allows Docker to work properly. To do so:

curl -sSL https://get.docker.com | sh

Now you will be able to execute this:

sudo docker --version

And the output should look like this one (versions may vary):

Docker version 24.0.2, build cb74dfc

We could also make docker to work without root permissions, but Portainer will have some troubles when managing the swarm, so we will keep it like this.

Configure Swarm

Swarm is the cluster mode of Docker, so we can deploy sevear applications with redundancy and manage how many containers of one image we want to deploy in our workers nodes. It’s very useful, but to properly work on raspberry, we need some extra modules, so the nodes can communicate between them using ports/sockets:

sudo apt install linux-modules-extra-raspi

Once the command finished, just reboot:

sudo reboot now

Now, to start a new Swarm, we need to pick a Master. This device will be the one with the administration privileges on the Swarm, and will delivery the tasks (known as Stacks) to the Workers. Do not execute in the worker nodes!

sudo docker swarm init

The command above should have returned something like this:

Swarm initialized: current node (kzm57ekc4z6c9fgqqtuq4qbr4) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-1s6k2gsnifn031uwh0dx9sbnbd7gruam7px83d4jy2fhel0cdl-d3xmvp9fosnclgjbr16dd1c8a 192.168.65.4:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Now, let’s copy the token and make the other nodes to join, by executing it on them (remember, the token may vary, use your own):

docker swarm join --token SWMTKN-1-1s6k2gsnifn031uwh0dx9sbnbd7gruam7px83d4jy2fhel0cdl-d3xmvp9fosnclgjbr16dd1c8a 192.168.65.4:2377

Now you’ll have your swarm!

Setup Portainer CE in Swarm mode

Portainer, as mentioned before, is a web interface that allows us manage Docker, even in Swarm mode. It has a Community Edition that let us use as far as five nodes in swarm. It has been packaged on a docker container, and has two different components: The manager and the agent. The manager will be deployed on our swarm manager, and the agent will be deployed on every node. To do so, we will create the services with this stack file on our swarm manager node:

curl -L https://downloads.portainer.io/ce2-18/portainer-agent-stack.yml -o portainer-agent-stack.yml

Now, execute the stack with docker just in the manager:

sudo docker stack deploy -c portainer-agent-stack.yml portainer

Let’s check if the stack has been deployed:

sudo docker ps

The output should show us two containers running on the manager, and one on each worker node:

CONTAINER ID   IMAGE                           COMMAND                  CREATED        STATUS        PORTS                          NAMES
1bb6be4190c0   portainer/portainer-ce:2.18.3   "/portainer -H tcp:/…"   21 hours ago   Up 21 hours   8000/tcp, 9000/tcp, 9443/tcp   portainer_portainer.1.pq87h75ubxvr02n8ea92r81u0
d2ea350b4153   portainer/agent:2.18.3          "./agent"                21 hours ago   Up 21 hours                                  portainer_agent.xrbwhtatogmj9lmgyw4v587pa.fmb8cufvxquz8jmq3yq1jqvk9

Let’s dive in on Portainer’s web interface by going to our manager’s node IP, on port 9443 with https, and it will show up the admin user creation page, fill it:

Now, the enviroment wizard, we will select the local enviroment with the Get Started option:

Once done, Portainer is all working, and should look like this:

In the future, more posts about Portainer and its functionallity will come!

Summary

We’ve covered the Ubuntu installation on Raspberry, how to download and make Docker run on them, and how to create a swarm and manage it with Portainer.
Now, we have a functional Docker swarm, on low power consumption nodes with ARM on the Pi, and we can deploy all our containers, composes and stacks with ease!

Leave a Reply