Share on Social Media

HAProxy is a Reliable, High Performance TCP/HTTP Load Balancer. Here, you will learn how to install HAProxy load balancer on RHEL 8. #centlinux #linux #loadbalancer

What is HAProxy?:

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C programming language and has a reputation for being fast and efficient (in terms of processor and memory usage).

HAProxy is used by a number of high-profile websites including GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Slack, Speedtest.net, Tumblr, Twitter and Tuenti and is used in the OpsWorks product from Amazon Web Services.(Source: Wikipedia)

HAProxy Features:

HAProxy has the following features:

  • Layer 4 (TCP) and Layer 7 (HTTP) load balancing
  • URL rewriting
  • Rate limiting
  • SSL/TLS termination
  • Gzip compression
  • Proxy Protocol support
  • Health checking
  • Connection and HTTP message logging
  • HTTP/2
  • Multithreading
  • Hitless Reloads
  • gRPC Support
  • Lua and SPOE Support
  • API Support
  • Layer 4 Retries
  • Simplified circuit breaking

Environment Specification:

We are using a minimal RHEL 8 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Red Hat Enterprise Linux (RHEL) 8.3
  • Hostname – haproxy-01.centlinux.com
  • IP Address – 192.168.116.238 /24

Updating the Linux Server:

Connect with haproxy-01.centlinux.com as root user with the help of a ssh client.

Build cache for the Red Hat Subscription Management repositories.

# dnf makecache
Updating Subscription Management repositories.
Red Hat Enterprise Linux 8 for x86_64 - BaseOS  2.6 kB/s | 4.1 kB     00:01
Red Hat Enterprise Linux 8 for x86_64 - BaseOS  335 kB/s |  28 MB     01:26
Red Hat Enterprise Linux 8 for x86_64 - AppStre 3.3 kB/s | 4.5 kB     00:01
Red Hat Enterprise Linux 8 for x86_64 - AppStre 253 kB/s |  26 MB     01:46
Last metadata expiration check: 0:00:08 ago on Sun 21 Feb 2021 11:41:31 AM EST.
Metadata cache created.

Update installed software packages in your Linux server as follows.

# dnf update -y

The above command has updated the Linux kernel, therefore, we are required to reboot the Linux operating system with new Kernel.

# reboot

Install Apache Web Server on RHEL 8:

In this article, you are required two web servers and a HAProxy load balancing server. But, we do not have three machines, therefore, we are configuring the two Apache virtual hosts on the same HAProxy server and then setup the HTTP Load Balancer for them.

Install Apache web server on your RHEL server by using dnf command.

# dnf install -y @httpd

Create “Document Root” directories for your Apache virtual hosts.

# mkdir /var/www/host-{80,8080}

Generate the index.html (the default webpage) for your Apache virtual hosts.

# vi /var/www/host-80/index.html

Add following HTML code therein.

<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-80</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1>
</BODY>
</HTML>

Similarly, for second virtual host.

# vi /var/www/host-8080/index.html

Add following HTML code.

<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-8080</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1>
</BODY>
</HTML>

Add virtual host configurations in Apache web server.

# vi /etc/httpd/conf.d/test.conf

Add following directives in this file.

Listen 80
Listen 8080

<VirtualHost 192.168.116.238:80>
    ServerName haproxy-01.centlinux.com
    DocumentRoot "/var/www/host-80"
</VirtualHost>

<VirtualHost 192.168.116.238:8080>
    ServerName haproxy-01.centlinux.com
    DocumentRoot "/var/www/host-8080"
</VirtualHost>

Apache web server is using ports 80/tcp and 8080/tcp, therefore, you have to allow these network ports in your Linux firewall.

# firewall-cmd --permanent --add-port={80,8080}/tcp
success
# firewall-cmd --reload
success

Enable and start the Apache web server.

# systemctl enable --now httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service â /usr/lib/systemd/system/httpd.service.

Verify Apache virtual hosts, by using the curl command.

# curl http://haproxy-01.centlinux.com:80
<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-80</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1>
</BODY>
</HTML>

# curl http://haproxy-01.centlinux.com:8080
<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-8080</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1>
</BODY>
</HTML>

If you got the above output then your Apache virtual hosts are configured successfully.

Install HAProxy Load Balancer on RHEL 8:

You can install HAProxy from Red Hat subscription management repositories by using dnf command.

# dnf install -y haproxy
Updating Subscription Management repositories.
...
Installed:
  haproxy-1.8.23-5.el8.x86_64

Complete!

HAProxy load balancer is installed on your Linux server.

Configure HAProxy Log in RHEL 8:

Now you need to configure the HAProxy log. This log is very helpful in troubleshooting HAProxy on Linux server.

Open rsyslog configuration file in vim text editor.

# vi /etc/rsyslog.conf

Locate and uncomment following two lines in this file.

module(load="imudp") # needs to be done just once
input(type="imudp" port="514")

Create a rsyslog configuration file for setting HAProxy logfile location.

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

Add following directives in this file.

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

Restart rsyslog service to load changes.

# systemctl restart rsyslog.service

Configure HAProxy Load Balancer:

Open HAProxy configuration file in vim text editor.

# vi /etc/haproxy/haproxy.cfg

Add following directives at the end of this file.

# HAProxy Load Balancer for Apache Web Server
frontend http-loadbalancer
 bind 192.168.116.238:9000
 default_backend web-servers

backend web-servers
 mode http
 balance roundrobin
 stats enable
 stats auth centlinux:Ahmer@1234
 server webserver-01 192.168.116.238:80 check
 server webserver-02 192.168.116.238:8080 check

In above configurations, we have setup a HAProxy load balancer named http-loadbalancer, running on the network port 9000/tcp.

Our HTTP load balancer will perform round robin load balancing between two web servers. i.e. webserver-01 and webserver-02.

Here, we have used Apache virtual hosts, therefore we are using same IP address with different ports. You can also use the IP Address or Hostname of your web servers, if running services on different machines.

You must allow the service port 9000/tcp in your Linux firewall, to make it accessible across the computer network.

# firewall-cmd --permanent --add-port=9000/tcp
success
# firewall-cmd --reload
success

Make sure you have completed above configurations correctly. Thereafter, enable and start the HAProxy service.

# systemctl enable --now haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service â /usr/lib/systemd/system/haproxy.service.

Execute curl command to access your HAProxy load balancer multiple times.

# curl http://haproxy-01.centlinux.com:9000
<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-80</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1>
</BODY>
</HTML>

# curl http://haproxy-01.centlinux.com:9000
<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-8080</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1>
</BODY>
</HTML>

# curl http://haproxy-01.centlinux.com:9000
<HTML>
<HEAD>
<TITLE>TEST PAGE AT HOST-80</TITLE>
<BODY>
<H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1>
</BODY>
</HTML>

You can see that the HAProxy load balancer is rotating the service requests between the two web servers in round robin fashion.

However, you can see the HAProxy statistics and logs in /var/log/haproxy.log file. But you can also view the Haproxy integrated statistics report via your web browser.

For this purpose, you can open http://haproxy-01.centlinux.com:9000/haproxy?stats in a web browser.

HAProxy Statistics Report

Feel difficulty in understanding the above configurations, then your should buy and read RHCSA Red Hat Enterprise Linux 8 (UPDATED): Training and Exam Preparation Guide (EX200), Second Edition (PAID LINK) by Asghar Ghori.

Conclusion:

In this article, we have successfully installed and configured HAProxy load balancer on RHEL 8 server.

Leave a Reply

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