Skip to main content

HAPROXY LOADBALENCER FOR HTTPD ON CENTOS 7


HAProxy(High Availability Proxy) is an open-source load-balancer whisch is is free,fast and reliable. It offers load-balancing, high-availability, and proxying for TCP and HTTP-based applications.Here we are using it for loadbalencing httpd.Here we are configuring haproxy server assuming that we have httpd installed on our two nodes.

First install the dependencies
#yum install gcc pcre-static pcre-devel -y
Download the ha-proxy package
#wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.gz
Extract it
#tar -xvzf haproxy.tar.gz
#cd haproxy-1.7.8
then ensure you compile the program for your system.
#make TARGET=linux2628
#make install
Setting up HAProxy for your server
Create the following directories
#mkdir -p /etc/haproxy
#mkdir -p /run/haproxy
#mkdir -p /var/lib/haproxy
#touch /var/lib/haproxy/stats

Make a symbolic link so the binary will allow you to run HAProxy commands as a normal user.
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

copy the init file
cp ~/haproxy-1.7.8/examples/haproxy.init /etc/init.d/haproxy

Change the permission and reload sysytemd
#chmod 755 /etc/init.d/haproxy
#systemctl daemon-reload

Add haproxy user
#useradd -r haproxy

Configuring load balancing for layer 4
#vi /etc/haproxy/haproxy.cfg



  Different load balancing algorithms
Roundrobin: Every server is gonna be used in turns according to their weights. This is considered the smoothest and fairest algorithm with the servers processing time remains equally distributed. While this algorithm is dynamic, it allows server weights to be adjusted on the fly.

Leastconn: This algorithm will usually go with the server which has the lowest number of connections. While Round-robin is performed between a couple of servers with the same load. This algorithm is a good choice for long sessions, such as LDAP, SQL, TSE, and others. But not so commonly used for short sessions like HTTP.

First: The first server which has available connection slots will get the connection. The servers are gonna be chosen from the lowest numeric identifier to the highest, which will end up defaulting to the server’s position on the farm. Once a server has reached the maxconn value, the next server will be used.

Source: The source IP address will be hashed and divided because of the total weight of the running servers to point out which server will have the request. This will have the same client IP address which will always reach the same server while the servers are gonna be staying the same

Now restart the service
#/etc/init.d/haproxy restart
Now from browser access
Your <load balencer server ip>
To know the stats
Your <load balencer server ip>/haproxy?stats


Configuring load balancing for layer 7
 


#vi /etc/haproxy/haproxy.cfg
frontend http_front
 bind *:80
 stats uri /haproxy?stats
 acl url_blog path_beg /blog
 use_backend blog_back if url_blog
 default_backend http_back

backend http_back
 balance roundrobin
 server <server name> <private IP>:80 check
 server <server name> <private IP>:80 check

backend blog_back
 server <server name> <private IP>:80 check

Restart the service
#/etc/init.d/haproxy start

Password protection the statistics page

To have the statistics page simply listed at the front end, is gonna be publicly open for anyone to view, in most cases will not really be a good idea. So instead, you can set it up to its own port number by adding the following example to the end of your haproxy.cfg file. Replace the username and password with something good and secure.

open haproxy.cfg file and make the following entries

listen stats
 bind *:8181
 stats enable
 stats uri /
 stats realm Haproxy\ Statistics
 stats auth username:password

Now restart the service
#/etc/init.d/haproxy restart

Now from browser type
http://<load balancer public IP>:8181
It will ask you for password