Introduction: Raspberry Pi 3 "Bramble"

About: Management consultant by day, tech nerd by night!

Deploying an Apache2 webserver across a scalable Raspberry Pi3 Model B "bramble" via a HAProxy load balancer!

I do a lot of web development and have done a lot of reading about setting up geo-redundancies and load balancers, so I figured it was time I take a shot at setting it all up myself. I've had the displeasure of having a server go down and wanted to prevent that from happening again in the future!

Plus, it just looks cool.

Step 1: Getting Started

** UPDATE **

**************************************************************************************************************************

I actually made this Instructable a bit easier. I refactored the repo on Github to automatically install, configure and deploy the HAProxy loadbalancer on your chosen Pi! Less code, less editing, less chances for error and more chances for fun!

**************************************************************************************************************************

Head on over to https://www.raspberrypi.org/downloads/raspbian/ and download a fresh copy of Raspbian Stretch Lite.

Extract the .ZIP file and write the .img to each MicroSD card for each Pi in your cluster. OSX users, a great tool for this is https://etcher.io/

After you etch the .img to the MicroSD card, navigate to the card in a new Finder or File Explorer window - this should be named boot by default. On that, create a new filed named SSH. Make sure you don't put anything for a file extension. This will enable SSH on your Rpi3. Eject the card and place it into your Pi. Repeat this step for the remaining 2 Pi's (or however many you end up using).

Step 2: Setting Up Your Hardware

This example assumes a 3 node Rpi cluster, and the list of equipment is below:

  1. 5 port Ethernet Switch x 1
    1. https://www.amazon.ca/gp/product/B00QR6XFHQ/ref=oh...

  2. 5 port USB Power Adaptor x 1 **
    1. https://www.amazon.ca/gp/product/B017R9IJTU/ref=oh...

  3. Ethernet Cables x 4
    1. https://www.amazon.ca/gp/product/B01J8KFTB2/ref=oh...

  4. USB 2. to Micro USB B Power Cables x 3
    1. https://www.amazon.ca/gp/product/B019U0V75W/ref=oh...

  5. Raspberry Pi3 Model B x 3
    1. https://www.amazon.ca/gp/product/B01CD5VC92/ref=od...

  6. Heatsinks x 6
    1. https://www.amazon.ca/gp/product/B010ER7UN8/ref=od_aui_detailpages00?ie=UTF8&psc=1

* It's important that you use a USB Power Adaptor that is capable of supplying the Raspberry Pi's with their minimum required operating voltage.

  • Connect x1 Ethernet Cable from your router to the Ethernet Switch.
  • Connect x1 Ethernet Cable from your Ethernet Switch to each one of your Pi's
  • Connect x1 USB-to-MicroUSB from each of your Pi's to the USB Power Adaptor.
  • Plug it all in and look at the blinky lights

While not necessary, but I wanted to the bramble to look as cool as possible and keep everything a bit neater. I decided to pick up 3 of these stackable cases from Amazon. I didn't include it in this instructable as it's _technically_ not required, but for aesthetics points I'd recommend picking some up.

https://www.amazon.ca/gp/product/B07BNDFXN9/ref=oh...

** Bonus Points **

I opted to go for extra nerd points and wanted a better visual indication of which server I was interacting with. I had some spare protoboard laying around and a bunch of LED's and resistors, so I quickly hacked together some boards to lay over the pi's GPIO pins. Ideally, I would have used some female headers, but I only had male so I needed to trim up some jumper wires.

If you want to go that route as well (because LED's are awesome), you'll want to follow this tutorial at:

https://thepihut.com/blogs/raspberry-pi-tutorials/...

Step 3: Configure Your Pi's

You'll need to know the IP addresses of each of the Pi's on your local network. If you're a CLI ninja, this should be easy-peasy. For everyone else, you can use a free IP Scanner, such as SuperScan (OSX). Write the IP addresses down.

Next, copy your SSH public key to each of your Pi's by using the following command from your terminal:

ssh-copy-id

Example::

 ssh-copy-id -i ~/.ssh/id_rsa.pub pi@192.168.0.228 

Don't have an SSH key? No problem! Just run:

ssh-keygen 

in your terminal and follow the prompts. We're almost there!

Step 4: Install Ansible

If you've made it this far, congratulations! You're just a few minutes away from running your very own distributed computing network.

On your local computer / laptop, you'll want to install Ansible from the command line. For Mac users, it's:

sudo pip install ansible 

For everybody else, refer to https://docs.ansible.com/ansible/latest/installat... for your OS.

Now, you'll want to clone this repo into a folder OR download the .ZIP and extract it into a folder on your local machine.

<strong><a href="https://github.com/Jtilley84/ansible-apache2-webserver" rel="nofollow">https://github.com/Jtilley84/ansible-apache2-webse...</a></strong>

In that repo, you'll see a hosts.ini file. Open it up in your favourite text editor (or nano or vim):

 [loadbalancer] 
pi-headnode ansible_host=192.168.0.228  # <--- Change this to the ip address of the Pi that you want to HAProxy on.
[nodes] 
node2 ansible_host=192.168.0.16 # <--- Change this to the ip address of your second Pi 
node3 ansible_host=192.168.0.58 # <--- Change this to the ip address of your third Pi 

That's it! To run the playbook, navigate to the base repo folder and type the following in your terminal:

ansible-playbook playbook.yml

Step 5: Congratulations!!

You just made computer magic happen. Congratulations!

This is just a proof of concept. In this repo, the playbook pushes a unique index.html file to each of the nodes so that you can visually debug whether or not it is working. For a production server, you'd want to obviously edit the playbook to deploy your site.

If you have any questions or comments, I'd love to hear them! Please checkout the Github repo and fork away! I'd love to see what you come up with.

Microcontroller Contest

Participated in the
Microcontroller Contest