Introduction to Pi Clusters Beeeeeen 2025-03-31

This guide will go over how to cluster multiple raspberry pi’s together, and how to run python scripts off the pi cluster

## Prerequisites

  • More than one raspberry pi
  • Each raspberry pi must be plugged into an ethernet switch
  • Power/ethernet cables for each pi
  • Internet access to download updates and module
  • A micro SD card with Raspbian for each pi

    Raspbian Installation

    1. Plug each micro SD card into your pc, then Download Raspian
    2. Plug in the micro SD card, ethernet, and power to each pi
    3. Power on each of the Raspberry Pis
    4. Enable ssh on each of the pis

Getting Started

Before we get started, we will need to connect the pis to the internet. Using the wireless connectivity options of the pis or an ethernet hub, connect the pis to the internet at this time. This can be done by simply plugging in the ethernet, or selecting an available Wi-Fi network. Note that Pis like to be finiky when there is no display output for the pi when it is turned on, so it is reccomended that each pi temporarily gets plugged in to a display when it gets powered on.

Ok now you have all of the pis on and connected to the internet, what now? Now you need to update your systems using these basic commands. Run this on EACH of the pis in the cluster:

sudo apt update
sudo apt upgrade

Next, you will want to intall MPICH, which allows the Pis to split tasks among multiple pis. Run this on EACH of the pis

sudo apt install mpich python3-mpi4py

Finally install another python library for mpi. Run this on EACH of the pis.

sudo apt install python3-pip python-dev-is-python3 libopenmpi-dev

Installs python and other resources needed to run tasks in parrellel

Setting IP addresses

Now that everything is installed, lets get working on setting IP addresses manually.

Using the command nmtui, click edit a connection, then select the wired ethernet connection.

Next, go down to the IPv4 Configuration section, and change the IP to a manual address by selecting automatic, then manual.

Finally, in the IPv4 Configuration section down to where it says addresses, and type in the desired address. Go to the bottom and select ok.

After exiting nmtui, run the commands sudo ifconfig eth0 down, then sudo ifconfig eth0 up

Getting IP addresses and usernames

For each pi, an IP address will need to be configured using the above method. Because of network segments, the IP addresses will need to be on the same network segments in order for this guide to work (If you dont know what that means look at the example file below). You can use the ifconfig command to get the IP address of the device.

Here is an example of what it should look like, the IP address is on line 2 after the “inet” keyword: https://raspberrytips.com/wp-content/uploads/2018/08/ifconfig.png</img>

Now, write down/remeber the IP of each of the Pi’s, you will need them for later. Save this to a file called ip_addrs on the master node.

touch ip_addrs

In the file, write down the IP’s on a separate line

Additionally, you will want to write down the users for which MPI will be used for. This is very important. Use the whoami command to get the username of the user. DO NOT USE ROOT, MPI WILL NOT WORK AS ROOT
This is the syntax for which you will write down the usernames and IP addresses:

user@IPAddress

Here is an example of what the file should look like in its final form:

pi@192.168.1.1
pi2@192.168.1.2
mpiGuy@192.168.1.3
timmy@192.168.1.4
etc...

Note, by default, MPI will attempt to use the same user across all nodes by default. Adding the user is optional, but allows for different users to be utilized for MPI.

Fun with SSH

From your master node you will want to create and copy your SSH key to each node. Follow this process. First, create a ssh key.

ssh-keygen

Do this from the master node, and copy the id to each of the pis. User stands for the user that you want to have mpi on. Do this on the master node for EACH PI

ssh-copy-id user@PI_IP

Next, back on the master node, run this command. USER is the user of the master node, and USER2 is the user of the slave node, and IPAddress is the IP address of the slave node:

scp /home/USER/.ssh/id_rsa.pub USER2@IPAddress:/home/USER2/master.pub

Finally, ssh into the user

ssh user@NODE_IP

and run these commands:

cat master.pub >> .ssh/authorized_keys
rm master.pub

Testing MPI

Alright, to test that MPI is working, lets run a simple command on the master node

 nano Hello.py

In the file, enter:

print("Hello World!")

Ctrl + x, then y, then copy the file to EACH OF THE SLAVE NODES:

scp Hello.py pi@NODE_IP:/home/USER/ </pre>
>**^COPY TO EACH PI IP ADDRESS**

Now, all you need to do is enter this command:
mpirun --hostfile ip_addrs python3 Hello.py
Thats it! Mpi is now working, we will dive further into MPI in a later guide.