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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF