Notes on getting Docker Swarm cluster running on two Raspberry Pi 3 devices. We will then deploy a simple nodejs app container on the new cluster.
A big thanks to the folks at Docker and Hypriot for making this easy. What I used for this experiment…
- Two Raspberry Pi 3 boards.
- Two USB power supply adaptors for the Pi 3 boards.
- Two 16 GB microSDHC card.
- I am using a Mac OS laptop so cannot confirm that the steps will work on other platforms.
I bought the above (except the Mac) using my Amazon Prime membership with same day delivery. We need to load the SD cards with HypriotOS. To help with that, download the flash utility from github https://github.com/hypriot/flash.
Create a file device-init.yaml with your wifi settings. If you plan to use ethernet wiring then you can ignore this part. For each SD card, change the hostname in the file to desired name for the particular Pi3 device. In my case swarm-pi01/02. Don’t forget to do that before each “flash”.
1 2 3 4 5 6 |
hostname: swarm-p01 wifi: interfaces: wlan0: ssid: "your wifi ssid" password: "your wifi password" |
Now run the flash commands for each Raspberry Pi 3 board. (Reminder again) Make sure to change the device-init.yaml with the updated hostname before flashing for each SD card (pi02).
1 2 |
./flash --config device-init.yaml --hostname node1 https://github.com/hypriot/image-builder-rpi/releases/download/v1.7.1/hypriotos-rpi-v1.7.1.img.zip ./flash --config device-init.yaml --hostname node2 https://github.com/hypriot/image-builder-rpi/releases/download/v1.7.1/hypriotos-rpi-v1.7.1.img.zip |
Insert the SD cards into each of the Pi 3 devices. Connect the power supply. The device should now boot up. I use LanScan Pro to discover the IP addresses for the two Pi3 devices. In this case you can use the hostname to ssh into each device using default password of hypriot.
1 2 |
ssh pirate@node1 ssh pirate@node2 |
Now we start the Swarm cluster setup. Lets initialize the Swarm manager on swarm-pi01
1 |
docker swarm init --advertise-addr 192.168.1.165 |
Once completed it will print (to console) the join command which can be used to add additional devices to the cluster. An example of this from my local environment is shown below. Run this on the 2nd Pi 3 device (in my case on swarm-pi02).
1 2 3 |
docker swarm join \ --token SWMTKN-1-3bsrh8dxzvvcoyg0k3ixzaeaeblhl49az9tjd6pd9xe4psh364-ck6zaq23xt67anp6en5j15n4t \ 192.168.1.165:2377 |
Run docker node ls to list all the nodes in the cluster. Run this from the docker manager node.
So now we have a Docker Swarm cluster up and running. Next step is to deploy a simple application to test our cluster. Clone the simple Nodejs code from github repo https://github.com/thomasma/expressjs_docker. We will build a new Docker image and upload that to Docker hub. Next we will log into the Raspberry Pi3 that is the Swarm manager for your cluster and deploy this as a Docker service.
In the steps below replace references to mattazoid with your own name. Also refer to Docker documentation on how to push to the docker hub – https://docs.docker.com/engine/getstarted/step_six/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Make sure you are in the root folder for the nodejs project // Build the Docker image docker build -t expresshello . // List the images to get the image id docker images // Tag the image. Replace id below with your image id docker tag 2c78ab7dd4e6 mattazoid/expresshello:latest // login to docker hub (please create an account there) docker login // push the image to the remote hub docker push mattazoid/expresshello // check Docker hub to confirm if the image was uploaded there |
Post that, log into the Raspberry Pi3 node that is your Swarm manager and run the following
1 2 3 4 5 6 |
// create the docker service docker service create -p 3000:3000 --name expresshello --replicas=5 mattazoid/expresshello // to check status of your deploy run one of docker service ls docker service ps expresshello |
In the above case two of the nodes are active while the other 3 are still getting prepared. If on the actively running node (in this case swarm-pi01) you run ‘curl http://localhost:3000/test123″ you will see the text echoed back.
Tip: I had my initial container based off a Ubuntu image (and unfortunately forgot completely about that). Eventually I changed to the https://github.com/hypriot/rpi-node image. This image is a Raspberry Pi compatible Docker base image.