Raspberry Docker Cluster
A cluster is a group of computers performing the same task, operating as a single system. Although Raspberry Pi's are pretty incredible devices, a single Pi can only do so much.
A Raspberry Pi cluster can be used anytime you want to improve performance or availability, and there are many use cases including serving web content, mining cryptocurrency, processing large amounts of data (using tools like hadoop), and much more. This guide will show you how to run a Raspberry Pi cluster using Docker Swarm.
This is fun part.
You don't actually need a tower, but you might as well build one. Using a tower like this makes everything more organized and easier to work with.
We've written a detailed guide that will walk you through the process ofÂ installing Jessie on your Raspberry Pi. Since we're dealing with multiple Pi's, you'll have to do this for each one.
Note: For each Pi we're going to do some configuration while the SD card is in your computer. So read the next step to see what to do after installing Raspbian on each SD card.
After installing Jessie, and while the SD card is still in your computer, we're going make a few changes to the configuration. Follow the links in this step for detailed instructions on each change.
First, go ahead andÂ enable SSH.
ThenÂ set up WiFi for the Pi.
Once you've installed and configured each SD card, insert one into each Raspberry Pi.
*Protip: Once you've configured one SD card, you can use a utility such asÂ ApplePi BakerÂ to clone it onto the other SD cards.
Connect your Raspberry Pi's to their power supplies, and make sure they boot properly. Near the USB connector, you should see a solid red LED. This indicates that the Pi has power. Then you should see a green flashing LED, which indicates there is SD card activity.
We've written a guide that shows youÂ how to find the IP address of your Raspberry Pi. So follow this guide if you need more detailed instructions, but we're going to use nmap.
Note: You might need to use a different subnet range. Follow the guide for more information on finding the correct subnet.
This will list all of the devices connected to the network, so we should see all four of our Raspberry Pi's. When you spot them, take note of the IP address for each. Here is my output:
So the IP addresses for all four of my Raspberry Pi's are:
For each Raspberry Pi (node) in our cluster, I'm going to change the hostname so it's easier to access. Since I've got four nodes I'm going to name them docker1, docker2, docker3, and docker 4.
You'll also want to add an entry in your local /etc/hosts file that maps the new hostname to the IP address for each Pi. The end of my /etc/hosts now looks like this:
This step is optional, but it will allow us to easily SSH into each node without using a password. I'll use the ssh-copy-id command to copy my public key to the remote server.
If you don't have a key generated already, you can do so with:
Hit enter twice and leave the passphrase empty (unless you really want to use a passphrase).
Then copy the key to each node with:
You can use this simple bash one-liner to install docker on each node.
Of course, you'll have to use your own hostnames, but this should loop through each host and install docker. For more detailed instructions onÂ how to install Docker on your Raspberry Pi see this guide.
Now that Docker is installed we need to create our swarm. We only need to do this on one of the nodes. So the order of operations will be: 1. create swarm on a single node (this node will be a manager node), 2. join one more node as a manager, and 3. join two more nodes as workers. So we'll have a total of 2 managers and 2 workers in our swarm.
So in this step, I'll create the swarm on docker1.
To do this, we'll first have to get the IP address of the node. If you're following this guide step by step, we already have the IP address in our local /etc/hosts file. If not, you can useÂ this guideÂ as a reference.
For me, the address of docker1 isÂ 192.168.1.181. So I'll use this command to create the swarm:
You'll notice that this outputs a command that can be used to join other nodes to the swarm as a worker. The command looks something like this:
Keep track of this command. Although you can regenerate a token if necessary, we're going to use this in the next step.
Now we've got our swarm with one manager node. You can confirm by typing:
Now we've got to add the rest of the nodes to the swarm. As I mentioned in the previous step, we're going to add one more manager and two more workers.
Let's start by adding our next manager node on docker2. To do this, we'll have to generate a manager token. While still logged into docker1, type:
This will generate another join command like the one from the previous step. So we'll grab this command, ssh into docker2 (from our local machine), and use the manager join command.
Keep in mind that the two swarm join commands look similar, so we'll have to keep track of which is which. We just joined docker2 to the swarm as a manager, so now we'll use the other token and join docker3 and docker4 as workers.
And again from your local machine:
Now all of our nodes have joined the swarm. Let's run ourÂ node lsÂ command again to confirm.
And you should see all of the nodes listed:
At this point we've accomplished the purpose of the guide, but we might as well run a service to demonstrate how to use the swarm. Our swarm can run any number of services and they can be scaled and distributed across the nodes in our cluster according to our preferences. To begin, we can run a visualizer service that will provide a web interface to visualize how the containers are distributed across the swarm.
This service is built from an image called "visualizer" originally created byÂ Github user ManoMarks. But we'll have to use a version of this image that was built to work on the Raspberry Pi and other ARM devices.
So let's log on to docker1 and create our visualizer service:
This will probably take a few minutes to spin up because it has to download the image. We can check the status by typing:
Until the service is ready, you'll see the REPLICAS value asÂ 0/1.
It will sayÂ 1/1Â when it's ready.
Once it's ready, you can visitÂ http://192.168.1.181:8080Â (the IP of docker1 and port 8080). This will show you a nice visualization of the swarm and which containers are running on which nodes.
Of course, for your purpose you'll be creating at least one other service. There is a lot of pertinent information about docker that won't (and can't) be covered in this guide, but check out theÂ official docker documentationÂ for more information and of course check out theÂ docker interestÂ on howchoo.
We have 35 guests and no members online
Articles Most Read
Articles by Date