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

  1. Download the Raspbian image from Raspberry Pi website
  2. Download and install Win32 Disk Imager from SourceForge website
  3. Burn the Raspbian image to the MicroSD Card using Win32 Disk Imager (I used 32 Gb MicroSD)
  4. Enable SSH by creating an extension-less file named "ssh" in the root of the boot section
  5. Connect the MicroSD Card (with Raspbian burned on it) to the Raspberry Pi
  6. Connect a network cable from the Raspberry Pi to your router/switch (same router your PC is connected to)
  7. Connect a Micro USB cable from the Raspberry Pi to a power source.
  8. Download PuTTY from website
  9. Open up command prompt (type cmd in the search of Windows)
  10. Type “ping raspberrypi ” and hit enter (write down your Pi’s IP Address)
  11. Open up PuTTY and type the Pi’s IP Address and click open
  12. login is pi and password is raspberry
  13. Type “sudo raspi-config“ and hit enter
  14. Select "Update"
  15. Expand the file system
  16. Advanced options
  17. Set the hostname to "Pi01", or type "sudo nano /etc/hostname"
  18. Exit raspi-config

 

Installing analytical programs (Mathematica and R)

To check how much space is left in the SD card type "df -h"

 

Installing MPICH

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”)

 

Installing MPI4PY

 

Install nmap

 

Create image file of Pi01 and Duplicate to other MicroSD

  1. Remove the MicroSD from the Raspberry Pi
  2. Create image using the Win32 Disk Imager
  3. Copy to other MicroSD
  4. Insert the duplicate MicroSD into the additional Raspberry Pi nodes

 

Get current IP

 

Scan subnet for Pi0x

 

Change Pi0x Name

Do this step for Pi02, Pi03, Pi04, Pi05, and Pi06:

  1. Let's assume that the IP addresses are:
    1. Pi01: 192.168.1.1
    2. Pi02: 192.168.1.2
    3. Pi03: 192.168.1.3
    4. Pi04: 192.168.1.4
    5. Pi05: 192.168.1.5
    6. Pi06: 192.168.1.6
  2. Do the following for Pi02, Pi03, Pi04, Pi05, and Pi06

or

 

Edit NODES file

Chage the IP addresses below to reflect the IP addresses of the Pi0x. Enter them in the nodes file.

Hold CTRL Press 6 x Press y Hit Enter (or CTRL O and CTRL X)

 

Text NODES

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

Pi02

Pi03

Pi04

Pi05

Pi06

Append the authorized keys to the Pi01

From Pi01, do the following

 

Check new nodes

 

Run Python code on cluster

  1. Create prime.py file in the /home/pi of Pi01
  2. Then copy to the other nodes as follows

Then copy the primality_cluster_test1.py (listed below) to all nodes, and runt the following command in Pi01

This code will run only on node 1 (Pi01), using all four cores (-n 4). It took 7.738 seconds to find 109926 primes.

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.

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)