HAProxy is a fast and useful tool for high availability and load balancing that can work with TCP and HTTP-based applications. Most websites require very high uptime, which isn’t possible with a single server setup. Therefore, it is necessary to set up a high-availability environment that can be managed with a single server failure.
Following are the steps to install HAProxy on Centos, after which the Load Balancer will transfer the request to the web server and distribute the traffic to balance the load.
1. Install HAProxy using this command –
# sudo yum install haproxy
2. Configure the HAProxy Configuration file as per the requirement.
It is located at /etc/haproxy; you can use your preferred editor to modify the haproxy.cfg
# nano /etc/haproxy/haproxy.cfg
global log 127.0.0.1 local0 log 127.0.0.1 local1 debug maxconn 45000 # Total Max Connections. daemon nbproc 1 # Number of processing cores. defaults timeout server 86400000 timeout connect 86400000 timeout client 86400000 timeout queue 1000s # [HTTP Site Configuration] listen http_web 192.168.1.10:80 mode http balance roundrobin # Load Balancing algorithm option httpchk option forwardfor server server1 192.168.1.10:80 weight 1 maxconn 512 check server server2 192.168.1.10:80 weight 1 maxconn 512 check # [HTTPS Site Configuration] listen https_web 192.168.1.10:443 mode tcp balance source# Load Balancing algorithm reqadd X-Forwarded-Proto: http server server1 192.168.1.101:443 weight 1 maxconn 512 check server server2 192.168.1.102:443 weight 1 maxconn 512 check
You can modify the IP Address in the configuration file as per your network setup.
Inside the HTTP Site Configuration, if any request is received on port 80 of IP Address 192.168.1.10, it will redirect the request to Port 80 of 192.168.1.101 or 192.168.10.102 to balance the server load.
The same thing will work for the https with port 443. If any request comes on port 443 of IP Address 192.168.1.10, it will redirect the request to the Port 443 of 192.168.1.101 or 192.168.10.102 to balance the server load.
You can also modify the configuration file further as per your requirement.
- nbproc (value) # Number of processing cores.
- mode (value) # 'http' for http website and ‘tcp’ for https website
- balance (value) # Type of load balancing like ‘source’, ’roundrobin’ etc
3. Execute the below-given command to start and enable the haproxy –
# systemctl start haproxy
# systemctl enable haproxy
This is it!
You have installed and configured the HAProxy service.