20 C
Texas
Melhttp://www.osradar.com
Guru Unix /Linux and GNU supporter

How to configure HAProxy Load Balancing on Centos 7

Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.
A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If any server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.
In this manner, a load balancer performs the following functions:
• Distributes client requests or network load efficiently across multiple servers
• Ensures high availability and reliability by sending requests only to servers that are online
• Provides the flexibility to add or subtract servers as demand dictates

To install and configure HAProxy loadbalancer we will consider below senario

  1. One is web server1>IP Address: 192.168.248.132>Hostname:system1.osradar.com
  2. Two is web server2>IP Address: 192.168.248.133>Hostname:system2.osradar.com
  3. HAproxyloadbalancer>IPAddress:192.168.248.134>Hostname:loadbalancer.osradar.com

First make sure that you systems has above host name and IP Address.

Step 01: To check hostname (for three systems)
#hostnamectl status
Static hostname: system1.osradar.com
Static hostname: system2.osradar.com
Static hostname: loadbalancer.osradar.com

- Advertisement -

Step 02: To Check IP Address (for three system)
#ip addr
or
#ifconfig
inet 192.168.248.132 netmask 255.255.255.0 broadcast 192.168.248.255
inet 192.168.248.133 netmask 255.255.255.0 broadcast 192.168.248.255
inet 192.168.248.134 netmask 255.255.255.0 broadcast 192.168.248.255

Step 03: Insert below information to all systems(system1, system2 and loadbalancer) /etc/hosts file
#vim /ete/hosts
192.168.248.132 system1.osradar.com system1
192.168.248.133 system2.osradar.com system2
192.168.248.134 loadbalancer.osradar.com loadbalancer

Step 04: To check that you can reach each system
#ping system1
#ping system2
#Pint loadbalancer

Step 05: Now install httpd to systems (system1, system2)
#yum install httpd -y
#systemctl start httpd
#systemctl enable httpd
#firewall-cmd –permanent –zone=public –add-service=http
#firewall-cmd –permanent –zone=public –add-service=https
#firewall-cmd –reload
#firewall-cmd –list-all (to ckeck status)

Step 06: To check your web service is ok visit 192.168.248.132 and 192.168.248.133 from your systems browser. You will find that Apahe test page appears.

You can make different index.html files for system1 and system2. So that you can get the difference. Here, we create index.html for system1 and system2 under /var/www/html

#vim /var/www/html/index.html (for system1)
This is <h1>system1</h1>.osradar.com
Website
<h3>IP: 192.168.248.132</h3>
#vim /var/www/html/index.html (for system2)
This is <h1>system2</h1>.osradar.com
Website
<h3>IP: 192.168.248.133</h3>

Step 07: Install HAProxy on loadbalancer system and configure
#yum install haproxy.x86_64 -y
#vim /etc/haproxy/haproxy.cfg
at the end of this file paste following information, save and exit.

##############Frontend Server Configuration##########
frontend webapp
bind *:80
default_backend webserver

#####################Backend Server Configuration##########

backend webserver
balance roundrobin
server system1 192.168.248.132:80 check
server system2 192.168.248.133:80 check

Step 08: Now we will check firewall status
#firewall-cmd –list-all
If we found that port 80 and 8080 is not added in firewall then we have to add

#firewall-cmd –permanent –zone=public –add-port=80/tcp
#firewall-cmd –permanent –zone=public –add-port=8080/tcp
#firewall-cmd –reload

Step 09: We will change SeLinux status from enforcing to permissive
#setenforce 0

Step 10:
#systemctl start haproxy.service
#systemctl enable haproxy.service

From loadbalancer system web browser visit 192.168.248.134. Hit(refresh) this IP two/three times and you will see web page from system1 and system2 are visible.

That’s how loadbalancer works.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article