How to Install and Configure HAProxy on Rocky Linux 8
When you deploy an application on the Internet, the availability, scalability and performance of the application are very important for a better user experience. There are many technologies that introduce redundancy, such as load balancing, clustering and proxy.
HAProxy is a free, open source and reliable load balancer designed for high traffic websites. It load balances incoming traffic by distributing it across multiple backend servers.
In this article we show you how to install HAProxy on Rocky Linux 8.
For the purpose of this tutorial, we will use the following setup:
Server IP Address
HAProxy 172.16.10.10
Backend1 172.16.10.11
Backend2 172.16.10.12
Prerequisites
- A server running RockyLinux 8.
- A root password is configured on the server.
Install HAProxy
By default, the HAProxy package is available in the Rocky Linux 8 default repo. You can install it with the following command:
dnf install haproxy -y
Once the HAProxy is installed, start the HAProxy service and enable it to start at system reboot:
systemctl start haproxy
systemctl enable haproxy
Configure HAProxy
Next, you will need to edit /etc/haproxy/haproxy.cfg file and modify two sections frontend and backend. In the Frontend section, you will need to define HAProxy IP its port, stats URI and backend name. In the Backend section, you will need to define the load balance algorithm, backend server’s name, IPs, and port.
nano /etc/haproxy/haproxy.cfg
Modify the following sections:
frontend http_balancer bind 172.16.10.10:80 option http-server-close option forwardfor stats uri /haproxy?stats default_backend Apache_webservers backend apache_webservers mode http balance roundrobin option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost server apache1 172.16.10.11:80 check server apache2 172.16.10.12:80 check
Save and close the file when you are finished then you will need to edit /etc/rsyslog.conf file.
nano /etc/rsyslog.conf
Uncomment the following lines:
module(load="imudp") input(type="imudp" port="514")
Save and close the file then create haproxy.conf file for rsyslog with the following command:
nano /etc/rsyslog.d/haproxy.conf
Add the following lines:
local2.=info /var/log/haproxy-access.log local2.notice /var/log/haproxy-info.log
Next, start the rsyslog service and enable it to start at system reboot:
systemctl start rsyslog
systemctl enable rsyslog
Finally, restart the HAProxy service to apply the changes:
systemctl restart haproxy
You can also verify the status of the HAProxy service with the following command:
systemctl status haproxy
You should see the following output:
? haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2021-08-03 03:10:37 EDT; 1min 16s ago Process: 44032 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 44036 (haproxy) Tasks: 2 (limit: 12524) Memory: 3.1M CGroup: /system.slice/haproxy.service ??44036 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ??44111 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid Aug 03 03:10:36 RockyLinux8 systemd[1]: Starting HAProxy Load Balancer... Aug 03 03:10:37 RockyLinux8 systemd[1]: Started HAProxy Load Balancer.
Configure Firewall
Next, you will need to configure firewalld on all servers and allow port 80 through the firewall.
You can run the following command to allow port 80:
firewall-cmd --permanent --add-port=80/tcp
Next, reload the firewalld service to apply the changes.
firewall-cmd --reload
Configure Backend Servers
Next, you will need to install the Apache server on both backend servers. Run the following command to install the Apache server.
dnf install httpd -y
Once Apache server has been installed, start the Apache service and enable it to start at system reboot:
systemctl start httpd
systemctl enable httpd
Next, you will need to modify the default index.html file on both backend servers.
On the first backend, edit the index.html file with the following command:
nano /usr/share/httpd/noindex/index.html
Remove all lines and add the following line:
Welcome to First Apache Web Server!
Save and close the file when you are finished.
On the second backend, edit the index.html file with the following command:
nano /usr/share/httpd/noindex/index.html
Remove all lines and add the following line:
Welcome to Second Apache Web Server!
Save and close the file when you are finished.
Verify HAProxy
At this point, HAProxy is configured to forward all incoming requests to the backend servers based on the load-balancing algorithm. Now, it's time to check whether HAProxy is working properly or not.
Open your web browser and type the HAProxy IP in the URL http://172.16.10.10. You should see your first apache webserver page:
Next, refresh the same page again, you should see your second apache webserver page:
This confirms that HAProxy is working as expected and it is distributing traffic between two backend servers.
Conclusion
Congratulations! you have successfully installed and configured the HAProxy load balancer on Rocky Linux 8. You can now implement HAProxy in the production environment to achieve high performance and scalability.