Share on Social Media

Learn how to install and configure HAProxy Load Balancer on RHEL 8. Follow this step-by-step guide to enhance your server performance and reliability. #centlinux #linux #loadbalancer

What is HAProxy?

HAProxy (High Availability Proxy) is a robust, high-performance, and open-source load balancing and proxying software for TCP and HTTP-based applications. It is widely used to improve the performance, availability, and reliability of server environments by distributing incoming network or application traffic across multiple servers. Here are some key features and benefits of HAProxy:

  1. Load Balancing: Distributes incoming traffic evenly across multiple servers to prevent any single server from becoming a bottleneck.
  2. High Availability: Ensures that applications remain accessible even if one or more servers fail by automatically redirecting traffic to available servers.
  3. Scalability: Allows easy scaling of applications by adding or removing backend servers without downtime.
  4. Security: Offers advanced features such as SSL termination, protection against DDoS attacks, and rate limiting.
  5. Flexible Configuration: Highly configurable with support for various algorithms, session persistence, and ACLs (Access Control Lists).
  6. Logging and Monitoring: Provides extensive logging and monitoring capabilities to track performance and troubleshoot issues.
  7. Open Source: Available for free under the GPL license, with an active community and extensive documentation.

HAProxy is commonly used in high-traffic websites, e-commerce platforms, cloud services, and other environments where performance and uptime are critical.

HAProxy vs Nginx

HAProxy and Nginx are both popular open-source tools used for load balancing and reverse proxying, but they have distinct differences in their design, features, and typical use cases. Here’s a comparison to help understand their differences and use cases:

HAProxy

Primary Use Case:

  • Designed primarily as a load balancer and proxy server.
  • Highly efficient for both TCP and HTTP load balancing.

Performance:

  • Extremely high performance and low latency.
  • Optimized for handling large volumes of simultaneous connections.

Configuration:

  • Configuration is straightforward but can be complex for advanced scenarios.
  • Focused on load balancing and related features.

Features:

  • Advanced load balancing algorithms (round-robin, least connections, etc.).
  • Health checks, session persistence, SSL termination, and more.
  • Strong support for high availability and failover.

Logging and Monitoring:

  • Detailed logging capabilities.
  • Extensive monitoring options with support for various metrics.

Scalability:

  • Easily scalable, designed to handle large-scale deployments.

Nginx

Primary Use Case:

  • Initially designed as a web server with a strong focus on serving static content.
  • Also functions as a reverse proxy and load balancer.

Performance:

  • High performance with efficient handling of static content.
  • Can also handle a large number of concurrent connections.

Configuration:

  • Configuration syntax is considered simpler and more user-friendly.
  • Versatile, suitable for a variety of tasks beyond load balancing (e.g., web serving, caching).

Features:

  • Comprehensive HTTP and reverse proxy features.
  • Supports load balancing, caching, and WAF (Web Application Firewall) functionalities.
  • Can be extended with modules for additional capabilities.

Logging and Monitoring:

  • Robust logging capabilities.
  • Integrated monitoring features, and support for third-party monitoring tools.

Scalability:

  • Highly scalable, suitable for both small and large-scale deployments.
  • Easily configurable to handle different types of workloads.

When to Use HAProxy

  • When the primary need is advanced and high-performance load balancing for both TCP and HTTP traffic.
  • In environments where detailed control over load balancing algorithms and connection handling is required.
  • For scenarios requiring high availability and failover with detailed health checks.

When to Use Nginx

  • When a versatile web server is needed that can also handle load balancing and reverse proxying.
  • In scenarios where serving static content efficiently is a priority.
  • When a simpler configuration and a broader range of functionalities (beyond load balancing) are beneficial.

In summary, choose HAProxy if your main focus is on advanced load balancing and you need fine-grained control over connection handling. Opt for Nginx if you need a versatile tool that combines web serving, caching, and load balancing with simpler configuration and broader use cases.

Recommended Online Training: Master Load Balancing with HAProxy

4014252 3344 3show?id=oLRJ54lcVEg&offerid=1486687.3919727172258524700957&bids=1486687

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 Load Balancer Statistics
HAProxy Load Balancer Statistics

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.

Final Thoughts

In conclusion, HAProxy stands out as a robust and efficient solution for load balancing and proxying, particularly in environments requiring high performance and reliability. Its advanced features, such as detailed load balancing algorithms, health checks, SSL termination, and extensive monitoring capabilities, make it an ideal choice for optimizing server performance and ensuring high availability.

If you need expert assistance to install and configure HAProxy Load Balancer on RHEL 8, check out my Fiverr gig: I will be your linux server admin. I provide professional services to ensure your server environment is optimized for performance and reliability.

Leave a Reply