HAProxy is an open source proxy that can be used to increase availability and server as load balancing for a wide variety of web apps. It has been especially designed for projects that require high availability, so it is swift and predictable, HAProxy is based on a single process model.
HAProxy works by balancing user requests between two web servers; Server1 and Server2, if either of them freezes or becomes unavailable, all requests will be processed by the available server, and once the unavailable server comes back online, the load balancing will be done once again.
How to install HAProxy
HAProxy is available by default in most Linux distributions; On CentOS, Redhat, or Fedora, we can install it through the following command:
yum install haproxy
On Ubuntu, Debian, Linux Mint or Debian-derived distributions, the install command is:
apt-get install haproxy
Load balancer with HAPlox
In the example below, we will explain how to do load balacing by using a configuration file haproxy.conf. The IP addresses 192.168.0.2, 192.168.0.3, 192.168.0.4, 192.168.0.5, 192.168.0.6, 192.168.0.7 and 192.168.0.8 are the balancer nodes, the web server is 192.168.0.1 which is also the addres of the balancer, the one on which we receive user requests to our websites. We have named the nodes of this load balancer using the days of the week.
overall
log 192.168.0.1 local 0
maxconn 280000
user nobody
group nobody
nbproc 7 # Number of processors (physical)
pidfile /var/run/haproxy.pid
daemon
defaults
mode http
option httpclose
option abortonclose
option httplog
option dontlognull
option httpclose
retries 5
maxconn 150000
monitors / haproxy-ping
option redispatch
contimeout 3000
clitimeout 30000
srvtimeout 30000
listen HTTP_SERVERFARM 192.168.0.1:70
mode http
retries 3
option redispatch
maxconn 36000
contimeout 5000
clitimeout 50000
srvtimeout 50000
option http close
option forward for; this sets X-Forwarded-For
balance round robin; Load Balancing algorithm
stats enable
stats / balancer-status
monitors / hap- pxy-ping
option httpchk
server months 192.168.0.2:70 weight 1 maxconn 3000 check inter 160000
server Tuesday 192.168.0.3:70 weight 1 maxconn 3000 check inter 160000
server miecuri 192.168.0.4:70 weight 1 maxconn 3000 check inter 160000
server Thursday 192.168.0.5:70 weight 1 maxconn 3000 check inter 160000
server Friday 192.168.0.6:70 weight 1 maxconn 3000 check inter 160000
server Saturday 192.168.0.7:70 weight 1 maxconn 3000 check inter 160000
server Sunday 192.168.0.8:70 weight 1 maxconn 3000 check inter 160000
A software combination between Linux and HAProxy can replace a commercial load balancer which can cost an exorbitant amount.
If you want the latest version of HAProxy, run the following commands:
echo deb http://httpredir.debian.org/debian jessie-backports main | tee /etc/apt/sources.list.d/backports.list
apt-get install debian-keyring
Add the cle-gpg key:
gpg --keyring /usr/share/keyrings/debian-keyring.gpg --export [email protected] | apt-key add -
apt-get update
apt-get install haproxy-t jessie-backports-1.6
HAProxy activation
# activation of HA proxy in / etc / default / haproxy
sed -i "s / ENABLED = 0 / ENABLED = 1 /g"/etc/default/haproxy
#activation of HA Proxy in /etc/init.d/haproxy
sed -i "s / ENABLED = 0 / ENABLED = 1 / g" /etc/init.d/haproxy