Load balancer distribute workload and improve website and application performance.
load balancer distribute incomming application traffic across multiple backend(target).load balancer also called as Frontend or reverse proxy.
HAproxy is one of the product of load balancer, HAproxy is a high-performance, open source load balancer & reverse proxy for HTTP and TCP .
- Set-up:
For Loadbalancer setup I launched 4 AWS EC2 Instances One Instance make Load Balancer(Frontend),and remaining three make Backend, In my case named as Backend-1 (ip-172-31-44-4) ,Backend-2 (ip-172-31-33-138),Backend-3(ip-172-31-40-19).
On three Backend instances, configure following same set-up as it is on each backend nodes:
-
Install httpd php software(Apache):
yum install httpd php -y
-
DocumentRoot for "Apache server is /var/www/htmt" Create here one code folder which is access for client. here put Webpage named as index.php:
cd /var/www/html
Create index.html Webpage:
vim index.php
code:-
<pre>
<?php
print_r($_SERVER);
?>
</pre>
-
Start the httpd service:
systemctl enable httpd --now
-
Install HAproxy load balancer in frontend instance.
yum install haproxy -y
-
Registration of Backend to Load Balancer, HAproxy config file "/etc/haproxy/haproxy.cfg" where add backend node and also add port number which can helps to client to access the Application. for this do follow setup:
vim /etc/haproxy/haproxy.cfg
ADD this code in config file:
- Note:
here we use Round Robin Algorithm that work as client will connect to web server through Load balancer , 1st connect to web 1 then web 2 and next web 3 then again go to 1. here also bind the port no. as 8080 .
-
Start HAproxy service and check status:
systemctl restart haproxy systemctl start haproxy systemctl status haproxy
-
Check HAproxy config file is valid or not:
haproxy -f haproxy.cfg -c
Now loadbalancer configuration is done.
Load Balancer(Frontend) instance change Inbound rule - allow all traffic Anywhere
Check Load balancer on browser:
Public IP of LB + port no. (http://65.0.18.65:8080)
Also on local command prompt -->>(curl http://65.0.18.65:8080)