Bobcares

AWS EC2 Keepalived HAProxy | Set Up Guide

by | Nov 8, 2023

Learn how to use HAProxy with Keepalived and AWS EC2. Our AWS Support team is here to help you with your questions and concerns.

AWS EC2 Keepalived HAProxy | Set Up Guide

Keepalived is a routing software, while HAProxy is an open-source load balancer or reverse proxy.

Today, we are going to take a look at how to use HAProxy with Keepalived. So, we will be creating two HAProxy EC2 instances. Here, we will be turning off the master instance and making sure the second instance takes over.

1. Prepare two EC2 Instances

To begin with, we have to have 2 EC2 instances up and running.

In this example, we will call the main EC2 instance Tom and our secondary EC2 instance Jerry.

We can use the Amazon Linux AMI images to create both instances.

Our experts would like to point out that we have to create a role for both instances that includes permissions to perform changes to EC2 instances.

2. Install HAProxy

Then, it is time to install HAProxy on both servers.

yum install haproxy

We can use the configuration file in /etc/haproxy/haproxy.cfg

Although the actual varnish backend is non-existent, we can make sure we have a proper backend configured.
It is essential to have a 503 error file configured so that it is easy to check if our failover works.

For example, /etc/haproxy/errorfiles/503.http :

AWS EC2 Keepalived HAProxy | Set Up Guide

We have to change {servername} to the server name for the failover to work.

3. Configure HAProxy to start on boot

Now, we are going to configure HAProxy to start on boot:

chkconfig haproxy on
service haproxy start

Once we access both HAProxy instances via the browser, we will see the error pages we configured.

4. Attach Elastic IP to our main server

Next, we have to attach an Elastic IP to our main server (Tom). Then, we can configure it in the DNS. Now, when we visit the URL or IP, we will see a welcome message.

5. Install keepalived

At this point, we have to install Keepalived on both servers.

Keepalived keeps track of which server is the master server and when a failover has to occur.

yum install keepalived

Since in our example Tom is the main server, we have to let keepalived know that Tom is the master.

Furthermore, we have to set the unicast_src_ip to the private IP of the current server. In other words set the unicat_peer to the private IP of the Jerry server. Jerry is our secondary server.

Hence, we let Keepalived know that Jerry is the backup server.

The config file /etc/keepalived/keepalived.conf will look like this:

vrrp_script check_haproxy
{
script "pidof haproxy"
interval 5
fall 2
rise 2
}
vrrp_instance VI_1
{
debug 2
interface eth0
state MASTER
virtual_router_id 1
priority 110
unicast_src_ip 10.0.1.131
unicast_peer
{
10.0.1.42
}
track_script
{
check_haproxy
}
notify_master /etc/keepalived/failover.sh
}

Furthermore, set unicast_src_ip to the private IP of the current server. So, set the unicat_peer to the secondary server’s (Jerry’s) private IP.

The config file /etc/keepalived/keepalived.conf looks like this.


vrrp_script check_haproxy
{
script "pidof haproxy"
interval 5
fall 2
rise 2
}
vrrp_instance VI_1
{
debug 2
interface eth0
state BACKUP
virtual_router_id 1
priority 100
unicast_src_ip 10.0.1.42
unicast_peer
{
10.0.1.131
}
track_script
{
check_haproxy
}
notify_master /etc/keepalived/failover.sh
}

Additionally, set the unicast_src_ip to the private IP of the current server. So, set the unicat_peer to the Tom’s private IP.

Then, create the /etc/keepalived/failover.sh on both servers. We have to ensure that the instance_id has the instance ID of the other server.


#!/bin/bash
EIP=52.212.151.17
INSTANCE_ID=i-0bdd8a68eb573fd1a
/usr/bin/aws ec2 disassociate-address --public-ip $EIP
/usr/bin/aws ec2 associate-address --public-ip $EIP --instance-id $INSTANCE_ID

Then, make this file executable.

chmod 700 master.sh

Next, let’s configure Keepalived to start during booting:

chkconfig keepalived on
service keepalived start

6. Test your setup

Then run the following command:

tail -f /var/log/messages

Now, we will see the following message appear at Tom:

Keepalived_vrrp[1196]: VRRP_Instance(VI_1) Entering MASTER STATE

On the other hand, the backup server has the following message:

Keepalived_vrrp[784]: VRRP_Instance(VI_1) Entering BACKUP STATE

In this case, we are all set up. When we call the Elastic IP in our browser, we will see the welcome message from the master server.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to use HAProxy with Keepalived and AWS EC2.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.