
How to Install and Configure HAProxy on Fedora: A Comprehensive Guide
In the world of web infrastructure, ensuring high availability and reliability is paramount. HAProxy, a powerful and open-source solution, stands out as a leading high-performance TCP/HTTP load balancer and reverse proxy. By distributing web traffic across multiple servers, HAProxy prevents any single server from becoming a point of failure, dramatically improving the uptime and performance of your applications.
This guide provides a step-by-step walkthrough for installing and configuring HAProxy on Fedora Linux, transforming your single-server setup into a resilient, load-balanced architecture.
Prerequisites
Before we begin, ensure you have the following:
- A system running a recent version of Fedora Linux.
- Access to a user account with sudo or root privileges.
- At least two backend web servers to balance traffic between (for testing purposes).
Step 1: Installing HAProxy on Fedora
The first step is to install the HAProxy package from Fedora’s default repositories. It’s always a best practice to update your system’s package index before installing new software.
- Update Your System Packages: Open your terminal and run the following command to ensure all your existing packages are up to date.
bash
sudo dnf update -y
- Install HAProxy: Once the update is complete, install HAProxy using the
dnf
package manager.
bash
sudo dnf install haproxy -y
- Verify the Installation: After the installation finishes, you can verify that HAProxy is installed correctly and check its version.
bash
haproxy -v
This command will output the installed version of HAProxy, confirming the installation was successful.
Step 2: Starting and Enabling the HAProxy Service
With HAProxy installed, the next step is to start the service and enable it to launch automatically on system boot. This ensures your load balancer will be active even after a server reboot.
- Start the HAProxy Service: Use
systemctl
to start the HAProxy daemon.
bash
sudo systemctl start haproxy
- Enable HAProxy on Boot: To make the service persistent across reboots, enable it with this command.
bash
sudo systemctl enable haproxy
- Check the Service Status: You can confirm that the service is running without errors by checking its status.
bash
sudo systemctl status haproxy
Look for anactive (running)
message in the output, which indicates the service is operating correctly.
Step 3: Configuring a Basic Load Balancer
The real power of HAProxy lies in its configuration file. This file, located at /etc/haproxy/haproxy.cfg, defines how traffic is received and where it is sent. Let’s configure a simple HTTP load balancer.
First, open the configuration file in a text editor like nano
or vim
:
sudo nano /etc/haproxy/haproxy.cfg
An HAProxy configuration is typically divided into four key sections: global
, defaults
, frontend
, and backend
.
global
: Sets process-wide security and performance parameters.defaults
: Defines default parameters for all subsequentfrontend
andbackend
sections.frontend
: Describes a set of listening sockets that accept client connections.backend
: Describes a set of servers to which the proxy will forward incoming connections.
Here is a practical example configuration. You can replace the existing content or append this to your haproxy.cfg
file.
global
log /dev/log local0
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
# Frontend: How requests are received
frontend http_frontend
bind *:80
default_backend http_backend
# Backend: Where requests are sent
backend http_backend
balance roundrobin
server web-01 192.168.1.10:80 check
server web-02 192.168.1.11:80 check
In this example:
- The
frontend
namedhttp_frontend
listens for all incoming traffic on port 80. - It then forwards this traffic to the
backend
namedhttp_backend
. - The backend uses a
roundrobin
balancing algorithm, which distributes requests evenly across the defined servers. - Replace
192.168.1.10
and192.168.1.11
with the actual IP addresses of your web servers. Thecheck
parameter enables health checks, so HAProxy will stop sending traffic to a server if it becomes unresponsive.
Step 4: Configuring Firewall and SELinux
By default, Fedora’s firewall and SELinux policies will block incoming web traffic. You must create rules to allow HAProxy to function correctly.
- Allow HTTP Traffic Through the Firewall: If your frontend is listening on port 80, you need to allow the
http
service throughfirewalld
.
bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
If you are also load-balancing HTTPS traffic on port 443, run this as well:
bash
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
- Configure SELinux: SELinux may prevent HAProxy from binding to network ports. To allow this, you can set the appropriate SELinux boolean.
bash
sudo setsebool -P haproxy_connect_any=1
The-P
flag makes this change persistent across reboots.
Step 5: Validating and Applying Your Configuration
Before applying any changes, it is crucial to validate your configuration file for syntax errors. This simple step can prevent downtime.
Validate the Configuration File: Run the following command.
haproxy -c -f /etc/haproxy/haproxy.cfg
If the file is valid, you will see the message “Configuration file is valid”. If there are errors, the output will tell you which line is causing the issue.
Reload the HAProxy Service: Once validated, apply the new configuration by reloading the service. A reload is a graceful way to apply changes without dropping existing connections.
bash
sudo systemctl reload haproxy
Your HAProxy load balancer is now live! You can test it by navigating to the public IP address of your HAProxy server in a web browser. Each time you refresh the page, your request should be served by a different backend server according to the roundrobin
algorithm.
Source: https://kifarunix.com/setup-haproxy-load-balancer-on-fedora-30-fedora-29/