How to Build a Raspberry Pi Cluster?
The instructions below will show how to built a Raspberry Pi Cluster with six Raspberry Pi (1x RPi 4, 3x RPi 3 B+, and 2x RPi 3 v. 1.2). They are based on many excellent guidelines (but not limited to), as follows:
Installing and setting up Raspbian
- Download the Raspbian image from Raspberry Pi website
- Download and install Win32 Disk Imager from SourceForge website
- Burn the Raspbian image to the MicroSD Card using Win32 Disk Imager (I used 32 Gb MicroSD)
- Enable SSH by creating an extension-less file named "ssh" in the root of the boot section
- Connect the MicroSD Card (with Raspbian burned on it) to the Raspberry Pi
- Connect a network cable from the Raspberry Pi to your router/switch (same router your PC is connected to)
- Connect a Micro USB cable from the Raspberry Pi to a power source.
- Download PuTTY from website
- Open up command prompt (type cmd in the search of Windows)
- Type “ping raspberrypi ” and hit enter (write down your Pi’s IP Address)
- Open up PuTTY and type the Pi’s IP Address and click open
- login is pi and password is raspberry
- Type “sudo raspi-config“ and hit enter
- Select "Update"
- Expand the file system
- Advanced options
- Set the hostname to "Pi01", or type "sudo nano /etc/hostname"
- Exit raspi-config
Installing analytical programs (Mathematica and R)
- sudo apt-get -y update
- sudo apt-get -y upgrade
- sudo apt-get -y install wolfram-engine
- sudo apt-get -y install r-base
To check how much space is left in the SD card type "df -h"
Installing MPICH
- sudo mkdir mpich2
- cd ~/mpich2
- sudo wget http://www.mpich.org/static/downloads/3.2/mpich-3.2.tar.gz
- sudo tar xfz mpich-3.2.tar.gz
- sudo mkdir /home/rpimpi/
- sudo mkdir /home/rpimpi/mpi-install
- sudo mkdir /home/pi/mpi-build
- sudo apt-get -y install gfortran
- sudo /home/pi/mpich2/mpich-3.2/configure -prefix=/home/rpimpi/mpi-install
- sudo make
- sudo make install
- cd ..
- nano .bashrc
Paste the following line to bottom of file:
PATH=$PATH:/home/rpimpi/mpi-install/bin
I also added two optional commands in the .bashrc after the PATH:
df -h (to show space left in the MicroSD), and
htop (to show the interactive process viewer)
Then hold CTRL press “6” “x” let go of CTRL press “y” “enter”)
- sudo reboot
- mpiexec -n 1 hostname
Installing MPI4PY
Install nmap
- sudo apt-get -y update
- sudo apt-get -y install nmap
Create image file of Pi01 and Duplicate to other MicroSD
- Remove the MicroSD from the Raspberry Pi
- Create image using the Win32 Disk Imager
- Copy to other MicroSD
- Insert the duplicate MicroSD into the additional Raspberry Pi nodes
Get current IP
Scan subnet for Pi0x
- sudo nmap -sn 192.168.1.*
Change Pi0x Name
Do this step for Pi02, Pi03, Pi04, Pi05, and Pi06:
- Let's assume that the IP addresses are:
- Pi01: 192.168.1.1
- Pi02: 192.168.1.2
- Pi03: 192.168.1.3
- Pi04: 192.168.1.4
- Pi05: 192.168.1.5
- Pi06: 192.168.1.6
- Do the following for Pi02, Pi03, Pi04, Pi05, and Pi06
- ssh pi@192.168.1.2
- password is raspberry
- sudo nano /etc/hostname
or
- sudo raspi-config
- Scroll down to Advanced hit Enter
- Scroll down to Hostname hit Enter
- Change the name (Pi01,Pi02,Pi03,Pi04) Hit Enter
- Press Tab until Finish is selected then hit Enter
Edit NODES file
Chage the IP addresses below to reflect the IP addresses of the Pi0x. Enter them in the nodes file.
- nano nodes
- 192.168.1.1
- 192.168.1.2
- 192.168.1.3
- 192.168.1.4
- 192.168.1.5
- 192.168.1.6
Hold CTRL Press 6 x Press y Hit Enter (or CTRL O and CTRL X)
Text NODES
- mpiexec -n 1 hostname
- mpiexec -f nodes -n 6 hostname
Should get errors on the second command because Pi01 does not have access to the other nodes. So, do the following:
Add keys to all Pi0x
Pi01
- ssh-keygen
- cd ~
- cd .ssh
- cp id_rsa.pub Pi01
Pi02
- ssh pi@192.168.1.2
- ssh-keygen
- cd .ssh
- cp id_rsa.pub Pi02
- scp 192.168.1.1:/home/pi/.ssh/Pi01 .
- cat Pi01 >> authorized_keys
- exit
Pi03
- ssh pi@192.168.1.3
- ssh-keygen
- cd .ssh
- cp id_rsa.pub Pi03
- scp 192.168.1.1:/home/pi/.ssh/Pi01 .
- cat Pi01 >> authorized_keys
- exit
Pi04
- ssh pi@192.168.1.4
- ssh-keygen
- cd .ssh
- cp id_rsa.pub Pi04
- scp 192.168.1.1:/home/pi/.ssh/Pi01 .
- cat Pi01 >> authorized_keys
- exit
Pi05
- ssh pi@192.168.1.5
- ssh-keygen
- cd .ssh
- cp id_rsa.pub Pi05
- scp 192.168.1.1:/home/pi/.ssh/Pi01 .
- cat Pi01 >> authorized_keys
- exit
Pi06
- ssh pi@192.168.1.6
- ssh-keygen
- cd .ssh
- cp id_rsa.pub Pi06
- scp 192.168.1.1:/home/pi/.ssh/Pi01 .
- cat Pi01 >> authorized_keys
- exit
Append the authorized keys to the Pi01
From Pi01, do the following
- scp 192.168.1.2:/home/pi/.ssh/Pi02 .
- cat Pi02 >> authorized_keys
- scp 192.168.1.3:/home/pi/.ssh/Pi03 .
- cat Pi03 >> authorized_keys
- scp 192.168.1.4:/home/pi/.ssh/Pi04 .
- cat Pi04 >> authorized_keys
- scp 192.168.1.5:/home/pi/.ssh/Pi05 .
- cat Pi05 >> authorized_keys
- scp 192.168.1.6:/home/pi/.ssh/Pi06 .
- cat Pi06 >> authorized_keys
Check new nodes
- cd ~
- mpiexec -f nodes -n 6 hostname
Run Python code on cluster
- Create prime.py file in the /home/pi of Pi01
- Then copy to the other nodes as follows
- scp prime.py 192.168.1.2:/home/pi
- scp prime.py 192.168.1.3:/home/pi
- scp prime.py 192.168.1.4:/home/pi
- scp prime.py 192.168.1.5:/home/pi
- scp prime.py 192.168.1.6:/home/pi
- mpiexec -f nodes -n 4 python prime.py
Then copy the primality_cluster_test1.py (listed below) to all nodes, and runt the following command in Pi01
- scp primality_cluster_test1.py 192.168.1.2:/home/pi
- scp primality_cluster_test1.py 192.168.1.3:/home/pi
- scp primality_cluster_test1.py 192.168.1.4:/home/pi
- scp primality_cluster_test1.py192.168.1.5:/home/pi
- scp primality_cluster_test1.py 192.168.1.6:/home/pi
This code will run only on node 1 (Pi01), using all four cores (-n 4). It took 7.738 seconds to find 109926 primes.
- time mpiexec -n 4 python primality_cluster_test1.py
Then, this code will run on all nodes, using all twenty-four cores (-n 24). It took 5.952 seconds to find 109926 primes. Remember the Pi01 (node 1) is a Raspberry Pi 4. When tested on Pi06 (node 6), which is a Raspberry Pi 3 v 1.2, it took 25.4 seconds!!! So combining old and new versions of Raspberry Pi may not be the ideal approach for building a cluster.
- time mpiexec -hostfile nodes -n 24 python primality_cluster_test1.py
Use htop to view the processes in each node.
Sample Python code (prime.py)
Copy exact as shown below.
def is_prime(n):
status = True
if n < 2:
status = False
else:
for i in range(2,n):
if n % i == 0:
status = False
return status
for n in range(1,101):
if is_prime(n):
if n==97:
print n
else:
print n,",",
How a Raspberry Pi should work by Gary Explains
https://www.youtube.com/watch?v=VzcarXuVUvU&t=639s
The file for primality_cluster_test1.py is here.
Adding an external harddisk
Some additional information by Alasdair Allan, Coobird's Lab, (Part 1, Part 2, Part 3)