HAProxy is a free, open-source software that provides high availability, load balancing, and proxying for TCP and HTTP-based applications. It is commonly used to distribute incoming network traffic across multiple servers to ensure that no single server becomes overwhelmed. In this article, we are going to teach you how to configure HAProxy on AlmaLinux 8, a popular Linux distribution.
Prerequisites:
– A Linux VPS Server running AlmaLinux 8
– A non-root user with administrative privileges
Before doing anything, you should launch an Interactive Terminal. First, you need to install HAProxy with the following command:
sudo dnf install haproxy
In response to the question “Is this OK?“, enter “Y“.
After the installation, you should enable HAProxy:
sudo systemctl enable haproxy.service
The output would be like this:
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
Now You have to Start HAProxy by running the following command. It will run with a default configuration on the server.
sudo systemctl start haproxy.service
You can check the Status of HAProxy with the following command:
sudo systemctl status haproxy.service
If you see the Active (running) section in your output, it indicates that HAProxy is running properly on your server.
First, you should open /etc/haproxy/haproxy.cfg in your desired editor:
sudo vi /etc/haproxy/haproxy.cfg
Now you need to press “i” to navigate to INSERT mode. After that find the line log 127.0.0.1 local2 and add a # to the beginning of the line.
You can comment it out by doing this:
. . . # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # #log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid . . .
Then add the following line after the commented-out line:
log /dev/log local0
The edited /etc/haproxy/haproxy.cfg section should have the following lines:
. . . # local2.* /var/log/haproxy.log # #log 127.0.0.1 local2 log /dev/log local0 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid . . .
The chroot restricts the HAProxy process to access files in the /var/lib/haproxy directory. The log /dev/log local0 line creates a file inside that directory that Rsyslog uses to collect entries from.
Now press ESC, type:wq, and then press Enter again to save and close the file.
Finally, you need to create the /var/lib/haproxy/dev directory because it does not exist by default:
sudo mkdir /var/lib/haproxy/dev
In the last step Restart HAProxy with the following command:
sudo systemctl restart haproxy.service
The default Rsyslog configuration in AlmaLinux 8 does not handle HAProxy reports, you need to open a new /etc/rsyslog.d/99-haproxy.conf file using your desired editor to collect reports from the HAProxy service:
sudo vi /etc/rsyslog.d/99-haproxy.conf
Now you should press “i” to navigate to INSERT mode and paste the following command into the file:
$AddUnixListenSocket /var/lib/haproxy/dev/log # Send HAProxy messages to a dedicated logfile :programname, startswith, "haproxy" { /var/log/haproxy.log stop }
A Unix domain socket is created in the specified location using the above command. The :programname, startswith, “haproxy” section specifies the file that Rsyslog writes the log entries to that it collects from the socket. Then save the file and exit.
Next, you need to determine if SELinux applies access control on the AlmaLinux 8 system. You can check the current SELinux policy with the following command:
getenforce
If you get Permissive or Disabled output, you can restart Rsyslog by running the following command:
sudo systemctl restart rsyslog
By default, the Haproxy package is sent with a configuration file that creates an HTTP listener socket on port 5000. The configuration points to an existing backend server, so any request to the port will result in an HTTP 503 error.
First, you should create an HTTP request:
curl -si http://127.0.0.1:5000
Now you need to check /var/log/haproxy.log for each HTTP 503 response:
sudo grep -E 'NOSRV.+503' /var/log/haproxy.log
That is it.
Configuring HAProxy on AlmaLinux 8 enables organizations to enhance the availability, scalability, and performance of their web services. By following this guide, users can deploy HAProxy effectively, leveraging its robust features to achieve seamless load balancing and high availability. With AlmaLinux 8 providing a stable and secure foundation, HAProxy becomes an invaluable tool for optimizing web infrastructure and delivering exceptional user experiences. We suggest that you also read the article How to Install a Proxy on AlmaLinux.
How useful was this post?
Click on a star to rate it!
Average rating 5 / 5. Vote count: 3
No votes so far! Be the first to rate this post.
In this guide, we will take you through the step-by-step process of setting up this powerful analyti...
Samba is a widely used open-source software suite that allows interoperability between Linux and Win...
What is your opinion about this Blog?