Load Balancing with HAProxy
Introduction
HAProxy (High Availability Proxy) is a free, very fast, and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. In this tutorial, we will guide you through the steps to set up load balancing using HAProxy on a Linux system.
Prerequisites
Before we begin, ensure you have the following:
- A Linux server with root privileges
- Basic knowledge of Linux command line
- Multiple backend servers to load balance
Installing HAProxy
To install HAProxy on your Linux server, follow these steps:
sudo apt-get update
sudo apt-get install haproxy
Verify the installation by checking the HAProxy version:
haproxy -v
HA-Proxy version 2.0.13-2ubuntu0.3 2020/08/12
Configuring HAProxy
HAProxy configuration file is located at /etc/haproxy/haproxy.cfg
. Open this file with your preferred text editor:
sudo nano /etc/haproxy/haproxy.cfg
The configuration file consists of several sections:
- global: Settings that apply to the entire HAProxy instance.
- defaults: Default settings that apply to all the proxies unless overridden.
- frontend: Defines how requests are received by HAProxy.
- backend: Defines how requests are forwarded to backend servers.
Example Configuration
Here's an example configuration for load balancing HTTP requests between two backend servers:
global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners stats timeout 30s user haproxy group haproxy daemon defaults log global mode http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 frontend http_front bind *:80 default_backend http_back backend http_back balance roundrobin server server1 192.168.1.2:80 check server server2 192.168.1.3:80 check
In this configuration:
- The frontend section listens on port 80 and forwards requests to the backend section named
http_back
. - The backend section named
http_back
uses a round-robin load balancing algorithm to distribute requests betweenserver1
andserver2
.
Starting HAProxy
After configuring HAProxy, start the service using the following command:
sudo systemctl start haproxy
Enable HAProxy to start on boot:
sudo systemctl enable haproxy
Check the status of HAProxy to ensure it is running correctly:
sudo systemctl status haproxy
● haproxy.service - HAProxy Load Balancer Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-08-12 14:23:45 UTC; 5min ago
Testing HAProxy
To verify that HAProxy is correctly load balancing requests, you can use a web browser or a tool like curl
to make requests to the HAProxy server:
curl http://your-haproxy-server
Refresh the page or run the curl
command multiple times to see the requests being distributed to the backend servers.
Conclusion
In this tutorial, you have learned how to install and configure HAProxy for load balancing HTTP requests across multiple backend servers. HAProxy is a powerful tool that can handle high traffic loads and provide high availability for your applications.