HAProxy for Load Balancing setup
Share with your friends
Customize readability

HAProxy for Load Balancing setup

shilpadeepthi

All Rights Reserved ©

Summary

HAProxy stands for High Availability Proxy which is a TCP/HTTP load balancer and proxy server which allows a web server to spread incoming requests across multiple endpoints. It is used when too many concurrent connections are requested on a single cloud hosting server. Using HAProxy, It allows a client connecting to a single server that handles all of the requests, the client will be connected to an HAProxy instance that will use a reverse proxy to forward the requests to the available endpoints using the load-balancing algorithm.HAProxy to select the server when doing the load balancing it uses balance Algorithm The Source, Round Robin and Lastconn modes are available.

Status
Complete
Chapters
1
Rating
n/a
Age Rating
13+

Chapter 1

HAProxy stands for High Availability Proxy which is a TCP/HTTP load balancer and proxy server which allows a web server to spread incoming requests across multiple endpoints.

It is used when too many concurrent connections are requested on a single cloud hosting (https://www.hostingraja.in/)server. Using HAProxy, It allows a client connecting to a single server that handles all of the requests, the client will be connected to an HAProxy instance that will use a reverse proxy to forward the requests to the available endpoints using the load-balancing algorithm.HAProxy to select the server when doing the load balancing it uses balance Algorithm The Source, Round Robin and Lastconn modes are available.

Round Robin is a simple balance algorithm on the new connection it handles the next backend server and when the last backend server is reached, it will once again start from the top of the backend list.

Lastconn, on a new connection, the backend server(http://mariechatfield.com/tutorials/explanations/backend.html)with least amount of connections will handle the new connection.

Source,it is used for sticky sessions, that is it has the client IP to determine the backend server that received the last request from this IP.

HAProxy also supports various features like SSL (https://www.ssl.com/faqs/faq-what-is-ssl/) compressions, keep-alive, custom log formats, and header rewriting.

An haproxy server is a fast and lightweight and also the load balancer uses a small memory and CPU usage is low. This is used by many large sites and has become popular software load balancer and proxy server.

Here, we will see we will guide to install and configure HAproxy on 3 servers where one server act as a load balancer where the HAProxy will be installed and configured (HAproxy will act as a load balancer), while the other two as nginx servers where the Nginx web server will be installed and configured.

The below are the steps to configure HAproxy on three servers:

Requirements:3 servers with root access

In this case, the 3 servers are:

Server Name : ServerLB

Server IP: 10.12.17.31

Server Name : Server_1

Server IP: 10.12.17.32

Server Name : server_ 2

Server IP: 10.12.17.33

Here, we take serverLB as loadbalancer and server_1 & server_2 as nginx server

Step 1 : Basic setup of the servers:Configure the /etc/hosts files

Step 1.1: Login into ServerLB

ssh [email protected]

Step 1.2: To ensure you are working in root session

sudo su

Step 1.3: In vi /etc/hosts

add the hostnames:

10.12.17.31 ServerLB ser1

10.12.17.32 Server_1 ser2

10.12.17.33 Server_2 ser3

Step 1.4: Save the file and exit the editor. (To save the changes use wq to write and quit)

Step 1.5: Now you should edit host file for server_1

ssh [email protected]

Step 1.6: In vi /etc/hosts

add the hostnames:

10.12.17.31 ServerLB ser1

10.12.17.32 Server_1 ser2

10.12.17.33 Server_2 ser3

Step 1.7: Save the file and exit the editor. (To save the changes use wq to write and quit)

Step 1.8: Now you should edit host file for server_1

ssh [email protected]

Step 1.9: In vi /etc/hosts

add the hostnames:

10.12.17.31 ServerLB ser1

10.12.17.32 Server_1 ser2

10.12.17.33 Server_2 ser3

Step 1.10: Save the file and exit the editor. (To save the changes use wq to write and quit)

Step 2 : Now in this step you have to Install and Configure HAProxy

Step 2.1: In ServerLB

ssh [email protected]

Step 2.2: yum -y update

Step 2.3: yum -y install haproxy

Step 2.4: go to the “/etc/haproxy/” d

cd /etc/haproxy/

Step 2.5:Backup the original configuration file

mv haproxy.cfg haproxy.cfg.orig

Step 2.6:create a new haproxy.cfg and add the below code, modification accordingly

vi haproxy.cfg

Paste the 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 ser2 10.12.17.32:80 check #server2

server ser3 10.12.17.33:80 check #server3

Save the configuration file and exit. (To save the changes use wq to write and quit)

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.

Step 3 :To configure the rsyslog daemon to log the HAproxy statistics ,for that we will need to edit rsyslog.conf file in which we need to enable the UDP port 514 to be used by rsyslog

Now in this step you have to Install and Configure HAProxy

Step 3.1: In vi /etc/rsyslog.conf

$ModLoad imudp

$UDPServerRun 514

If you need to use a specific IP, You can add a the below line the your desired local IP

$UDPServerAddress 127.0.0.1

Save the changes which you have made and exit. (To save the changes use wq to write and quit)

Step 3.2: Next , you have to create a new 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 the changes and exit. (To save the changes use wq to write and quit)

Step 3.3: And now we need to restart the rsyslog and then start the HAproxy

systemctl restart rsyslog

systemctl start haproxy

You need to add HAproxy to start at boot time in HAproxy.conf

systemctl enable haproxy

Step 4 - Installing and Configuring Nginx

we need to install Nginx from epel repository on server_1 and Server_2 server.

Step 4.1:Log in to the servers:

ssh [email protected]

Install the epel repository with the yum command below:

Step 4.1.1: yum -y install epel-release

Step 4.1.2:Now you can install Nginx:

yum -y install nginx

Step 4.1.3: 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>v1.loadbalance.me</h1>” > index.html #For nginx server1

Step 4.1.4:Next, add Nginx to start at boot time in HAproxy.conf and then start:

systemctl enable nginx

Step 4.1.5: start the server

systemctl start nginx

Step 4.2:Log in to the servers:

ssh [email protected]

Install the epel repository with the yum command below:

Step 4.2.1: yum -y install epel-release

Step 4.2.2:Now you can install Nginx:

yum -y install nginx

Step 4.1.3: 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>v1.loadbalance.me</h1>” > index.html #For nginx server2

Step 4.1.4:Next, add Nginx to start at boot time in HAproxy.conf and then start:

systemctl enable nginx

Step 4.1.5: start the server

systemctl start nginx

Step 5 :Testing

For testing using the browser by accessing the load balancer IP: 10.12.17.31

Request handled by Nginx- server 2

Request handled by Nginx- server 1

You can also test it using the curl command:

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v2.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v1.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v2.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v1.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v2.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v1.loadbalance.me</h1>

[root@xyz-1 rsyslog.d]# curl 10.12.17.31

<h1>v2.loadbalance.me</h1>

Let shilpadeepthi know what you thought about this chapter!

Further Recommendations

Vengeance

shazy_faith: i wished that elizabet's powers would be envoked and she would be powerful and mighty and also seee her fae side of magic. alice would have been a guider to elizabeth in her magical journey . owen and jo obviously are married and viper and smitty will be like bestman[s for brax when liza and brax g...

Read Now
Discovering Freya

Helhall: Before anything else, I’d like to emphasize that I really did like the overall story. Characters were more than two-dimensional, so definitely a plus. I didn’t even mind the shifting points of view.But...There are a couple of things that made reading the story very difficult.1- Punctuation is your f...

Read Now
Dorian Ponder - Atoned Souls - Book 1

Margaret Lambert: Strong womenAuthor not frightened to show women with strength

Read Now
Queen Alpha

ltericson: Argh - why now for a review?!!!

Read Now
His Luna Queen [Discontinued]

Angela: A good story so far. I like Amora's character and the ideas such as, Aiden taken power over the pack and stripping Amora of her title. The drama around how Aiden tortured Amora carving his name into her skin was dramatic which I liked. Gave the story some substance. When Aiden captures Amora and beg...

Read Now
The Half Blood Wolf (Lupus Antiqua Series #1)

Suiihera: OMG, I just finished reading your story, and I really enjoyed it! The world you've created is fascinating. As I was reading, I could easily picture the characters, emotions, and key moments unfolding visually.Have you ever considered adapting it into a visual storytelling format such as a comic, man...

Read Now
From Felon to Family

Stephany Elworth: Good story

Read Now
P.I

Susan: Great story, I loved it

Read Now
The Discreet Charm of Lady Mary

Sarah: I really enjoyed your story. It made me wonder how you actually write. Are you someone who plans everything in advance, or do you discover the story as the chapters unfold?

Read Now