/314-swarm

DIY Raspberry Pi (Zero) cluster with Docker Swarm

MIT LicenseMIT


zeroSwarm

Raspberry Pi (Zero) cluster with Docker Swarm

Stuff you'll need

In this project I'll use:

Material Units Price
Raspberry Pi Zero W x2 ~10€/each
Samsung EVO (class 10) 32GB microSD card x2 ~13€/each
5V 2.4A Dual USB Charger 1x ~10€
(optional) TP-LINK TL-WR802N Nano router 1x ~25€

Steps

  1. Install Raspbian Stretch Lite on each microSD card. I've used the version from 2018-03-13.
  2. Enable SSH and set up Wi-Fi. In order to do this, insert the microSD into your pc, navigate to /media/YOUR_USER_NAME/boot and create an empty shh file:
    $ sudo touch ssh
    Then, open the other volume and use sudo nano /etc/wpa_supplicant/wpa_supplicant.conf to include the following lines:
    network={
        ssid="YOUR_NETWORK_NAME"
        psk="YOUR_PASSWORD"
        key_mgmt=WPA-PSK
    }
    
  3. Reproduce step 2 on each microSD and turn on all the Raspberry's.
  4. After 20 seconds, scan your network to find the IP of each Raspberry Pi Zero. You can either access your Wi-Fi router's settings or use:
    $ sudo nmap -sn 192.168.1.0/24
    Note: This step is optional if you have set up static IP addresses for all your nodes.
  5. SSH into your nodes (ssh pi@192.168.1.XYZ) and install Docker. By default the password is raspberry. Then (for each node) run:
    pi@raspberrypi:~ $ curl -sSL https://get.docker.com | sh
    Note: This will take a while!
  6. Only on the first (master) node initialize the swarm by using:
    pi@raspberrypi:~ $ sudo docker swarm init --advertise-addr 192.168.1.XYZ
    Where 192.168.1.XYZ is the IP of the current node. This will generate a command that will be used to add the rest of the nodes to the swarm.
  7. SSH into the rest of the nodes and run the command produced on the previous step to connect the swarm.
  8. On the master node (the one from step 6), run:
    pi@raspberrypi:~ $ sudo docker node ls
    to check all the nodes on the swarm. You can create a swarm of one manager node, but you cannot have a worker node without at least one manager node. This means that we'll have to promote our second node to a manager:
    pi@raspberrypi:~ $ sudo docker promote NODE_ID
    Node NODE_ID promoted to a manager in the swarm.

Now, let's create a service in our swarm with docker service create:

pi@raspberrypi:~ $ 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 build and start an image with just one replica:

pi@raspberrypi:~ $ sudo docker service ls
ID   NAME MODE       REPLICAS IMAGE                            PORTS
xxxx viz  replicated 1/1      alexellis2/visualizer-arm:latest *:8080->8080/tcp

To scale that image, run:

pi@raspberrypi:~ $ sudo docker service scale viz=4

At this point you should be able to visit 192.168.1.XYZ:8080 to see a nice swarm visualizer:


Visualizer

Congrats!