Load Balancing through HAProxy: Achieving High Availability and Scalability
In the age of technology, where downtime could result in large financial losses and poor user experiences, web-based application availability and scalability are essential.
Load balancing is an essential step in this process, ensuring that incoming network traffic is efficiently divided over numerous servers.
HAProxy, an elegant open-source software solution, emerging as an important participant in load balancing.
In this article, I will share what HAProxy is, how it works, and why it has become a top choice for business organizations wanting to improve the stability and performance of their infrastructure.
HAProxy, short for High Availability Proxy, is an open-source software application that excels in load balancing and proxying network traffic. It describes itself as an adaptable and flexible solution that can spread out the load across several backend servers while providing high availability and fault tolerance.
Let's begin by installing HAProxy on the server where you plan to run it. You can typically install it using your system’s package manager (e.g., apt, yum, or dnf for Linux distributions). Here I will be using CentOS, i.e., yum.
sudo yum install haproxy
First, make sure to configure the hostname of the server.
hostnamectl set-hostname webserverlb
Note down the host IP address for future reference.
hostname -I
You should have set up the necessary backend servers by this time.
Follow my article on “Virtual Web Hosting through Apache | httpd”
Start the HAProxy service using the following command once the installation is complete.
systemctl start haproxy
After the installation is finished, the firewall settings must be changed to permit HTTP traffic.
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
In my case, I have already changed the settings to permit traffic.
Note down the host IP address for future reference.
hostname -I
We must modify haproxy.cfg (the HAProxy configuration file) in order to connect our components, namely the servers and the load balancer (HAProxy).
We should continue by creating a configuration file. The default file path is /etc/haproxy/haproxy.cfg
You can use vim or nano to edit this file.
sudo vim /etc/haproxy/haproxy.cfg
For HAProxy to accept inbound connections, define the frontend section in your configuration file. The front end will be configured to listen on a certain IP address and port.
Comment out the current rules and add the following to the file: Include the IP Address over “ * ”
frontend haproxy
mode http
bind *:9000
stats enable
stats refresh 10s
stats uri /stats
stats admin if LOCALHOST
stats auth admin:haproxy
default_backend webservers
* In my case, I have already configured three backend web servers.
Once the configurations are done, save the configuration file (esc -->:+w+q)
The HAProxy Status page is a dashboard that is also provided by HAProxy. The key metrics that show server health, current request rates, response times, and more are displayed here.
Let's access this now,
Note down the host IP address for future reference.
hostname -I
You can test this by navigating to http://your_ip_address/stats/
, where you should see something like this.
You can view the HAProxy Status page to access important information about the load balancer once you have entered the correct credentials.
Now you can test the load balancer by sending multiple requests from the load balancer browser.
Now configure the rsyslog to confirm the location of the HAProxy logs.
vim /etc/rsyslog.conf
Add the following
$ModLoad imudp
$UDPServerRun 514
Then move to haproxy.conf to assemble logs.
vim /etc/rsyslog.d/haproxy.conf
local2.* /var/log/haproxy.log
Use the Load Balancer IP address or set up a hostname and domain name for you to easily access through the web server name.
http://your_load_balancer_hostname
If all the files are configured correctly but still the web page is not working, try using:
http://your_load_balancer_ip:9000
or
http://your_load_balancer_hostname:9000
Now that your web server is set up, you have a variety of options for the content types you can offer and the technologies you can utilize to give users a richer experience.
Disclaimer: The following article is based on my experience gained during a university project. While the principles and techniques discussed are applicable in various contexts, it’s important to note that real-world implementations may differ in complexity and requirements. This article aims to provide a foundational understanding of configuring HAProxy for load balancing, drawing from my educational experience.