-
Notifications
You must be signed in to change notification settings - Fork 0
The Pi Network
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.
Let's enumerate each Pi and outline the network configuration we want to achieve below.
Pi Number | Interface | IP Address | Hostname |
---|---|---|---|
1 | eth0 | 192.168.0.101 | master |
2 | eth0 | 192.168.0.102 | worker2 |
3 | eth0 | 192.168.0.103 | worker3 |
4 | eth0 | 192.168.0.104 | worker4 |
5 | eth0 | 192.168.0.105 | worker5 |
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 on the network switch.
Edit file /etc/dhcpcd.conf
and modify the following lines
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.
Edit file /etc/hostname
and replace raspberrypi
hostname with the new one for each Pi as specfied in the Network Lookup Table
sudo nano /etc/hostname
Reminder: You have to do this manually on each Raspberry Pi.
By changing the hostname to master
and workerX
you will be able to distinguish them without having to remember their IP addresses.
To map all Pi's IP addresses to it's new respective hostname, edit file /etc/hosts
and add the following:
192.168.0.101 master
192.168.0.102 worker2
192.168.0.103 worker3
192.168.0.104 worker4
192.168.0.105 worker5
Reminder: You have to do this 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.
You may ask what is SSH? Well, 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 and type in passwd
then hit Enter. Type in your new password and confirm it. You can use the same password for all Pi's or a custom one per Pi, up to you.
Open a terminal window and type in
sudo systemctl enable ssh
sudo systemctl start 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
With the hostname
mapped to each IP address on each Pi, 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 y
and hit Enter.
The problem here is that everytime you log in remotely to any other Pi, it will ask you for that Pi's password. Not cool!
To avoid typing in the password of the computer we're trying to remotely log in to, we create what is called a private and public key-pairs.
In theory, if you generate private/public key-pairs for computer A
, another computer B
without the public key of computer A
will not be able to log in or connect to that computer.
The private key is solely for the computer in which the key is being generated 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
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 TOUCH THE PRIVATE KEY!
Reminder: You have to generate keys manually on each Raspberry Pi.
Now, each public key will have to be appended to file ~/.ssh/authorized_keys
on every other Pi. The simplest way to do this is to generate all keys on each Pi then append each public key to the master node. Once the master has access to all public keys, you can copy (overwrite) this file to all workers.
First, on the master, we need to copy the master's public key to it's own authorized_keys
file
cat ~/.ssh/id_ed22519.pub >> ~/.ssh/authorized_keys
Next, run the following command on each worker
cat ~/.ssh/id_ed22519.pub | ssh pi@master 'cat >> ~/.ssh/authorized_keys'
Then, to replicate this work on all workers, simply copy over the master's authorized_keys
file to all workers
scp ~/.ssh/authorized_keys pi@workerX:~/.ssh/authorized_keys
Where X
represents the Pi Number in the Network Lookup Table.
You should now be able to log in remotely to any Pi on the network from any other Pi.
Upon completion of the previous three steps; assigning a static IP address, changing the hostname, and enabling SSH, we must reboot in order for these changes to take effect
sudo reboot
Reminder: You have to do this manually on each Raspberry Pi.