DevopsFault tolerance using HAProxy and AWS

March 22, 2021by Grep & Awk0

Fault tolerance using HAProxy and AWS

Introduction: In this tutorial we will learn how to configure HAProxy as Layer 7 (Application) Load Balancer. Load balancing can improve the performance, availability and resilience of your environment. HAProxy is also used as reverse proxy which acts as first level of defence.


We require 3 ec2 instance/server.

Two ec2 instance acts as web server

One for HAProxy.

I am using amazon ec2 instance. Thanks to #AWS.


Why do we want Load Balancer?

Let’s assume that we have an e-commerce application which is running on single host and it’s been accessed by many user across the world.

What If the server goes down? all the users going to be impacted and resulting in loss of business. Isn’t it embarrassing?

How we can overcome this problem?

By placing the web servers behind the load balancer and load balancer will take care of distributing the traffic among the servers based on availability of the server. The load balancer will do the regular health check of server and forward the request only if it register as healthy. By doing this we can improve the performance and high availability of application.

Let’s balance the Load.

Navigate to #aws management console and launch 3 ec2 instance.

  1. Login to first ec2 instance.

It uses key based authentication.

Default user name: ec2-user

  1. Run the below command to setup http web server.

yum install httpd –y # This will install the package

service httpd start  #To start the service

cd /var/www/html #Document root

  1. echo “<h1> My name is Load balancer and I distribute traffic :Server 1</h1>” > index.html # creating a simple  web page


Note: Make sure to allow the inbound traffic over port 80 in the security group(This is the default port for http web server).

Connect to second ec2 instance and repeat the above step. Replace server 1 with server 2 in index.html file (This is used for identification purpose).

  1. Copy the public DNS of both the server and paste it in the browser.
  2. Yeah!! Both My web server is up and running.Let’s configure  a HAProxy load Balancer now.Login to third ec2-instance and run the below command to install and configure haproxy.
    1. yum install haproxy –y
    2. service haproxy start
    3. cd /etc/haproxy/

    Add the below lines in haproxy.cfg file.

    frontend haproxy_inbound

               bind *:80

               default_backend haproxy_httpd

    backend haproxy_httpd

               balance roundrobin

               server server1 #Replace the dns name with dns/public ip of your server

    server server2

    4.service haproxy restart

    Copy the dns of load balancer and paste it in browser

  3. The load balancer will distribute the traffic among all the servers.

    Conclusion: You should have load balancer configured and running. You can modify the configuration file according to your requirement. This is the simplest of simplest load balancer you see. Isn’t it so simple?

Leave a Reply

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