HAProxy is a very fast and decent tool to achieve high availability, load balancing. It can work with TCP and HTTP-based applications. Most of the websites require very high uptime for their websites, which can't be possible with a single server setup. It is required to set up high availability environment which can be managed with single server failure.
Following are the steps to install HAProxy on Centos. Load Balancer will transfer the request to the webserver and distribute the traffic to balance the load.
- Install HAProxy using the below command.
# sudo yum install haproxy
- Configure HAProxy Configuration file as per the requirement. It is located at /etc/haproxy. You can use your favorite 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 HTTP Site Configuration, if any request 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. 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
- Hit the below command to start and enable the haproxy.
# systemctl start haproxy
# systemctl enable haproxy
This is it. You have installed and configured HAProxy service.