DEV Community

Mikts
Mikts

Posted on • Originally published at clusterstacks.codes

How to prepare Raspberry Pi for Docker Swarm

Swarm Cluster

Introduction

With the release of the Raspberry Pi 4B using the board to run container orchestrators
like Kubernetes and Docker Swarm is a viable option both a learning platform but also
as a low power development lab. In this series we will build a Swarm cluster on those little boards.

Components

  • x3 Raspberry Pi 4B
  • x3 SD Cards
  • x3 External Storage optional

OS Prepare

We will use the hypriotOS a lightweight arm distribution optimized for docker engine with some cool
features like cloud-init and a nice flash tool which we will use.

curl -LO https://github.com/hypriot/flash/releases/download/2.5.0/flash
chmod +x flash
sudo mv flash /usr/local/bin/flash
Enter fullscreen mode Exit fullscreen mode

Then download the latest hypriot.img

wget https://github.com/hypriot/image-builder-rpi/releases/tag/v1.11.5
Enter fullscreen mode Exit fullscreen mode

Cloud-Init

A convenient configuration tool mostly used in cloud environment.

Below is a template you can find the vars you should change with "{{ var_name }}" useful for ansible 😉

hostname: " {{ your_hostname}} "
manage_etc_hosts: true

users:
  - name: " {{ your_user }} "
    gecos: "Swarm Admin"
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    groups: users,docker,video,input
    lock_passwd: true
    ssh_authorized_keys:
      - ssh-rsa "{{ your_pub_key }}"

runcmd:
  # Pickup the hostname changes
  - 'systemctl restart avahi-daemon'
Enter fullscreen mode Exit fullscreen mode

Flash the img to each sd card better to use different hostname for each

flash -u cloud-init.yml hypriotos-rpi-v1.11.5.img.zip
Enter fullscreen mode Exit fullscreen mode

Choose your sd card in the prompt and let it flash it!

ssh to your pi with your rsa-key and you are ready for the final configurations.

Docker Engine optimize

Although you can run everything in the sd card and work perfectly fine the docker-engine
performs some heavy writing and sd cards are slow bricks so we will use the external drives
to mount the /var/lib/docker path to them and improve the performance and our sd cards lifetime.

Find your disk with lsblk and modify your /etc/fstab file similar to this:

cat /etc/fstab

/dev/sda1 /var/lib/docker ext4 defaults,auto,nofail,noatime 0 0
Enter fullscreen mode Exit fullscreen mode

Now all your docker related data will be stored in the external disk improving the performance.

Why Swarm

It's clear Kubernetes won the orchestrator war and is a much for feature complete proposition but also
a more complex one. Swarm is much easier to begin especially when you are just starting to learn about
Docker and Container Orchestration.

Conclusion

That's it your raspberries are ready with a lightweight os with docker-runtime ready to create a small
cluster. We will continue the series with creating the cluster start running swarm services and stacks
on it.

Check how to deploy your Docker Swarm How to deploy Docker Swarm in Raspberry Pi Cluster

Top comments (0)