How to setup a loadbalancer with HAProxy in RedHat 7

You have many options to install and configure a loadbalancer in RedHat 7. One of the best options is HAPorxy.

To setup HAProxy, you have to download related RPM file and then install it:

rpm -ivh haproxy.rpm

Then, you have to make changes in config file (/etc/haproxy/haproxy.conf):

in this sample we have 4 servers as below:

  • APP1: 192.168.1.2
  • APP2: 192.168.1.3
  • APP3: 192.168.1.4
  • LoadBalancer: 192.168.1.5
global
	log         127.0.0.1:514 local2 debug
	chroot      /var/lib/haproxy
	pidfile     /var/run/haproxy.pid
	maxconn     4000
	user        haproxy
	group       haproxy
	daemon
	maxconn    10000
	stats socket /var/lib/haproxy/stats
	tune.ssl.default-dh-param	2048
	
listen stats
	bind *:4331
	mode http
	log global
	maxconn 10
	timeout client 100s
	timeout server 100s
	timeout connect 100s
	timeout queue 100s
	stats refresh 30s
	stats enable
	stats uri /haproxy?stats
	stats realm HAProxy\ Statistics
	stats auth admin:123456
	stats show-node

	
frontend public
	bind            *:80 name clear
	redirect scheme https code 301 if !{ ssl_fc }
	bind            *:443 ssl crt /etc/pki/tls/certs/app_test.pem
	mode            http
	log         127.0.0.1:514 local3 debug
	log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
	http-request set-log-level debug
	option			tcplog
	option          httplog
	option          dontlognull
	monitor-uri     /monitoruri
	maxconn         8000
	timeout client  30s
	default_backend app_test

backend app_test
	mode            http
	log         127.0.0.1:514 local4 debug
	option			tcplog
	option          httplog
	option          dontlognull
	log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
	maxconn    10000
	balance roundrobin
	cookie SERVERID insert indirect nocache
	option prefer-last-server
	option			forwardfor
	retries         2
	option redispatch
	timeout connect 50s
	timeout server  50s
	server          s1 192.168.1.2:80   check cookie s1
	server          s2 192.167.1.3:80   check cookie s2
	server          s3 192.167.1.4:80   check cookie s3

Then you have to start the haproxy and make it autostart on reboots with these two commands:

systemctl start haproxy.service
systemctl enable haproxy.service

you can see the statistics page with this url:

http://192.168.1.5:4331/haproxy?stats

and your application can be accessed via the IP of this server.

Sometimes you encountered the following error:

haproxy Starting proxy stats: cannot bind socket [0.0.0.0:4331]

In order to solve this error, you have to run this command:

setsebool -P haproxy_connect_any=1

to see the system log:

tail -f /var/log/haproxy.log

to see the access log:

tail -f /var/log/haproxy-access-frontend.log

As you can see, you have to install rsyslog on your server, to do so, run these commands:

yum install -y rsyslog
systemctl enable rsyslogd
systemctl start rsyslog

After that, you should config rsyslog to gather HAProxy’s logs. So you have to edit /etc/rsyslog.conf file and add these lines:

local2.*  /var/log/haproxy.log
local3.*  /var/log/haproxy-access-frontend.log
local4.*  /var/log/haproxy-access-backend.log

after that a new file in /etc/rsyslog.d/ should be created as below:

vi /etc/rsyslog.d/haproxy.conf
[root@s1vlmizbanlvs01 haproxy_reports]# cat /etc/rsyslog.d/haproxy.conf
# Create an additional socket in haproxy's chroot in order to allow logging via
# /dev/log to chroot'ed HAProxy processes
#$AddUnixListenSocket /var/lib/haproxy/dev/log

# Send HAProxy messages to a dedicated logfile
#if $programname startswith 'haproxy' then /var/log/haproxy.log
#&~
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514
local2.* /var/log/haproxy.log
local3.* /var/log/haproxy-access-frontend.log
local4.* /var/log/haproxy-access-backend.log

then, you have to restart the service:

systemctl restart rsyslogd