In this topic we will learn how to install and configure HAProxy 1.4.24 in CentOS 6.
HAproxy is widely used Open Source tcp/http based loadbalancer.And many renowned company also using the HAProxy.Here in this tutorial we will install the latest stable HAProxy version 1.4.24 in CentOS 6.4.
We recommend to install latest stable version 1.4.24, read the below given release note.
Follow the given below steps for HAProxy 1.4.24 installation in CentOS 6.4
Details:
LoadBalancer Server Name = HAProxy
No. of Webserver = 2 (webserver1 and webserver2)
HAProxy IP address = 192.241.190.251
webserver1 IP address = 192.168.122.101
webserver2 IP address = 192.168.122.102
server Health Check File name = healthcheck.txt
Step 1: Login as root and download the HAProxy tarball in Server
yum install wget gcc gcc-c++ autoconf automake make wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
Step 2: Extract the tarball
tar -xvzf haproxy-1.4.24.tar.gz
Step 2: Now compile the HAProxy
cd haproxy-1.4.24 make TARGET=linux26 make install
Step 3: Create haproxy directory in /etc and its config file
mkdir -p /etc/haproxy
Now create a file called haproxy.cfg in /etc/haproxy
Note: In configuration file we have added two webserver named with webserver1 and webserver2.You can add two or more than two webserver in list. Replace the Servername and IP address as per your information.
vi /etc/haproxy/haproxy.cfg global log 127.0.0.1 local0 log 127.0.0.1 local1 notice maxconn 5000 chroot /var/lib/haproxy user haproxy group haproxy daemon defaults log global mode http option httplog option dontlognull option redispatch retries 3 maxconn 4000 contimeout 8000 clitimeout 80000 srvtimeout 80000 listen LOADBAL 192.241.190.251:80 mode http cookie LOADBAL insert balance roundrobin option httpclose option forwardfor option httpchk HEAD /healthcheck.txt HTTP/1.0 server webserver1 192.168.122.101:80 cookie webserver1 check server webserver2 192.168.122.102:80 cookie webserver2 check capture response header Location len 40 listen stats :4997 mode http stats enable stats hide-version stats realm Haproxy Statistics stats uri / stats auth scloaduser:your_password
Step 3: Now login into both server[webserver1 and webserver2] and create a blank file in its webspace called healthcheck.txt (See the line having “option httpcheck” in configuration)
Here in this example we have two server webserver1 and webserver2.
(a) Login into both server as a root
(b) Our web application path is at /var/www/html/application_name
Hence we are creating a blank file in same path in both server respectively.
touch /var/www/html/application_name/healthcheck.txt
Step 4: Now start the haproxy service in Loadbalancer server a.k.a HAproxy server
haproxy -f /etc/haproxy/haproxy.conf
To stop the haproxy service,first get the pid of haproxy and kill it
ps -ef|grep haproxy kill -9 pidof_haproxy
Step 5: To get the haproxy dashboard in configuration file below given lines were already added
listen stats 192.241.190.251:4997 mode http stats enable stats hide-version stats realm Haproxy Statistics stats uri / stats auth scloaduser:your_password
As per above configuration.
listen stats 192.241.190.251:4997 means in ip address 192.241.190.251 port no. 4997 you can get the haproxy dashboard.
stats auth scloaduser:your_password here you have to mention your haproxy’s dashboard username and password.
To access the HAproxy dahsboard, open the webbrowser and in URL type
http://loadbalancer-ip-address:4997
In our eg. it is
http://192.241.190.251:4997
To enable haproxy logging. Read this post https://sharadchhetri.com/2013/10/16/how-to-enable-logging-of-haproxy-in-rsyslog/
Thanks dude!!
Welcome Manoj