Setting Up a Docker Swarm Cluster on 3 RaspberryPi Nodes
As the curious person that I am, I like to play around with new stuff that I stumble upon, and one of them was having a docker swarm cluster running on 3 Raspberry Pi’s on my LAN.
The idea is to have 3 Raspberry Pi’s (Model 3 B), a Manager Node, and 2 Worker Nodes, each with a 32 GB SanDisk SD Card, which I will also be part of a 3x Replicated GlusterFS Volume that will come in handy later for some data that needs persistent data.
If you have an internal DNS Server, set an A Record for each node, or for simplicity, set your hosts file on each node so that your hostname for each node responds to it’s provisioned IP Address:
Time to set up our swarm. As we have more than one network interface, we will need to setup our swarm by specifying the IP Address of our network interface that is accessible from our LAN:
Now that we have our IP Address, initialize the swarm on the manager node:
12345678910
pi@rpi-01:~ $ docker swarm init --advertise-addr 192.168.0.2
Swarm initialized: current node (siqyf3yricsvjkzvej00a9b8h) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \ --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 \ 192.168.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Then from rpi-02 join the manager node of the swarm:
12
pi@rpi-02:~ $ docker swarm join --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 192.168.0.2:2377
This node joined a swarm as a worker.
Then from rpi-03 join the manager node of the swarm:
12
pi@rpi-03:~ $ docker swarm join --token SWMTKN-1-0eith07xkcg93lzftuhjmxaxwfa6mbkjsmjzb3d3sx9cobc2zp-97s6xzdt27y2gk3kpm0cgo6y2 192.168.0.2:2377
This node joined a swarm as a worker.
Then from the manager node: rpi-01, ensure that the nodes are checked in:
12345
pi@rpi-01:~ $ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
62s7gx1xdm2e3gp5qoca2ru0d rpi-03 Ready Active
6fhyfy9yt761ar9pl84dkxck3 * rpi-01 Ready Active Leader
pg0nyy9l27mtfc13qnv9kywe7 rpi-02 Ready Active
Setting Up a Replicated GlusterFS Volume
I have decided to setup a replicated glusterfs volume to have data replicated throughout the cluster if I would like to have some persistent data. From each node, install the GlusterFS Client and Server:
Then your GlusterFS Volume will be mounted on all the nodes, and when a file is written to the /mnt/ partition, data will be replicated to all the nodes in the Cluster:
Docker Swarm’s Routing mesh takes care of the internal routing, so requests will respond even if the container is not running on the node that you are making the request against.
With that said, verifying on which node our service is running:
123
pi@rpi-01:~ $ docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sd67cd18s5m0 web.1 hypriot/rpi-busybox-httpd:latest rpi-02 Running Running 2 minutes ago
When we make a HTTP Request to one of these Nodes IP Addresses, our request will be responded with this awesome static page:
We can see we only have one container in our swarm, let’s scale that up to 3 containers:
12
pi@rpi-01:~ $ docker service scale web01=3
web01 scaled to 3
Now that the service is scaled to 3 containers, requests will be handled using the round-robin algorithm. To ensured that the service scaled, we will see that we will have 3 replicas:
123
pi@rpi-01:~ $ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
vsvyanuw6q6y web replicated 3/3 hypriot/rpi-busybox-httpd:latest *:891->80/tcp
Verifying where these containers are running on:
12345
pi@rpi-01:~ $ docker service ps web01
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sd67cd18s5m0 web.1 hypriot/rpi-busybox-httpd:latest rpi-02 Running Running 2 minutes ago
ope3ya7hh9j4 web.2 hypriot/rpi-busybox-httpd:latest rpi-03 Running Running 30 seconds ago
07m1ww7ptxro web.3 hypriot/rpi-busybox-httpd:latest rpi-01 Running Running 28 seconds ago
Lastly, removing the service from our swarm:
12
pi@rpi-01:~ $ docker service rm web01
web01
Massive Thanks:
a Massive thanks to Alex Ellis for mentioning me on one of his blogposts: