Setting up a Raspberry Pi cluster

I am always interested in trying things out. Recently I have had the idea to build a small Raspberry Pi cluster, both as an exercise but also to have a cluster available for other tests in the future. The Raspberry Pi is both cheap and quite nice to hack around with.

I looked around a while before settling on using a D-Link DIR-100 router. I had already bought a 5 port USB power adapter and the DIR-100 was also powered by 5V and had 4 LAN ports. Perfect setup for a small 4 node cluster.

Yeah, getting short cables and 3D printing some holders for the Raspberries was also fun. I think there is some special aesthetics with open electronics.

Well, then came the question how to manage this little thing? As I connected the router to my already local LAN the nodes got ip addresses from and upward. The router could also be configured to put one of the nodes as DMZ host and that one could be the master. Getting the specific ip addresses on each node was just a matter of powering them in the right order.

Now everything was powered and accessable. But, it felt tedious logging in and jumping between the nodes in the terminal. So next challenged got to be to create a few cluster helper scripts. I created a small script suit I call rpi-cluster. The main script being slaves_exec which can distribute commands to the other nodes in cluster, either in sequence or parallel.

# -m to add master to nodes being executed
# -p to run in parallel
slaves_exec -m -p sudo apt-get update
slaves_exec -m -p sudo apt-get upgrade
# See screencast here

Other scripts are slaves_halt and slaves_reboot (takes -m for including the master node).

As I also want to be able to reset the cluster and easily start from clean, one challenge was to go from vanilla installed raspberian-lite SD cards to a working cluster setup repeatable and quickly. So this ended up to be a couple of init scripts that can do some of the tedious work. But there are a few things to setup on the master manually. Some I managed to put in a small script I put on the net so it can easily be streamed into bash.

curl -s | bash

This will install git and clone the rpi-cluster repo and then end with displaying the manual parts to get all done.

In .bashrc, above check for interactive shell add
  export CLUSTER_HOME=~/rpi-cluster
If you use to get locale warnings in bash you can add
  export LC_ALL=C
  nano .bashrc
Edit ip addresses in rpi-cluster/master and rpi-cluster/slaves to
reflect your local setup.
  nano rpi-cluster/master
nano rpi-cluster/slaves
  cd rpi-cluster
git add master slaves
git commit -m “Updated ip addresses”
Run ‘sudo raspi-config’ to expand filesystem and change hostname to ‘pi01’ (master hostname)
Reboot master and login again and now run ‘init_cluster’ to setup slaves

Made a screencast of the whole procedure from plain raspberian-lite SD card installation to rpi-cluster setup here.

I managed to buy a couple extra DIR-100, drop me an email if you are interested.

So, time to ponder on new challenges that can be created on a small cluster.