HAProxy is a free and 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 distributes the load among the web and application servers.
Haproxy is popular for load balancing because of its efficiency, reliability, and low memory and CPU footprint. Load balancing is a common solution for distributing web applications horizontally across multiple hosts while providing the users with a single point of access to the service.
It is available for install on major Linux distributions. In this guide we will learn how to install and configure HAProxy load balancer on Debian 11 and Debian 12.
Also check:
- How to install and Configure HAProxy load balancer on Rocky Linux/Alma Linux 8
- How to install and Configure HAProxy load balancer on Ubuntu 20.04
# HAProxy Load balancing algorithms
The balancing algorithms are used to decide which server at the backend each connection is transferred to. Some of the useful options include the following:
Roundrobin
: Each server is used in turns according to their weights. This is the smoothest and fairest algorithm when the servers’ processing time remains equally distributed. This algorithm is dynamic, which allows server weights to be adjusted on the fly.Leastconn
: The server with the lowest number of connections is chosen. Round-robin is performed between servers with the same load. Using this algorithm is recommended with long sessions, such as LDAP, SQL, TSE, etc, but it is not very well suited for short sessions such as HTTP.First
: The first server with available connection slots receives the connection. The servers are chosen from the lowest numeric identifier to the highest, which defaults to the server’s position on the farm. Once a server reaches its maxconn value, the next server is used.Source
: The source IP address is hashed and divided by the total weight of the running servers to designate which server will receive the request. This way the same client IP address will always reach the same server while the servers stay the same.
# Ensuring that the server is up to date
Before proceeding, let us ensure that the server is up to date. Use this command to achieve this:
|
|
# Installing Haproxy
The package providing haproxy is available in the default Debian Repos. Install using this command:
|
|
Confirm the installation with this command:
|
|
# Configuring HAProxy as a load balancer
To configure HAProxy as a load balancer, we need to tell it what kind of connections it should be listening for and where the connections should be relayed to. This is done by creating a configuration file /etc/haproxy/haproxy.cfg
with the defining settings.
# Load balancing on layer 4
Upon installation, HAProxy will already have a template for configuring load balancer. Open the config file using your text editor, I am using vim:
|
|
Add this content to configure listening interface and backend interface where traffic will be load balanced to.
|
|
Replace the <server name>
with whatever you want to call your servers on the statistics page and the <private IP>
with the private IPs for the servers you wish to direct the web traffic to. This is the config that I am using in my server
|
|
The above defines a layer 4 load balancer with a front-end name http_front
listening to the port number 80
, which then directs the traffic to the default backend named http_back
. The additional stats URI /haproxy?stats
enables the statistics page at that specified address.
# Load balancing on layer 7
We can also configure the load balancer to work on layer 7. This is useful when parts of your web application are located on different hosts. This can be accomplished by conditioning the connection transfer for example by the URL.
Open the config file using your text editor, I am using vim:
|
|
Then add these config
|
|
The front end declares an ACL rule named url_blog
that applies to all connections with paths that begin with /blog
. use_backend
defines that connections matching the url_blog
condition should be served by the backend named blog_back
, while all other requests are handled by the default backend.
At the backend side, the configuration sets up two server groups, http_back
like before and the new one called blog_back
that servers specifically connections to example.com/blog
.
After making the configurations, save the file and restart HAProxy with the next command.
|
|
If you get any errors or warnings at startup, check the configuration for any mistypes and then try restarting again.
# Configuring SSL
If you have a private key file and a certificate file that you want to use with HAProxy, combine them with this command.
|
|
Then configure HAProxy to use the SSL certificate on the frontend.
|
|
# Testing the setup
With the HAProxy configured and running, open your load balancer server’s public IP in a web browser and check that you get connected to your backend correctly. The parameter stats uri in the configuration enables the statistics page at the defined address.
|
|
When you load the statistics page and all of your servers are listed in green your configuration was successful!
The statistics page contains some helpful information to keep track of your web hosts including up and down times and session counts. If a server is listed in red, check that the server is powered on and that you can ping it from the load balancer machine.
In case your load balancer does not reply, check that HTTP connections are not getting blocked by a firewall. Also, confirm that HAProxy is running with the command below.
|
|
# Password protecting the HaProxy statistics page
Having the statistics page simply listed at the front end, however, is publicly open for anyone to view, which might not be such a good idea. Instead, you can set it up to its own port number by adding the example below to the end of your haproxy.cfg
file. Replace the username and password with something secure.
|
|
After adding the new listen group, remove the old reference to the stats uri from the frontend group. When done, save the file and restart HAProxy again.
|
|
Then open the load balancer again with the new port number, and log in with the username and password you set in the configuration file.
|
|
Check that your servers are still reporting all green and then open just the load balancer IP without any port numbers on your web browser.
|
|
If your backend servers have at least slightly different landing pages you will notice that each time you reload the page you get the reply from a different host.
# Conclusions
That is it! In this guide, we learnt how to install and configure HAProxy to load balance traffic. This will increase your web application performance and availability.