-
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 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 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 change the hostname manually on each Raspberry Pi.
By changing the hostname to master
and workerX
you will be able to distinguish them.
To map all Pi's IP addresses to it's new respective hostname, edit file /etc/hosts
and add the following:
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
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.
Reboot system for changes to take effect
shutdown -r now
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
- 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 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 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 twice.
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 Pi.
The simplest way to accomplish this is to generate all keys on each Pi (including the master) then append each public key to the master node. Once the master has access to all public keys, including it's own, 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'
Enter the password when prompted.
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.