HAProxy is a free, open source software that provides high availability and load balancing for TCP and HTTP-based applications. With HAProxy you can improve your web application‘s performance, reliability and scalability by distributing workload across multiple servers.
In this comprehensive guide, we will go through the steps to install, configure and set up HAProxy as a load balancer on Ubuntu 20.04.
Prerequisites
Before we get started, there are a few prerequisites:
- At least 3 Ubuntu 20.04 servers
- 1 HAProxy frontend load balancer server
- 2 Apache backend web servers
- Root privileges on all servers
- All servers must be on the same network
- Key-based authentication set up between servers for SSH access
Here is the network information for our servers:
Frontend Load Balancer:
Hostname: haproxy-1
IP Address: 192.168.1.10
Backend Web Server 1:
Hostname: web-1
IP Address: 192.168.1.11
Backend Web Server 2:
Hostname: web-2
IP Address: 192.168.1.12
Install Apache on the Backend Servers
We will first set up Apache on the two backend servers that will host our web content.
Configure Hosts File
On both web-1
and web-2
edit the hosts file to include the IP and hostname of the frontend HAProxy server:
sudo nano /etc/hosts
Add the following:
192.168.1.10 haproxy-1
Save and exit the file when finished.
Install Apache
Update apt repository and install Apache:
sudo apt update
sudo apt install apache2 -y
Enable and start Apache:
sudo systemctl enable apache2
sudo systemctl start apache2
Configure Apache
Let‘s add some customization so we can distinguish between the two backend servers when viewing web pages.
On web-1
:
echo ‘‘ | sudo tee /var/www/html/index.html
On web-2
:
echo ‘‘ | sudo tee /var/www/html/index.html
Allow traffic to Apache in the ufw firewall:
sudo ufw allow ‘Apache Full‘
At this point, both backend web servers should be correctly serving web pages.
Install and Configure HAProxy
Now let‘s install and configure HAProxy on our third server to load balance requests between the two Apache web servers.
Install HAProxy
On the haproxy-1
frontend server, update apt and install HAProxy:
sudo apt update
sudo apt install haproxy -y
Configure Hosts File
Just like on the backend servers, we need to add entries for them to /etc/hosts
on the HAProxy server:
sudo nano /etc/hosts
Add:
192.168.1.11 web-1
192.168.1.12 web-2
Save and exit.
Configure HAProxy
Create a new configuration file at /etc/haproxy/haproxy.cfg
:
sudo nano /etc/haproxy/haproxy.cfg
Add the following configuration. This will make HAProxy listen on port 80 and balance using the round-robin algorithm between our two Apache backends:
frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back
backend http_back
balance roundrobin
server web-1 192.168.1.11:80 check
server web-2 192.168.1.12:80 check
Save and exit the file.
Verify the configuration:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
If no errors are returned, enable and start the HAProxy service:
sudo systemctl enable haproxy
sudo systemctl start haproxy
Testing the Setup
Our load balancer is now ready! Let‘s test it out.
Test with Curl
Run the following curl command a few times from the HAProxy server itself:
curl localhost
You should see the different web server outputs alternating each request:
This verifies requests are being properly load balanced between the two Apache backend servers.
Test from a Web Browser
Now try accessing the HAProxy server‘s IP address from a web browser on your local machine:
http://192.168.1.10
The same behavior should be witnessed – the web page will switch between the content served from the two different web servers.
Monitoring with HAProxy Stats Page
The HAProxy stats page we configured provides metrics and statistics to monitor activity.
Access it at:
http://192.168.1.10:8080/haproxy?stats
This will display real-time data such as:
- Current sessions
- Bytes processed
- Connection rate
- Health status of backend servers
- etc.
Very useful for observing general system performance and troubleshooting issues!
Additional Configurations
There are many additional configuration options you may want to set up on your HAProxy load balancer, including:
- SSL/TLS encryption
- Basic authentication
- Custom health checks
- Access logs
- Advanced load balancing algorithms
- Application-specific tuning and optimizations
Refer to the official HAProxy Documentation for more details.
Conclusion
Configuring HAProxy to load balance between backend application servers is a simple and powerful way to improve performance, availability and reliability of your web applications.
In this guide, we went through the complete steps to get a basic load balancer set up on Ubuntu with Apache backend servers in just a few minutes!
There are many possibilities to build on this foundation depending on the specific needs of your infrastructure and applications. The HAProxy community and documentation provides plentiful resources to help you on your way.
Let me know in the comments if you have any questions!