Setting up a Raspberry Pi Cluster
Tags: raspberrypi, ssh
Published: 2021-01-12 16:56:11

---
title: Setting up a Raspberry Pi Cluster
published: true
date: 2021-01-12 05:31:08 UTC
tags: raspberrypi, ssh
canonical_url: https://mikebobadilla.com/setting-up-a-raspberry-pi-cluster/
---

For whatever reason, in my head, running software on a raspberry pi is a lot cooler than running it on my macbook. I have one running [pi-hole](https://pi-hole.net/) and a couple others just lying around, so I thought of something to use them for. I have a couple web scrapers running for whenarethefights.com on lambda and thought it would be nice to move those to a pi-cluster.

<!--kg-card-begin: markdown-->

I already had 3 pis so here was my shopping list:

- [Rack](https://amzn.to/3bDEoFY)
- [Switch](https://amzn.to/2XwDfrJ)
- [Ethernet Cables](https://amzn.to/3qcEg4m)
- [microSD Cards](https://amzn.to/2Lfamhj)

<!--kg-card-end: markdown-->

### Installing the OS

So after I got everything I just need to flash each sd card with an OS. A list of them can be [found here](https://www.raspberrypi.org/software/operating-systems/). I chose Ubuntu Server for no reason other than I have used it before on [Digital Ocean](https://m.do.co/c/aacdd15c8d35). To flash each card I used [Etcher](https://www.balena.io/etcher/). Its pretty straight forward and keeps me from having to use dd. I knew I wanted to run these in a headless mode so I need to place an empty file named `ssh` on each drive after they were done flashing. To do this you will need to re-mount the drive after Etcher ejects it; The easiest way to do that on Mac is to use the disk utility.

<!--kg-card-begin: image-->

![](https://mikebobadilla.com/content/images/2021/01/image-6.png)<figcaption>This is the disk utility panel</figcaption>

<!--kg-card-end: image-->

<!--kg-card-begin: markdown-->

Once mounted, you can open up the terminal to place the empty file.

```
cd /Volumes
# find the drive with the rpi OS
cd <OS Drive>
touch ssh
cd
```

Unmount the drive and repeat for each drive you have

<!--kg-card-end: markdown-->

### Setting up each Raspberry Pi

<!--kg-card-begin: markdown-->

I like to set one pi up at a time because each pi sets its hostname as ubuntu and I don't want to arp for the ip of each one and guess. My plan was to name each pi green-pi, white-pi, etc according to whatever color ethernet cable is plugged into it. Load all the sd cards but only plug one of the pi in and wait a min. You should now be able to ssh into the first pi.

```
ssh ubuntu@ubuntu

# When asked for a password, enter 'ubuntu'. You will be asked to change it to a new one.
```

Now we need to change the hostname and restart so our dnsserver picks up the new name.

```
hostnamectl set-hostname green-pi
sudo reboot
```

If you try to ssh to `ubuntu` again it won't let you since the ssh keys changed, so you will need to delete `ubuntu` from known users. Open up `~/.ssh/known_hosts` and delete the line with `ubuntu` on it.

```
vim ~/.ssh/known_hosts
ubuntu,192.168.0.88 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGGRsJ...
```

Repeat for each pi.

<!--kg-card-end: markdown-->

### Log into each Pi

<!--kg-card-begin: markdown-->

So now that they are all set up, we need to log into each one so we know we can. Since the hostname was updated we should be able to use the new hostname.

```
ssh ubuntu@green-pi

# Enter the new password you created
```

Once we know that works, let's copy our keys over so we don't need to enter a password anymore.

```
ssh-copy-id ubuntu@green-pi

# Enter the password again. When it's successful it will log you out and tell you to login again.

ssh ubuntu@green-pi
```

You should be able to login without a password now.

<!--kg-card-end: markdown-->

### Setup your ssh config

<!--kg-card-begin: markdown-->

Less typing is always better for me so the final step is to set our ssh config so I don't have to type the username anymore. Open up ~/.ssh/config and place the following:

```
Host green-pi
  HostName green-pi
  User ubuntu

Host white-pi
  HostName white-pi
  User ubuntu

Host blue-pi
  HostName blue-pi
  User ubuntu
```

After adding that you can just ssh with the hostname and be done.

```
ssh green-pi
```

Now that the pi cluster is set up it can be used to do anything. We can do all the buzzword things like Blockchain, Kubernetes, crypto mining.

Just kidding, we're just gonna put some web scrapers on them.

<!--kg-card-end: markdown-->

<!--kg-card-begin: image-->

![Pictured - a man at a computer disguised as an anonymous hacker wearing a Guy Fawkes mask.](https://images.unsplash.com/photo-1562813733-b31f71025d54?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MXwxMTc3M3wwfDF8c2VhcmNofDV8fGhhY2tlcnxlbnwwfHx8&ixlib=rb-1.2.1&q=80&w=1080)<figcaption>Photo by <a href="https://unsplash.com/@cbpsc1?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Clint Patterson</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></figcaption>

<!--kg-card-end: image-->

Current word count: ~1681 / 250,000  
Daily average: ~560