Let’s take a look at how to setup HAProxy on RHEL. Our HAProxy Support team is here to help you with your questions and concerns.
How to Setup HAProxy on RHEL
If you are looking for a guide to help you set up HAProxy on RHEL, you are in luck. Our Experts have put together this detailed guide to get you started.
Update Hosts File on All Nodes
- First, open the hosts file in a text editor:
sudo vi /etc/hosts
Copy Code - Then, add the following entries:
xx.xxx.x.a haproxy-rhel-09 xx.xxx.x.b apache-web-1 xx.xxx.x.c apache-web-2
Copy CodeWe have to replace `xx.xxx.x.a`, `xx.xxx.x.b`, and `xx.xxx.x.c` with our IP addresses.
- Save the changes and exit.
Install HAProxy on RHEL
- Next, it is time to install HAProxy. So, update packages as seen here:
sudo dnf update -y
Copy Code - Then, install HAProxy:
sudo dnf install haproxy
Copy Code - Next, verify the installation:
rpm -qi haproxy
Copy Code
Configure HAProxy to Act as a Load Balancer
- Now, back up the original configuration file:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.bak
Copy Code - Then, edit the configuration file:
sudo vi /etc/haproxy/haproxy.cfg
Copy Code - Next, configure the frontend:
frontend load_balancer bind xx.xxx.x.a:80 option http-server-close option forwardfor stats uri /haproxy?stats default_backend webservers
Copy CodeRemember to replace `xx.xxx.x.a` with the HAProxy node’s IP address.
- After that, configure the backend:
backend webservers mode http balance roundrobin stats enable stats auth linuxtechi:Techi@1234 option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost server apache-web-1 xx.xxx.x.b:80 server apache-web-2 xx.xxx.x.c:80
Copy CodeRemember to replace `xx.xxx.x.b` and `xx.xxx.x.c` with the web server nodes’ IP addresses.
Configure Rsyslog
For logging, we have to configure Rsyslog to handle HAProxy logs:
- Edit the Rsyslog configuration file:
sudo vi /etc/rsyslog.conf
Copy Code - Then, uncomment these lines:
module(load="imudp") input(type="imudp" port="514")
Copy Code - Next, create an HAProxy configuration file:
sudo vi /etc/rsyslog.d/haproxy.conf
Copy Code - After that, define log files:
local2.=info /var/log/haproxy-access.log local2.notice /var/log/haproxy-info.log
Copy Code - Next, set SELinux rule:
sudo setsebool -P haproxy_connect_any 1
Copy Code - Then, restart and enable Rsyslog:
sudo systemctl restart rsyslog sudo systemctl enable rsyslog
Copy Code - Now, start and enable HAProxy:
sudo systemctl start haproxy sudo systemctl enable haproxy
Copy Code - Next, verify HAProxy status:
sudo systemctl status haproxy
Copy Code - Configure the firewall:
sudo firewall-cmd --add-port=80/tcp --permanent sudo firewall-cmd –reload
Copy Code
Setup Web Servers
Now it is time to set up our web servers to work with HAProxy:
- First, install Apache:
sudo dnf install httpd -y
Copy Code - Then, start and enable Apache:
sudo systemctl start httpd sudo systemctl enable httpd
Copy Code - Next, create sample HTML files:
echo "<h1>Welcome to Web Server 01</h1>" > /var/www/html/index.html echo "<h1>Welcome to Web Server 02</h1>" > /var/www/html/index.html
Copy Code - Then, restart Apache:
sudo systemctl restart httpd
Copy Code
Test HAProxy Load Balancing
Finally, test the HAProxy load balancer setup:
- Visit the load balancer’s IP:
http://loadbalancer-IP
Copy Code - Then, verify the round-robin algorithm by reloading the page to see if it directs us to the second web server.
- Next, run the curl command to confirm that the round-robin algorithm is applied.
- After that, view HAProxy statistics:
http://loadbalancer-IP/haproxy?stats
Copy Code
Let us know in the comments if you need further help with these steps.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to setup HAProxy on RHEL
0 Comments