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.
- Login to first ec2 instance.
It uses key based authentication.
Default user name: ec2-user
- 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
- 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).
- Copy the public DNS of both the server and paste it in the browser.
- 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.
- yum install haproxy –y
- service haproxy start
- cd /etc/haproxy/
Add the below lines in haproxy.cfg file.
server server1 ec2-18-221-162-88.us-east-2.compute.amazonaws.com #Replace the dns name with dns/public ip of your server
server server2 ec2-13-59-10-88.us-east-2.compute.amazonaws.com
4.service haproxy restart
Copy the dns of load balancer and paste it in browser
- 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?