Prepare EC2 Instances

First, you have to make sure you have 2 or more up and running AWS EC2 instances.
Let’s call our main EC2 instance Boyega and our secondary EC2 instance Bumaye. Also, make sure to also create a role for both with permissions to perform changes to EC2 instances.

Install HAProxy

You need to install HAProxy on both servers. You can do so by using the link below to compile and then Install HAProxy if you are using Amazon Linux 2

How to Install HAProxy in Amazon Linux 2

Once done installing then change the following configuration file in /etc/haproxy/haproxy.cfg to

The actual varnish backend does not exist. In your own environment, you would make sure of course that you have a proper backend configured. What is important for now is that we configured a 503 error file as an easy check to see if our failover works.

Make sure the file /etc/haproxy/errorfiles/503.http exists with the following content.

In order to see that the failover works, make sure to change the text of {servername} to the name of the server. This way we can show on which server we are currently running.

Let’s configure HAProxy to start on boot and to make sure it’s up and running.

If you access both HAProxy instances through your browser, you will see the error pages you configured.

Elastic Network Interface

Our next step is to create an ENI with a static ipv4 in AWS

Install keepalived

Next up, we have to install Keepalived on both servers.
Keepalived will keep track of which server is currently the master server and when a failover should occur.

Boyega is our main server. Therefore we tell keepalived that this is the master.

Make sure that the config file /etc/keepalived/keepalived.conf looks like this.

Make sure to set the unicast_src_ip to the private IP of the current server. Set the unicat_peer to the private IP of the Bumaye server.

Bumaye is our secondary server. Therefore we tell Keepalived that this is the backup server. Make sure that the config file /etc/keepalived/keepalived.conf looks like this.

Make sure to set the unicast_src_ip to the private IP of the current server. Set the unicat_peer to the private IP of the Boyega server or your secondary/backup server.

Next, create the /etc/keepalived/failover.sh on both servers. Make sure that the ENI_IP_CREATED has the ipv4 IP of the AWS ENI you created.

Please note, the subnet you created the AWS ENI in must be the same subnet as your AWS Instances.

Make sure the file is executable.

Let’s configure Keepalived to start on boot and to make sure it’s up and running.

Test your setup

If you do a tail -f /var/log/messages you will see the following message appear at Boyega.

Whereas Bumaye has the following message.

If this is the case, you should be good to go. If you now call the AWS ENI IP in your browser, you would see the welcomes message from Boyega.

Now stop HAProxy on Boyega.

Reload your browser. The message of Bumaye should appear.

You have now created a failover for HAProxy.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Want to Get in Touch?

Spelling error report

The following text will be sent to our editors: