Setup

To start with launch two instances, we'll call them lb-1.example.com and lb-2.example.com. One will have a public IP and one won't (check the box when creating an instance that says "Private networking only - don't assign a public IPv4 address"), generate each one with a random password rather than SSH key (this will come in handy later!)

We'll use Ubuntu 16.04 XS instances, but you can likely use any OS, provided you know how to script it as the way we'll do this is very basic functionality in Linux.

load-balancer-initial-state.png

This shows a live IP address (185.136.235.217) but we'll use your.public.ip.address throughout the rest of the guide.

The plan

We will install something that will serve HTTP requests on both servers. We'll write a script on each instance that checks every minute if HTTP responds on the public IP, if not, switch it to the current machine using the API. You could choose to do this more frequently, you could do it on a separate (and therefore independent) monitoring instance, but this will work to demonstrate the IP address transfer API.

The script

Create a file on both machines called /usr/local/bin/ha-switch.sh that contains the following:

#!/bin/bash

CURRENT_INSTANCE_ID=94c65d21-e5f7-4462-a55e-91d549f889da
PUBLIC_IP=your.public.ip.address
API_TOKEN=abcdef0123456789abcdef0123456789

r=`wget -q http://$PUBLIC_IP`
if [ $? -ne 0 ]; then
  curl -H "Authorization: bearer $API_TOKEN" -X PUT \
    https://api.civo.com/v2/instances/$CURRENT_INSTANCE_ID/ip/$PUBLIC_IP
fi

This script sets up a few variables (you should adjust the three variables CURRENT_INSTANCE_ID (this should change to be the instance ID of each instance, it's in the URL of the instance's details page), PUBLIC_IP and API_TOKEN (from https://api.civo.com/ when logged in). It then requests the webpage over HTTP and if wget exits with an error condition then make the "Moving a public IP between instances" API call to switch the public IP to point to this instance instead.

Remember to finish by making the script executable with chmod +x /usr/local/bin/ha-switch.sh. On the second machine lb-2.example.com remember it currently doesn't have inbound access from the internet (because it currently has no public IP), so you should connect to it using ssh root@your.public.ip.address first then from that instance use ssh root@lb2.internal.ip.address to connect over the private network. Remember this is why we created them with passwords (unless you want to get in to SSH Agent Forwarding).

Running it regularly

This part is very easy, simply run crontab -e and add a line like this:

* * * * * /usr/local/bin/ha-switch.sh

Getting the website up and running

Normally at this point, you'd install and configure your web site (or already have it running), but for now we'll just install Apache on both machines using:

apt update && apt install -y apache2

And you should then be able to hit http://your.public.ip.address/ in a browser and see a standard apache page:

Screen Shot 2017-05-12 at 13.39.21.png

The test

Now let's simulate a failure. lb-1.example.com has the public IP, so SSH in to that machine and run the command:

service apache2 stop

Apache will go down and within a minute the control panel will reflect that the IP address has switched over to the lb-2.example.com instance and HTTP traffic is being served again.