Author Photo

Ahmer's SysAdmin Recipes is a blog for Linux System Administrators. This blog provides recipes for Installation & Configuration of Linux, Ubuntu, Oracle Database, MySQL, Apache, Nginx, Oracle Cloud Control, FreeIPA, Kerberos & OpenLDAP, Subversion, PXE, DevOps, etc. over Red Hat Enterprise Linux (RHEL), CentOS, Ubuntu and Windows.

Please give me your feedback and help me improve this blog. Please let me know If you want me to write on a specific topic.

Saturday, 18 August 2018

HAProxy: Configure HTTP Load Balancer in CentOS 7

Configure HTTP Load Balancer with HAProxy Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool. Load Balancing improves the distribution of workloads across multiple computing resources. Using multiple components with load balancing instead of a single component may increase reliability and availability through redundancy.

Load Balancer is the device that perform Load Balancing. Hardware and software load balancers may have a variety of special features. The fundamental feature of a load balancer is to be able to distribute incoming requests over a number of backend servers in the cluster according to a scheduling algorithm.

In this post, we will configure a HTTP Load Balancer with HAProxy to distribute HTTP workload between two webservers.

 

System Specification:

We have two Apache Web Servers webserver-01.example.com and webserver-02.example.com and we want to configure a HTTP Load Balancer for them i.e. loadbalancer.example.com.

Hostname loadbalancer.example.com webserver-01.example.com webserver-02.example.com
IP Address 192.168.116.30/24 192.168.116.31/24 192.168.116.32/24
Operating System RHEL 7 RHEL 7 RHEL 7
Web Server NA Nginx Nginx

 

Configure HAProxy:

To ensure that our webservers are properly configured and browsable, open their URLs in a Browser.

Read Also: Install Linux, Apache, MySQL & PHP (LAMP) Stack
  Install Linux, Nginx, MySQL & PHP (LEMP) Stack

Webserver-01 Main PageWebserver-02 Main Page

I have set different index pages on both servers, to differentiate between servers, when we are accessing using the load balancer.

Connect to the loadbalancer.example.com and install HAProxy.

[root@loadbalancer ~]# yum install -y haproxy Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package haproxy.x86_64 0:1.5-0.3.dev22.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: haproxy x86_64 1.5-0.3.dev22.el7 localyum 696 k Transaction Summary ================================================================================ Install 1 Package Total download size: 696 k Installed size: 2.2 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : haproxy-1.5-0.3.dev22.el7.x86_64 1/1 Verifying : haproxy-1.5-0.3.dev22.el7.x86_64 1/1 Installed: haproxy.x86_64 0:1.5-0.3.dev22.el7 Complete!

HAProxy has been installed.

Now configure rsyslog to handle HAProxy logs.

[root@loadbalancer ~]# vi /etc/rsyslog.conf

Search and uncomment following lines.

$ModLoad imudp $UDPServerRun 514

Add following directive in the same file.

# HAProxy Logging local2.* /var/log/haproxy.log

Save settings and exit.

Restart rsyslog service to apply changes.

[root@loadbalancer ~]# systemctl restart rsyslog.service

Now configure HAProxy settings according to our environment.

[root@loadbalancer ~]# vi /etc/haproxy/haproxy.cfg

Add following directives in this file.

# HAProxy Load Balancer for Apache Web Server frontend http-balancer bind 192.168.116.30:80 default_backend web-servers backend web-servers mode http balance roundrobin stats enable stats auth admin:123 server webserver-01 192.168.116.31:80 check server webserver-02 192.168.116.32:80 check

Save settings and exit.

Start and enable haproxy service.

[root@loadbalancer ~]# systemctl start haproxy ; systemctl enable haproxy ln -s '/usr/lib/systemd/system/haproxy.service' '/etc/systemd/system/multi-user.target.wants/haproxy.service'

Allow HTTP service through Linux firewall.

[root@loadbalancer ~]# firewall-cmd --permanent --add-service=http success [root@loadbalancer ~]# firewall-cmd --reload success

 

Test HAProxy Configuration:

Open the URL of the loadbalancer.example.com in a browser.

Webserver-01 Main Page Through Load Balancer

You may observe that the HTTP request has been served by webserver-01.example.com. It shows that our Load Balancer has forwarded the request to this server.

Press F5 to refresh the webpage.

Webserver-02 Main Page Through Load Balancer

Since, we have configured the roundrobin balancing in HAProxy. Therefore, it forwards next request to alternate server. You can refresh webpage multiple times to observe the functionality.

To see the detailed statistics of the HAProxy. Browse to http://192.168.116.30/haproxy?stats.

HAProxy Statistics Webpage

We have successfully configured a HTTP Load Balancer with HAProxy.

HAProxy: Configure HTTP Load Balancer in CentOS 7


YOU MIGHT ALSO LIKE:

No comments:

Post a Comment