Here is out third article in the Building a Raspberry Pi Cluster series. We will talk about what software we can use to make all the cluster nodes respond to your commands all at once, so that you can install whatever you wish and do it only once for ll of the cluster nodes instead of configuring them one by one as separate entities. Such software greatly eases your work and reduces the time needed to perform operations. It does not matter if you have four, eight or fifty nodes to work with you can make them all do the same thing at the same time.
In this tutorial you will learn:
- How to install and configure ClusterSSH
- How to install fabric and use the fab command
- How to give commands to your cluster
Building Raspberry Pi Series:
- Building a Raspberry PI Cluster – Part I: Hardware Acquisition and Assembly
- Building a Raspberry PI Cluster – Part II: Operating system installation
- Building a Raspberry PI Cluster – Part III: Simultaneous Node Management
- Building a Raspberry PI Cluster – Part IV: Monitoring
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Raspberian Linux |
Software | ClusterSSH |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Preparation
You have your cluster set up, Raspbian is installed on each node. Now all you have to do is SSH into each of them and change the hostname of these Raspberry Pis starting with the one at the bottom of the cluster and going up. You can change the hostname with
$ sudo hostname rpi1
Reboot each Pi after that to make the changes take effect at the prompt level and move on to the next Raspberry Pi. Leave the default pi
user on each node and change its password to something else, but make sure that each node has the same user and the same password defined.
Work all nodes at once with ClusterSSH
The best thing about a Raspberry Pi cluster is that its cheap to make, fast and doesn’t require many resources. And once you set it up as detailed in our previous articles you can install software on each node as you were using a single computer. The best software for this is ClusterSSH – an SSH piece of software that can be set up on your cluster nodes so that you can access them all at once and give them commands to execute.
Imagine a four node Raspberry Pi cluster, each with its own terminal window. And whatever you type in a dialog box gets reproduced in real-time in each of those terminal windows. That’s what ClusterSSH does – it takes the input of a small window and transforms it into input distributed to all of the cluster nodes.
To set up clusterssh
you open up you favourite Linux distribution on your laptop and search for it in your repositories. If you’re using Ubuntu the installation is done with
$ sudo apt install clusterssh
This will create a few files on your system. The first one you should take into account however is your /etc/hosts
file. Open it up in a text editor and add the four nodes of the cluster in this file, one per line:
192.168.1.124 rpi4 192.168.1.126 rpi1 192.168.1.150 rpi3 192.168.1.252 rpi2
The cluster we’re using in this tutorial gets its IP addresses via DCHP from a local router. To check what IP address each of your Raspberry Pi nodes has use ifconfig
. We assigned an easy to remember hostname for each of these nodes: rpi1
, rpi2
, rpi3
and rpi4
. Once you have their IP address and have modified the /etc/hosts
file on your laptop or PC it will be easier to access each of these nodes. Save the /etc/hosts
file and close it.
Now you will have to open the /etc/clusters
file that clusterssh
uses. If this file is not present after you have installed clusterssh
you can create it yourself. Add the following on the first line of the file:
picluster pi@rpi1 pi@rpi2 pi@rpi3 pi@rpi4
and save the file. This tells clusterssh
that there is a cluster named picluster
defined and that it has four nodes with the same user on each one: pi
. There is another file you should know of – the configuration file located in ~/.clusterssh/
. Simply named config
, it contains configuration options regarding the terminal windows you will be using to command the cluster. For example, if you want to change the terminal font to Terminus, add the line
terminal_font=terminus-iso8859-9-16
to the file. You can predefine an ssh user the software will invoke when launched, once in each terminal window, so also add the
user=pi
line to ~/.clusterssh/config
.
Once you have set up the config file as you want it to you can connect all the cluster nodes so the Raspberry Pis will all boot, wait for about 30 seconds for them to reach the end of the booting process and then launch clusterssh
on your laptop with
$ cssh picluster
Four Terminal
windows should pop up at once and they will ask for the login password of a user named pi
. There is a small window with a dialog box that you can type all your command in and you should type the password into that box and hit Enter
. All the nodes will log you in at once and from this point on you can bulk install anything on the cluster, update all the nodes with a single command, edit configuration files and basically do everything once, instead of repeating the same steps four times in a row.
Work all nodes at once with Fabric
It may be that you want to give a command to the Raspberry Pi cluster and you don’t want to use four separate Terminal windows. If you plan to extend your cluster to – say – 8 or 12 nodes dealing with that many screens on your desktop would be unpleasant. So there is an alternative to clusterssh
and it’s a small Python script.
Make sure you have Python
installed on the laptop or PC you use to access the cluster nodes via SSH and use pip
to install the fabric
Python package:
$ sudo pip install fabric
Now create a fabfile.py
file in your home directory with
$ touch fabfile.py
and make it executable:
$ chmod +x fabfile.py
Now edit that file and add the following script to it:
from fabric.api import *
env.hosts = [
#RPi1
'pi@192.168.1.126',
#RPi2
'pi@192.168.1.252',
#RPi3
'pi@192.168.1.124',
#RPi4
'pi@192.168.1.150',
]
# the downside is you have to use a plaintext password
env.password = 'YOUR_PI_PASSWORD'
# launch the command to all the cluster nodes
@parallel
def cmd(command):
sudo(command)
Replace the IP addresses above with the ones assigned to your cluster nodes and change YOUR_PI_PASSWORD
to the password assigned to the pi
user on each node. Save the fabfile.py
file you have just modified. Now type
$ fab cmd:"ls -la"
to see a directory listing of all your files in the home directory of each cluster node – all in the same terminal window. The fab cmd:""
command takes any command you put in between those quotes and executes it on each cluster node giving you a verbose output in the Terminal window you typed that in. It’s useful when executing long operations such as compiling or updating your system as you can see the lines scroll and always know which cluster node is in which part of the process at what time.
Conclusion
You can thus setup each of your cluster nodes with the software you desire. You can use the cluster to bulk compile applications, parse data, install R packages – all without the need to repeat the same command. You can controll the entire cluster with either clusterssh
or the fabfile.py
detailed above. In the next part of the series we will take a look at how to get information out of your cluster and how to monitor all nodes and their resources.