Bobcares

How to Setup HAProxy on RHEL

by | Jul 7, 2024

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

  1. First, open the hosts file in a text editor:
    sudo vi /etc/hostsCopy Code
  2. 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 Code

    We have to replace `xx.xxx.x.a`, `xx.xxx.x.b`, and `xx.xxx.x.c` with our IP addresses.

  3. Save the changes and exit.

Install HAProxy on RHEL

  1. Next, it is time to install HAProxy. So, update packages as seen here:
    sudo dnf update -yCopy Code
  2. Then, install HAProxy:
    sudo dnf install haproxyCopy Code
  3. Next, verify the installation:
    rpm -qi haproxyCopy Code

Configure HAProxy to Act as a Load Balancer

  1. Now, back up the original configuration file:
    sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.bakCopy Code
  2. Then, edit the configuration file:
    sudo vi /etc/haproxy/haproxy.cfgCopy Code
  3. 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 Code

    Remember to replace `xx.xxx.x.a` with the HAProxy node’s IP address.

  4. 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 Code

    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:

  1. Edit the Rsyslog configuration file:
     sudo vi /etc/rsyslog.confCopy Code
  2. Then, uncomment these lines:
    module(load="imudp")
    input(type="imudp" port="514")Copy Code
  3. Next, create an HAProxy configuration file:
    sudo vi /etc/rsyslog.d/haproxy.confCopy Code
  4. After that, define log files:
    local2.=info   /var/log/haproxy-access.log
    local2.notice  /var/log/haproxy-info.logCopy Code
  5. Next, set SELinux rule:
    sudo setsebool -P haproxy_connect_any 1Copy Code
  6. Then, restart and enable Rsyslog:
    sudo systemctl restart rsyslog
    sudo systemctl enable rsyslogCopy Code
  7. Now, start and enable HAProxy:
    sudo systemctl start haproxy
    sudo systemctl enable haproxyCopy Code
  8. Next, verify HAProxy status:
    sudo systemctl status haproxyCopy Code
  9. Configure the firewall:
    sudo firewall-cmd --add-port=80/tcp --permanent
    sudo firewall-cmd –reloadCopy Code

Setup Web Servers

Now it is time to set up our web servers to work with HAProxy:

  1. First, install Apache:
    sudo dnf install httpd -yCopy Code
  2. Then, start and enable Apache:
    sudo systemctl start httpd
    sudo systemctl enable httpdCopy Code
  3. 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
  4. Then, restart Apache:
    sudo systemctl restart httpdCopy Code

Test HAProxy Load Balancing

Finally, test the HAProxy load balancer setup:

  1. Visit the load balancer’s IP:
    http://loadbalancer-IPCopy Code
  2. Then, verify the round-robin algorithm by reloading the page to see if it directs us to the second web server.
  3. Next, run the curl command to confirm that the round-robin algorithm is applied.
  4. After that, view HAProxy statistics:
     http://loadbalancer-IP/haproxy?statsCopy 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

Submit a Comment

Your email address will not be published. Required fields are marked *

Speed issues driving customers away?
We’ve got your back!