How to Set Up a HAProxy Load Balancer on Ubuntu
How to Set Up a HAProxy Load Balancer on Ubuntu
Setting up a load balancer is essential for managing high traffic on your web applications. HAProxy (High Availability Proxy) is a powerful and widely-used open-source load balancer and proxy server. In this guide, we will walk through the steps required to set up HAProxy on Ubuntu, ensuring high availability and efficient load distribution across your servers.
Prerequisites
Before you begin, ensure you have the following:
- Ubuntu Server: This guide is written for Ubuntu 20.04 or later. Ensure your server is updated.
- Root Access: You need root or sudo privileges to install and configure HAProxy.
- Multiple Backend Servers: At least two web servers to distribute traffic between (for this example, we will use two instances of Nginx).
Step 1: Update Your System
First, log in to your Ubuntu server and update the package list. This ensures you have the latest software.
sudo apt update
sudo apt upgrade -y
Step 2: Install HAProxy
Install HAProxy using the package manager:
sudo apt install haproxy -y
Once the installation is complete, you can verify that HAProxy is installed by checking its version:
haproxy -v
Step 3: Configure HAProxy
The configuration file for HAProxy is located at /etc/haproxy/haproxy.cfg. Before making any changes, it’s wise to back up the original configuration file:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
Next, open the configuration file in your preferred text editor:
sudo nano /etc/haproxy/haproxy.cfg
Basic Configuration
The default configuration file contains several sections. We will modify these to set up our load balancer.
- Global Settings: These settings apply to the HAProxy instance. You can adjust the logging level and other parameters here:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
- Default Settings: These settings apply to all frontend and backend sections unless overridden:
defaults
log global
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
- Frontend Configuration: This section defines how HAProxy listens for incoming traffic. Replace your_domain.com with your actual domain or IP address.
frontend http_front
bind *:80
acl is_webrequest path_beg /
use_backend web_servers if is_webrequest
Backend Configuration: Here, you specify the backend servers that HAProxy will distribute traffic to. Replace backend1 and backend2 with the IP addresses or hostnames of your web servers.
backend web_servers
balance roundrobin
server backend1 192.168.1.101:80 check
server backend2 192.168.1.102:80 check
Final Configuration
Your configuration file should resemble the following:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemondefaults
log global
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000msfrontend http_front
bind *:80
acl is_webrequest path_beg /
use_backend web_servers if is_webrequestbackend web_servers
balance roundrobin
server backend1 192.168.1.101:80 check
server backend2 192.168.1.102:80 check
After editing the configuration file, save and exit the editor.
Step 4: Enable and Start HAProxy
To enable HAProxy to start at boot, run:
sudo systemctl enable haproxy
- Start the HAProxy service:
sudo systemctl start haproxy
- To verify that HAProxy is running, check its status:
sudo systemctl status haproxy
Step 5: Test Your Configuration
You can test if HAProxy is working by accessing your server’s IP address or domain in a web browser. If configured correctly, you should be routed to one of your backend servers.
Checking HAProxy Stats
HAProxy provides a built-in stats page to monitor performance and traffic. To enable it, add the following to your configuration:
listen stats
bind *:8080
stats enable
stats uri /stats
stats refresh 10s
- After adding this section, restart HAProxy:
sudo systemctl restart haproxy
You can now access the stats page by visiting http://your_server_ip:8080/stats.
Thank you for visiting our page! If you’re interested in exploring more articles about Linux systems and monitoring your network with Cacti, feel free to check out the links below.
How to Monitor Your Network with Cacti on Ubuntu