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
- 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
We 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
- Then, install HAProxy:
sudo dnf install haproxy
- Next, verify the installation:
rpm -qi haproxy
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
- Then, edit the configuration file:
sudo vi /etc/haproxy/haproxy.cfg
- 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
Remember 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
Remember 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
- Then, uncomment these lines:
module(load="imudp")
input(type="imudp" port="514") - Next, create an HAProxy configuration file:
sudo vi /etc/rsyslog.d/haproxy.conf
- After that, define log files:
local2.=info /var/log/haproxy-access.log
local2.notice /var/log/haproxy-info.log - Next, set SELinux rule:
sudo setsebool -P haproxy_connect_any 1
- Then, restart and enable Rsyslog:
sudo systemctl restart rsyslog
sudo systemctl enable rsyslog - Now, start and enable HAProxy:
sudo systemctl start haproxy
sudo systemctl enable haproxy - Next, verify HAProxy status:
sudo systemctl status haproxy
- Configure the firewall:
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd –reload
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
- Then, start and enable Apache:
sudo systemctl start httpd
sudo systemctl enable httpd - 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
- Then, restart Apache:
sudo systemctl restart httpd
Test HAProxy Load Balancing
Finally, test the HAProxy load balancer setup:
- Visit the load balancer’s IP:
http://loadbalancer-IP
- 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
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