Learn how to install & configure HAProxy for TCP Load Balancing with Health Checks. Our HAProxy Support team is here to answer the queries and concerns.
Install & Configure HAProxy for TCP Load Balancing with Health Checks
HAProxy is a widely used high-performance TCP/HTTP load balancer. It distributes network traffic across multiple servers to improve the performance and reliability of web applications.
Today, we will walk through installing HAProxy on popular Linux distributions, configuring TCP load balancing, and setting up health checks to ensure high availability.
If you’re looking for more advanced setups, such as Kubernetes-based load balancing, check out our Kubernetes HAProxy Load Balancer guide.
An Overview:
How to Install HAProxy
- To start, log in to the server as a limited sudo user and follow the installation steps according to the Linux distribution.
- For Ubuntu 24.04 LTS:
sudo apt install haproxy
- For CentOS Stream 9:
sudo dnf install haproxy
- For openSUSE Leap 15.6:
sudo zypper in haproxy
- For Ubuntu 24.04 LTS:
- Then, check the installed version to confirm HAProxy is installed successfully:
sudo haproxy -v
- Now, start the HAProxy service:
sudo systemctl start haproxy
- We can enable HAProxy to start automatically on boot:
sudo systemctl enable haproxy
- Then, check the service status to verify it’s running:
systemctl status haproxy
The HAProxy Configuration File
HAProxy’s core settings are defined in the configuration file located at:
/etc/haproxy/haproxy.cfg
We can edit it with a text editor:
sudo nano /etc/haproxy/haproxy.cfg
This file contains all the rules for load balancing, health checks, and traffic flow control. If you’re configuring HAProxy in a firewall or network gateway setup, take a look at how to configure HAProxy with pfSense for additional context.
How to Configure TCP Load Balancing
HAProxy uses frontend and backend blocks to define how incoming traffic should be handled and routed.
Here is an example of Frontend configuration:
frontend http_front
bind *:80
default_backend http_back
Here is an example of Backend configuration:
backend http_back
balance roundrobin
server server1 192.168.1.101:80 check
server server2 192.168.1.102:80 check
server server3 192.168.1.103:80 check
The check keyword enables basic health checks to ensure that servers are available before routing traffic to them. For a real-world example of how HAProxy balances traffic to database servers, see our detailed article on MySQL Load Balancing with HAProxy.
How to Enable TCP Health Checks
HAProxy supports both active and passive health checks.
- Active Health Checks
Send application-specific queries at regular intervals:
server server1 192.168.1.101:80 check inter 2000
- Passive Health Checks
HAProxy automatically monitors TCP traffic for connection errors and removes failed servers from the pool.
Putting It All Together: Load Balancing with Health Checks
- Open the HAProxy configuration file:
sudo nano /etc/haproxy/haproxy.cfg
- Then, add the frontend and backend configurations as shown above.
- Finally, save the file and restart HAProxy:
sudo systemctl restart haproxy
Testing TCP Load Balancing
Open a web browser and visit the HAProxy instance’s public IP:
http://<HAProxy_Public_IP>
We will see a web page from one of the backend servers. Refresh the page or open it in a new tab to cycle through responses from backend2 and backend3.
This demonstrates that HAProxy is successfully distributing traffic using the Round Robin method.
Verify Health Checks
To test if health checks are functioning:
- Power off one backend server (e.g., backend1) from the cloud manager.
- Refresh the browser tabs. HAProxy should now skip backend1 and distribute traffic only between backend2 and backend3.
- On the HAProxy instance, check the logs:
sudo tail -f /var/log/haproxy.log
We will see a warning indicating that `server1` is marked DOWN, like:[WARNING] : Server web-test/server1 is DOWN, reason: Layer4 connection problem, info: “No route to host”
This confirms that the health check mechanism is working properly.
For use cases involving database performance, you might also want to explore how to set up and maintain Percona load balancing using HAProxy.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Setting up HAProxy for TCP load balancing with built-in health checks keeps the web application available, even when some servers go offline. By monitoring backend server health and distributing traffic intelligently, HAProxy helps us deliver consistent performance and uptime for our users.
In brief, our Support Experts demonstrated how to install & configure HAProxy for TCP Load Balancing with Health Checks.
0 Comments