How to Install and Configure HAProxy as Load Balancer for Web service on CentOS 7

HAProxy or High Availability Proxy is an open source TCP and HTTP load balancer and proxy server software. HAProxy is a fast and lightweight proxy server and load balancer with a small memory footprint and low CPU usage. It is used by large sites like Github, StackOverflow, Reddit, Tumblr, Twitter and others. It has become the most popular software load balancer and proxy server in the past years.

Basic Concept of HAProxy

HAProxy can run in two modes: TCP mode Layer 4 and HTTP Mode Layer 7. In Layer 4 TCP mode, HAProxy forwards the RAW TCP packets from the client to the application servers. In the Layer 7 HTTP mode, HAProxy is parsing the HTTP header before forwarding them to the application servers. In this tutorial, we will use Apache as the web server that only supports the Layer 7 HTTP mode.

The following modes are available:

Balance Algorithm : This is the algorithm that is used by HAProxy to select the server when doing the load balancing. 

Roundrobin : This is the most simple balance algorithm. For each new connection, it will be handled by the next backend server. If the last backend server in the list is reached, it will start again from the top of backend list.

Lastconn : The new connection will be handled by the backend server with least amount of connections. This is useful when the time and load of the requests vary a lot.

Source: This is for sticky sessions, the client IP will be hashed to determine the backend server that received the last request from this IP. So an IP A will always be handled by backend1, and IP B will always be handled by banckend2 to not interrupt sessions

There are other algorithm - check the official HAProxy site for details.

Prerequisites

4 CentOS 7 pre-install servers.
  • Load Balancer (192.168.2.110)
  • Web Server1 (192.168.2.101)
  • Web Server2 (192.168.2.102)
  • Web Server3 (192.168.2.103)
Root privileges on all 3 servers.

Step 1 – Install HAProxy

HAProxy package is available under default yum repository for CentOS & Red Hat systems. Use the following yum package manager command to install HAProxy on your system.
                         
#yum install haproxy

 Step 2 – Configure HAProxy

Update your HAProxy configuration file /etc/haproxy/haproxy.cfg as per your requirement, You may also use below given configuration file as an example of setup and modify it.

 #cd /etc/haproxy/
 #mv haproxy.cfg haproxy.cfg.orig  #### Take the backup file before doing modification.
 # vi haproxy.cfg
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 debug
        maxconn   45000 # Total Max Connections.
        daemon
        nbproc      1 # Number of processing cores.
defaults
        timeout server  1m
        timeout connect 10s
        timeout client  1m
        timeout queue   1m


# [HTTP Site Configuration]
listen  http_web 192.168.2.110:80
        mode http
        balance roundrobin  # Load Balancing algorithm
        option httpchk
        option forwardfor
        server server1 192.168.2.101:80 weight 1 maxconn 512 check
        server server2 192.168.2.102:80 weight 1 maxconn 512 check
        server server2 192.168.2.103:80 weight 1 maxconn 512 check

Save configuration file

Change the ips in configuration file as per your network setup. In HTTP Site Configuration section if any request on ip 192.168.2.110 on port 80, this will be redirected to port 80 of 192.168.2.100 or 192.168.2.101 or 192.168.2.102 servers.

You also need to make few changes in configuration file as per your system configuration.
nbproc <value> # Number of processing cores in your system.
mode <value> # ‘http’ for http site and ‘tcp’ for https site
balance <value> # Type of load balancing like ‘source’, ’roundrobin’ etc.

Configure 

We will configure the rsyslog daemon to log the HAProxy statistics. Edit the rsyslog.conf file to enable the UDP port 514 to be used by rsyslog

Uncomment this line to enable the UDP connection:
If you want to use a specific IP, you can add a new line like the one below:

#vi /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
$UDPServerAddress 192.168.2.110

Save configuration file

Then create new haproxy configuration file for rsyslog:

#cd /etc/rsyslog.d/
#vi haproxy.conf
local2.=info     /var/log/haproxy-access.log    #For Access Log
local2.notice    /var/log/haproxy-info.log      #For Service Info - Backend, loadbalancer

Save configuration file


Now restart rsyslog and then start the haproxy & Add haproxy to start at boot time.

#systemctl restart rsyslog
#systemctl start haproxy
#systemctl enable haproxy

Step 3 - Install and Configure Apache

In this section, we will install Apache on web01,web02 and web03 server.

Log in to the web servers using ssh:

#ssh web01

Now you can install Apache on all Servers:

#yum -y install httpd

Apache is installed. Go to the web directory and change the index file so that we can see which of the 3 servers delivered the html file:

#cd /var/www/html/
#echo "<h1>Welcome to `hostname`</h1>" > index.html     

Next, add Apache to start at boot time and then start it:

#systemctl enable httpd
#systemctl start httpd

Make sure you're doing this step on web01,web02 and web03 server.

Step 4 Testing 

Testing from browser by accessing the loadbalancer IP: 192.168.2.110






How to Enable HAProxy Stats

       HAProxy Stats provides a lot of information about data transfer, total connection, server state etc. After installing HAProxy if you want to view HAProxy stats in your web browser, You can easily configure it by making few changes in your HAProxy configuration using following steps.

Step 1: Enable Stats in HAProxy

        To enable stats edit your haproxy configuration file and add below entry after defaults section.

     # vi haproxy.cfg
        listen  stats   192.168.2.110:1936
        mode            http
        log             global

        stats enable
        stats hide-version
        stats refresh 30s
        stats show-node
        stats auth admin:password
        stats uri  /haproxy/stats

Save configuration file

Step 2: Access HAProxy Stats


Your can access HAProxy stats using following url. Change ip with your haproxy server ip address.

URL:http://192.168.2.110:1936/haproxy/stats
Login user: admin
Login password: password
  



Step 3: Change Login Details


If you want to changed login details of HAProxy stats, edit your configuration and update “stats auth” value like below

stats auth  username:password

Save configuration file and restart HAProxy to update service.

Step 4: Change HAProxy Stats URL
To change url of haproxy stats edit configuration file and update following value.

stats uri  /ha-stats
or
stats uri  /stats

Save configuration file and restart HAProxy to update service. 

Now you can access url like http://192.168.10.10:1936/ha-stats or http://192.168.10.10:1936/stats.


Haproxy stats configuration has been completed successfully.







2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. can you tell how to make the multiple port for same IP in linux pc
    I use oracl VB linux

    ReplyDelete