Background / Purpose / Getting Started / Cluster Guide
The so called Local Area Network (LAN). In order for the cluster to communicate, we need to set up a LAN switch so that each Pi board knows where to send and receive data, commands, and output results.
- Network Lookup Table
- Assign a static IP address
- Change Hostname
- Enable Secure Shell (SSH)
- Remote Log-in
Let's enumerate each Pi and outline the network configuration for reference.
Pi Number | Interface | IP Address | Hostname |
---|---|---|---|
0 | eth0 | 192.168.0.100 | master |
1 | eth0 | 192.168.0.101 | worker1 |
2 | eth0 | 192.168.0.102 | worker2 |
3 | eth0 | 192.168.0.103 | worker3 |
4 | eth0 | 192.168.0.104 | worker4 |
You guessed right! There is a "master" node that will oversee and control the cluster while each "worker" executes whatever command the master issued.
To facilitate communication between the cluster, let's assign a static local IP address to each Raspberry Pi board in the network switch.
Edit file /etc/dhcpcd.conf
sudo nano /etc/dhcpcd.conf
Uncomment the following lines and add X
interface eth0
static ip_address=192.168.0.10X/24
Where X
represents the Pi Number in the Network Lookup Table.
Reminder: You have to edit this file manually on each Raspberry Pi.
Raspbian by default, sets the hostname
of each Pi to raspberrypi
. Such hostname is not mapped to the IP address given it can change via DHCP but since we assigned a static IP address we can map it. Mapping the hostname to the static IP will ease communication between the network.
By changing the hostname to master
and workerX
you will be able to distinguish between the nodes.
Edit file /etc/hostname
and replace raspberrypi
with each the Pi's hostname
sudo nano /etc/hostname
Each Pi's hostname is specified in the Network Lookup Table.
Reminder: You have to change the hostname manually on each Raspberry Pi.
All of your Raspberry Pi's must have each of their IP addresses mapped to their hostnames in the /etc/hosts
file so that each Pi can fully communicate with each other.
Edit file /etc/hosts
and add the following:
# pi-cluster network
192.168.0.100 master
192.168.0.101 worker1
192.168.0.102 worker2
192.168.0.103 worker3
192.168.0.104 worker4
Reboot system for changes to take effect
shutdown -r now
Reminder: You have to do this manually on each Raspberry Pi.
SSH is a network protocol for operating network services securely such as log into a remote computer, execute commands, tunneling, forwarding TCP ports and transfer files (via secure copy or scp).
We need SSH for each Pi to communicate with each other and be able to execute commands coming from the Master node.
SSH is disabled by default as of latest Raspbian versions. Thus, we need to enable it but first, you must change your default password.
To change your default password, simply open a terminal window, type in passwd
, press enter and follow the prompts. We suggest something easy while configuring the cluster, we will discuss Cluster Security measurements later.
Open a terminal window and type in
sudo systemctl enable ssh
- Go to
Preferences
menu and openRaspberry Pi Configuration
- Head over to the
Interfaces
tab - Select
Enabled
right next toSSH
- Click
OK
- Start the service
- Open a terminal window and type in
sudo raspi-config
- Select
Interfacing Options
- Navigate to
SSH
and select it - Choose
Yes
- Select
Ok
- Choose
Finish
To start up the SSH service, run the following command
sudo systemctl start ssh
With the hostname
mapped to each IP address on each Pi and SSH enabled, remote login becomes easier
ssh pi@worker3
If that is your first time remotely logging in to worker3
, it will ask you if you trust the destination, type in yes
and press enter.
The problem here is that every time you log in remotely to any other Pi, it will ask you for that Pi's password.
To avoid typing in the password of the computer we're trying to remotely log in to, we create what is called private and public key-pairs.
The private key is solely for the computer in which the key is being generated (not to be shared) while the public key can be shared among other computers for these to be able to securely login remotely without entering a password.
To generate private/public keys, run this command
ssh-keygen -t ed25519
Press Enter three (3) times.
That will generate a private and public key-pair within the ~/.ssh
hidden directory. The private key is stored in file ~/.ssh/id_ed25519
and the public key is in file ~/.ssh/id_ed25519.pub
. DO NOT modify or share the private key file!
- Login to master node and generate keys for it
ssh-keygen -t ed25519
Press Enter three (3) times.
- Append master's public key to its own SSH authorized keys file
cat ~/.ssh/id_ed22519.pub >> ~/.ssh/authorized_keys
- Login to worker
X
node and generate keys for it
ssh-keygen -t ed25519
Press Enter three (3) times.
- Append worker
X
's public key to the master's SSH authorized keys file
cat ~/.ssh/id_ed22519.pub | ssh pi@master 'cat >> ~/.ssh/authorized_keys'
Enter the master
's password if prompted.
Note: If the destination username (master's username) is different from pi
, change it accordingly in the command above.
- Repeat steps 3-4 for each worker specified in the Network Lookup Table
- Login to master node
- Overwrite the master's SSH authorized keys file to each worker
X
scp ~/.ssh/authorized_keys pi@workerX:~/.ssh/authorized_keys
Where X
represents the Pi Number in the Network Lookup Table.
Note: If the destination username (master's username) is different from pi
, change it accordingly in the command above.
- Repeat step 6 for each worker specified in the Network Lookup Table
- Done
You should now be able to log in remotely to any Pi on the network from any other Pi.