Step 1 – Configure the /etc/hosts files

Log in to the load balancer server and edit the /etc/hosts file.

ssh loadbalancer@192.168.1.102
sudo su

vi /etc/hosts

Add nginx1 and nginx2 hostnames:

192.168.1.104    nginx1.loadbalancer.me     nginx1
192.168.1.105    nginx2.loadbalancer.me     nginx2

Save the file and exit the editor.

Next, edit the hosts file on the Nginx servers (nginx1 and nginx2):

ssh nginx1@192.168.1.104
ssh nginx2@192.168.1.105

Edit and add a new line for the load balancer in the hosts files:

vi /etc/host

Add the loadbalancer hostname on each nginx server:

192.168.1.102    loadbalancer

do this on nginx1 and nginx2 server.

Step 2 – Install and Configure HAProxy

HAProxy is available in the CentOS 7 repository, log in to the loadbalancer server and update the package lists:

ssh loadbalancer@192.168.1.104
yum -y update

Now install HAProxy with this yum command:

yum -y install haproxy

When the installation is finished, go to the “/etc/haproxy/” directory and backup the original configuration file:

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

Next, add a new HAProxy configuration file “haproxy.cfg” file with the vi editor:

vi haproxy.cfg

Paste the configuration below:

#---------------------------------------------------------------------
# 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 howtoforge:howtoforge            #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 nginx1 192.168.1.104:80 check                 #Nginx1 
    server nginx2 192.168.1.105:80 check                 #Nginx2

Save the configuration file and exit.

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.

vi /etc/rsyslog.conf

Uncomment this line to enable the UDP connection:

$ModLoad imudp
$UDPServerRun 514

If you want to use a specific IP, you can add a new line like the one below:

$UDPServerAddress 127.0.0.1

Save the file and exit.

Then create new haproxy configuration file for rsyslog:

cd /etc/rsyslog.d/
vi haproxy.conf

Paste configuration below:

local2.=info     /var/log/haproxy-access.log    #For Access Log
local2.notice    /var/log/haproxy-info.log      #For Service Info - Backend, loadbalancer

Save and exit.

Now restart rsyslog and then start the haproxy:

systemctl restart rsyslog
systemctl start haproxy

Add haproxy to start at boot time:

systemctl enable haproxy

Step 3 – Install and Configure Nginx

In this section, we will install Nginx from epel repository on nginx1 and nginx2 server.

Log in to the servers:

ssh nginx1@192.168.1.104
ssh nginx2@192.168.1.105

Install the epel repository with the yum command below:

yum -y install epel-release

Now you can install Nginx:

yum -y install nginx

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

cd /usr/share/nginx/html/
echo “<h1>nginx1.loadbalance.me</h1>” > index.html     #For nginx1 server
echo “<h1>nginx2.loadbalance.me</h1>” > index.html     #For nginx2 server

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

systemctl enable nginx
systemctl start nginx

Make sure you’re doing this step on nginx1 and nginx2 server.

Leave a Reply

Your email address will not be published. Required fields are marked *