HAProxy is very fast and dependable solution that offers high availability, load balancing, and proxying for TCP and HTTP-based applications. It is ideally suited for web sites very high traffic.
As a pre-requirement for the best results, you should have a minimum of two web servers and a server for the load balancer. The web servers need to be running at least the basic web service such as nginx or httpd to test out the load balancing between them.
Note that the firewall on CentOS 7 is restrictive for by default for the setup.
Whether you are using built-in, iptables, csf/apf type of firewall please open port 8181 TCP. We will use firewall-cmd now:
Enter the commands below to permit the required services and reload the firewall.
# firewall-cmd --permanent --zone=public --add-service=http # firewall-cmd --permanent --zone=public --add-port=8181/tcp # firewall-cmd --reload
Note: If you don't have firewalld installed you can install it with yum install firewalld then service firewalld restart and proceed with the commands above. Otherwise, use iptables as stated earlier.
2. To check current iptables rules (the output below show no iptables rules set).
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
Deploying your Load balancing server
Configure the load balancer
Create a configuration file /etc/haproxy/haproxy.cfg
which contains the required settings and configurations.
# vi /etc/haproxy/haproxy.cfg
Enter the following into the file:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back
backend http_back
balance roundrobin
server your_server1 private_IP:80 check
server your_server2 private_IP:80 check
Ensure to save the file before closing it.
Next, restart Haproxy using the command below:
# systemctl restart haproxy
# systemctl enable haproxy
If you get any errors or warnings at startup, ensure that there are no typographical errors in configuration and all the required files and folders are created, then restart.