- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Setup HAproxy Load Balance Server for Sharing Web Traffic
This article is written to prepare high availability of a website. This can also be used by those who want their website to be available to their clients or users with high availability servers and to share between their servers. It is a free and open source application used as a TCP/HTTP load balancer which will distribute web traffic to multiple servers and improves performance and reliability of the web server.
Installation
Assume that load balancer HA proxy server IP address is http://192.167.57.150
HAproxy Server details
OS: Centos 6.7, IP Address: 192.168.57.150
WebServer1
OS: Centos 6.7, IP Address: 192.168.57.147
WebServer2
OS: Centos 6.7, IP Address: 192.168.57.148
We need to install the Apache on both the web servers, i.e., 192.168.57.151 and 192.168.57.152.
# yum install http*
After installation, please access the Apache from web browser http://your-server-ip-address
Installing HAProxy Server
# yum install haproxy openssl-devel
Open the host files and add the below lines in all 3 servers (HAproxy Load balancer, webserver1, webserver2)
192.168.87.150 haproxy.demo.com haproxy 192.168.87.151 webserv1.demo.com haproxy 192.168.87.152 webserv2.demo.com haproxy # vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.87.150 haserver.cgi.com haserver 192.168.87.151 ha2.cgi.com ha2 192.168.87.152 ha1.cgi.com ha1
Now, you need to enable the HAproxy Logs to identify the problems for your future debugging
# vi /etc/haproxy/haproxy.cfg
Add these below lines to enable the logging
log 127.0.0.1 local Needed to enable the UDp SYSLOG reception in /etc/rsyslog.conf # vi /etc/rsyslog.conf #### MODULES #### $ModLoad imuxsock # provides support for local system logging (e.g. Via logger command) $ModLoad imklog # provides kernel logging support (previously done by rklogd) #$ModLoad immark # provides --MARK-- message capability # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 # Provides TCP SYSLOG reception #$ModLoad imtcp #$InputTCPServerRun 514
Now create a haproxy.conf in /etc/rsyslog.d/ folder to create log files
# vi /etc/rsyslog.d/haproxy.conf
Add the below line to create the file
local.* /var/log/haproxy.log
Restart the rsyslog services to update the changes we made
# service rsyslog restart
Configuring HAproxy global settings
we needed to setup the glogal setting in /etc/haproxy/haproxy.cfg # vi /etc/haproxy/haproxy.cfg #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000
We have to provide the details of both the web servers as front-end and back-end servers in case of failure.
frontend LB bind 192.168.87.150:80 reqadd X-Forwarded-Proto: http default_backend LB backend LB 192.168.87.150:80 mode http stats enable stats hide-version stats uri /stats stats realm Haproxy Statistics stats auth haproxy:redhat # Credentials for HAProxy Statistic report page. balance roundrobin # Load balancing will work in round-robin process. option httpchk option httpclose option forwardfor cookie LB insert server ha2 192.168.87.151:80 cookie ha1 check # backend server. server ha1 192.168.87.152:80 cookie ha2 check # backend server.
We need to restart the services and make the changes effective.
# service haproxy restart
To start the service automatically, use the below command
# chkconfig haproxy on
To check the autostart –
# chkconfig --list haproxy
After the services, re-start so that, we can access the load balancer on http://192.168.87.150:/stats
For testing the HAproxy load balancer, we will have to create an index.html file in both web servers with below code.
Create an index.html file in the webserver1 with the bellow code.
<html> <head> <title>HAProxy Test Page</title> </head> <body> <!-- Main content --> <h1>My HAProxy Test Page Server 1 </h1> </body> </html>
Create an index.html file in webserver2 with the bellow code.
<html> <head> <title>HAProxy Test Page</title> </head> <body> <!-- Main content --> <h1>My HAProxy Test Page Server 2 </h1> </body> </html>
Now access the haproxy server IP http://192.168.57.150
By default, you can see the page of webserver 1
Webserver1
Now, test the load balance by going to webserver 1 and stop the HTTPd server
# service httpd stop
Then, access the haproxy server IP, http://192.168.57.150
Now, you will see the web page of webserver2 automatically
Webserver2
To verify, you can access the HAproxy load balancer statistics page http://192.168.87.150/stats
HAproxy
Conclusion
After the configuration, you should be able to get your website without interruption even if one of the servers is down or unavailable. In the same way, we can also set up a HAproxy for Apache. You can also observe that, Webserver1 is not accessible or Down in the HAproxy load balancer statistics page by configuring the above as shown. You can also add any number of servers depending upon the requirement.