HaProxy

How To Install HaProxy On CentOS 7

install-haproxy
mm
Written by Santosh Prasad

HAProxy is a standout amongst the most popular open source load balancing software, which additionally offers high accessibility and proxy functionality. The main purpose of load balancing is to distribute web application horizontally across multiple hosts while providing the users with single point of access to the service.

HAProxy is also used to optimise resource usage, minimize response time, maximize throughput and avoid overloading any single resource.

HAProxy is especially suited for high traffic websites and frequently used to enhance web service reliability and performance for multi-server configurations. This guide lays out the means for setting up HAProxy as a load balancer on CentOS 7 to its own particular cloud have which at that point guides the traffic to your web servers.

In this article I will describe how to install HAProxy on CentOS 7 system.

Prerequisites

Require three CentOS 7 system.

1. Load Balancer – 192.168.0.5
2. nginx_app1 – 192.168.0.6
3. nginx_app2 – 192.168.0.7

Install And Configure HAProxy

Follow the below steps to install and configure HAProxy.

On Load Balancer 192.168.0.5

You can install HAProxy on your system using CentOS 7 repository, type the below command to install it.

# ssh [email protected]
# yum update -y
# yum install haproxy -y

Configuration

After installing HaProxy, now edit its configuration file located in /etc/haproxy/ directory.

Note :- Please make a copy of haproxy.cnf file before editing it.

# cd /etc/haproxy/
# mv haproxy.cfg haproxy.cfg.orig

Now create a new haproxy configuration file called “haproxy.cfg” usign vim editor.

# vim haproxy.cfg

Now paste the below configuration file. This file has been taken from www.howtoforge.com just for example purpose.

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2     #Log configuration
 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000                
    user        haproxy             #Haproxy running under user and group "haproxy"
    group       haproxy
    daemon
 
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
 
#---------------------------------------------------------------------
# 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
 
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen haproxy3-monitoring *:8080                #Haproxy Monitoring run on port 8080
    mode http
    option forwardfor
    option httpclose
    stats enable
    stats show-legends
    stats refresh 5s
    stats uri /stats                             #URL for HAProxy monitoring
    stats realm Haproxy\ Statistics
    stats auth looklinux:looklinux            #User and Password for login to the monitoring dashboard
    stats admin if TRUE
    default_backend app-main                    #This is optionally for monitoring backend
 
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend main
    bind *:80
    option http-server-close
    option forwardfor
    default_backend app-main
 
#---------------------------------------------------------------------
# BackEnd roundrobin as balance algorithm
#---------------------------------------------------------------------
backend app-main
    balance roundrobin                                     #Balance algorithm
    option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost    #Check the server application is up and healty - 200 status code
    server nginx_app1 192.168.0.6:80 check                 #Nginx web server 1
    server nginx_app2 192.168.0.7:80 check                 #Nginx web server 2

Save and close the file.

In the above configuration I have mention two Nginx web server to handle the load in roundrobin algorithm.

Nginx Installation And Configuration

No in this section, I am going to install nginx web server on both nginx_app1 and nginx_app2 server.

Login into both nginx server.

# ssh [email protected]
# ssh [email protected]

Now install epel repository on both servers using yum.

# yum install epel-release -y

Then install nginx on both servers.

# yum install nginx -y

Now go to the default web root directory on both server and create index file so that we can see which two servers delivered the html file.

# cd /var/www/html/
# echo "<h1>Welcome to nginx_app1</h1>" > index.html #For nginx_app1 server
# echo "<h1>Welcome to nginx_app2</h1>" > index.html #For nginx_app2 server

Next, start nginx service on both system and add it to start at boot time.

# service nginx start
# chkconfig nginx on

Testing

Open your browser and access the loadbalancer IP : 192.168.0.5. You will get both server index.html page.

http://192.168.0.5/

You can also test with curl command:

# curl http://192.168.0.5/

You will get something like below:

<h1>Welcome to nginx_app1</h1>
<h1>Welcome to nginx_app2</h1>

HAProxy Load Balancer Status Page

To monitor HAProxy loadbalancer type the below url in your browser with username and password “looklinux”:

http://192.168.0.5/stats

Thanks:)

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment