Introduction
In this guide, we’ll walk you through the process of installing and configuring HAProxy for load balancing. We’ll cover the following topics:
- Installation of HAProxy
- Configuration of HAProxy
- Testing and Troubleshooting
- Monitoring and Logs
By the end of this guide, you’ll have a working HAProxy setup that distributes incoming HTTP requests across two Apache web servers.
Scenario
For this guide, we’ll use the following scenario:
- Load Balancer (HAProxy): IP Address – 192.168.1.128
- Ansible Control Node: IP Address – 192.168.1.129
- Machine1 (Apache Web Server): IP Address – 192.168.1.127
- Machine2 (Apache Web Server): IP Address – 192.168.1.131
The HAProxy load balancer will distribute incoming HTTP requests to Machine1 and Machine2, which are running Apache web servers.
Step 1: Installing HAProxy
First, let’s install HAProxy on the Load Balancer machine. Open your terminal and run the following commands:
sudo apt update
sudo apt install haproxy
This will update your package list and install HAProxy.
Step 2: Configuring HAProxy
Once HAProxy is installed, we need to configure it. Open the HAProxy configuration file using a text editor of your choice. Here, we’ll use nano:
sudo nano /etc/haproxy/haproxy.cfg
Add the following lines to set up the frontend and backend configurations:
frontend http_front
bind *:80
default_backend http_back
backend http_back
server server1 192.168.1.127:80 check
server server2 192.168.1.131:80 check
Save the file and exit the editor.
Step 3: Testing and Troubleshooting
After configuring HAProxy, it’s crucial to test the setup. You can check the syntax of your configuration file with the following command:
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
If everything is fine, you’ll see an output like Configuration file is valid. Restart the HAProxy service to apply the changes:
sudo systemctl restart haproxy
You can also monitor the HAProxy logs to troubleshoot any issues:
sudo tail -f /var/log/haproxy.log
Step 4: Monitoring and Logs
Monitoring is essential for any production environment. Here are some commands to help you keep an eye on your HAProxy setup:
- Check HAProxy Status:
sudo systemctl status haproxy - View Active Internet Connections:
netstat -tulnorss -tuln
You can also view the Apache logs on the backend servers to ensure that the load is being distributed:
sudo tail -f /var/log/apache2/access.log
This will show you real-time access logs, helping you understand how the load is being balanced.
Conclusion
Congratulations! You’ve successfully installed and configured HAProxy for load balancing. You’ve also learned how to test, troubleshoot, and monitor your setup. This guide should provide you with a solid foundation for implementing HAProxy in your own environment.


Leave a Reply