How to Install HAProxy Load Balancer on RHEL 8 - CentLinux

Latest

Monday, 22 February 2021

How to Install HAProxy Load Balancer on RHEL 8

How to Install HAProxy Load Balancer on RHEL 8

HAProxy is a Reliable, High Performance TCP/HTTP Load Balancer. Here, you will learn how to install HAProxy load balancer on RHEL 8.

 

Table of Contents:

     

    What is HAProxy?:

    HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C programming language and has a reputation for being fast and efficient (in terms of processor and memory usage).

    HAProxy is used by a number of high-profile websites including GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Slack, Speedtest.net, Tumblr, Twitter and Tuenti and is used in the OpsWorks product from Amazon Web Services.(Source: Wikipedia)

     

    HAProxy Features:

    HAProxy has the following features:

    • Layer 4 (TCP) and Layer 7 (HTTP) load balancing
    • URL rewriting
    • Rate limiting
    • SSL/TLS termination
    • Gzip compression
    • Proxy Protocol support
    • Health checking
    • Connection and HTTP message logging
    • HTTP/2
    • Multithreading
    • Hitless Reloads
    • gRPC Support
    • Lua and SPOE Support
    • API Support
    • Layer 4 Retries
    • Simplified circuit breaking

     

    Environment Specification:

    We are using a minimal RHEL 8 virtual machine with following specifications.

    • CPU - 3.4 Ghz (2 cores)
    • Memory - 2 GB
    • Storage - 20 GB
    • Operating System - Red Hat Enterprise Linux (RHEL) 8.3
    • Hostname – haproxy-01.centlinux.com
    • IP Address - 192.168.116.238 /24

     

    Updating the Linux Server:

    Connect with haproxy-01.centlinux.com as root user with the help of a ssh client.

    Build cache for the Red Hat Subscription Management repositories.

    [root@haproxy-01 ~]# dnf makecache Updating Subscription Management repositories. Red Hat Enterprise Linux 8 for x86_64 - BaseOS 2.6 kB/s | 4.1 kB 00:01 Red Hat Enterprise Linux 8 for x86_64 - BaseOS 335 kB/s | 28 MB 01:26 Red Hat Enterprise Linux 8 for x86_64 - AppStre 3.3 kB/s | 4.5 kB 00:01 Red Hat Enterprise Linux 8 for x86_64 - AppStre 253 kB/s | 26 MB 01:46 Last metadata expiration check: 0:00:08 ago on Sun 21 Feb 2021 11:41:31 AM EST. Metadata cache created.

    Update installed software packages in your Linux server as follows.

    [root@haproxy-01 ~]# dnf update -y ... Upgraded: NetworkManager-1:1.26.0-13.el8_3.x86_64 NetworkManager-libnm-1:1.26.0-13.el8_3.x86_64 NetworkManager-team-1:1.26.0-13.el8_3.x86_64 NetworkManager-tui-1:1.26.0-13.el8_3.x86_64 dbus-1:1.12.8-12.el8_3.x86_64 dbus-common-1:1.12.8-12.el8_3.noarch dbus-daemon-1:1.12.8-12.el8_3.x86_64 dbus-libs-1:1.12.8-12.el8_3.x86_64 dbus-tools-1:1.12.8-12.el8_3.x86_64 device-mapper-8:1.02.171-5.el8_3.2.x86_64 device-mapper-event-8:1.02.171-5.el8_3.2.x86_64 device-mapper-event-libs-8:1.02.171-5.el8_3.2.x86_64 device-mapper-libs-8:1.02.171-5.el8_3.2.x86_64 dnf-plugin-subscription-manager-1.27.18-1.el8_3.x86_64 dracut-049-95.git20200804.el8_3.4.x86_64 dracut-config-rescue-049-95.git20200804.el8_3.4.x86_64 dracut-network-049-95.git20200804.el8_3.4.x86_64 dracut-squash-049-95.git20200804.el8_3.4.x86_64 glibc-2.28-127.el8_3.2.x86_64 glibc-common-2.28-127.el8_3.2.x86_64 glibc-langpack-en-2.28-127.el8_3.2.x86_64 kernel-tools-4.18.0-240.15.1.el8_3.x86_64 kernel-tools-libs-4.18.0-240.15.1.el8_3.x86_64 kexec-tools-2.0.20-34.el8_3.2.x86_64 kmod-25-16.el8_3.1.x86_64 kmod-libs-25-16.el8_3.1.x86_64 lvm2-8:2.03.09-5.el8_3.2.x86_64 lvm2-libs-8:2.03.09-5.el8_3.2.x86_64 python3-perf-4.18.0-240.15.1.el8_3.x86_64 python3-subscription-manager-rhsm-1.27.18-1.el8_3.x86_64 python3-syspurpose-1.27.18-1.el8_3.x86_64 subscription-manager-1.27.18-1.el8_3.x86_64 subscription-manager-rhsm-certificates-1.27.18-1.el8_3.x86_64 tuned-2.14.0-3.el8_3.2.noarch Installed: kernel-4.18.0-240.15.1.el8_3.x86_64 kernel-core-4.18.0-240.15.1.el8_3.x86_64 kernel-modules-4.18.0-240.15.1.el8_3.x86_64 python3-chardet-3.0.4-7.el8.noarch python3-pysocks-1.6.8-3.el8.noarch python3-requests-2.20.0-2.1.el8_1.noarch python3-urllib3-1.24.2-4.el8.noarch Complete!

    The above command has updated the Linux kernel, therefore, we are required to reboot the Linux operating system with new Kernel.

    [root@haproxy-01 ~]# reboot

     

    Installing Apache Web Server on RHEL 8:

    In this article, you are required two web servers and a HAProxy load balancing server. But, we do not have three machines, therefore, we are configuring the two Apache virtual hosts on the same HAProxy server and then setup the HTTP Load Balancer for them.

    Install Apache web server on your RHEL server by using dnf command.

    [root@haproxy-01 ~]# dnf install -y @httpd ... Installed: apr-1.6.3-11.el8.x86_64 apr-util-1.6.1-6.el8.x86_64 apr-util-bdb-1.6.1-6.el8.x86_64 apr-util-openssl-1.6.1-6.el8.x86_64 httpd-2.4.37-30.module+el8.3.0+7001+0766b9e7.x86_64 httpd-filesystem-2.4.37-30.module+el8.3.0+7001+0766b9e7.noarch httpd-tools-2.4.37-30.module+el8.3.0+7001+0766b9e7.x86_64 mailcap-2.1.48-3.el8.noarch mod_http2-1.15.7-2.module+el8.3.0+7670+8bf57d29.x86_64 mod_ssl-1:2.4.37-30.module+el8.3.0+7001+0766b9e7.x86_64 redhat-logos-httpd-81.1-1.el8.noarch sscg-2.3.3-14.el8.x86_64 Complete!

    Create "Document Root" directories for your Apache virtual hosts.

    [root@haproxy-01 ~]# mkdir /var/www/host-{80,8080}

    Generate the index.html (the default webpage) for your Apache virtual hosts.

    [root@haproxy-01 ~]# vi /var/www/host-80/index.html

    Add following HTML code therein.

    <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-80</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1> </BODY> </HTML>

    Similarly, for second virtual host.

    [root@haproxy-01 ~]# vi /var/www/host-8080/index.html

    Add following HTML code.

    <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-8080</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1> </BODY> </HTML>

    Add virtual host configurations in Apache web server.

    [root@haproxy-01 ~]# vi /etc/httpd/conf.d/test.conf

    Add following directives in this file.

    Listen 80 Listen 8080 <VirtualHost 192.168.116.238:80> ServerName haproxy-01.centlinux.com DocumentRoot "/var/www/host-80" </VirtualHost> <VirtualHost 192.168.116.238:8080> ServerName haproxy-01.centlinux.com DocumentRoot "/var/www/host-8080" </VirtualHost>

    Apache web server is using ports 80/tcp and 8080/tcp, therefore, you have to allow these network ports in your Linux firewall.

    [root@haproxy-01 ~]# firewall-cmd --permanent --add-port={80,8080}/tcp success [root@haproxy-01 ~]# firewall-cmd --reload success

    Enable and start the Apache web server.

    [root@haproxy-01 ~]# systemctl enable --now httpd.service Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service â /usr/lib/systemd/system/httpd.service.

    Verify Apache virtual hosts, by using the curl command.

    [root@haproxy-01 ~]# curl http://haproxy-01.centlinux.com:80 <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-80</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1> </BODY> </HTML> [root@haproxy-01 ~]# curl http://haproxy-01.centlinux.com:8080 <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-8080</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1> </BODY> </HTML>

    If you got the above output then your Apache virtual hosts are configured successfully.

     

    Installing HAProxy Load Balancer on RHEL 8:

    You can install HAProxy from Red Hat subscription management repositories by using dnf command.

    [root@haproxy-01 ~]# dnf install -y haproxy Updating Subscription Management repositories. Last metadata expiration check: 1:14:25 ago on Sun 21 Feb 2021 11:46:31 AM EST. Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: haproxy x86_64 1.8.23-5.el8 rhel-8-for-x86_64-appstream-rpms 1.3 M Transaction Summary ================================================================================ Install 1 Package Total download size: 1.3 M Installed size: 4.1 M Downloading Packages: haproxy-1.8.23-5.el8.x86_64.rpm 229 kB/s | 1.3 MB 00:05 -------------------------------------------------------------------------------- Total 229 kB/s | 1.3 MB 00:05 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: haproxy-1.8.23-5.el8.x86_64 1/1 Installing : haproxy-1.8.23-5.el8.x86_64 1/1 Running scriptlet: haproxy-1.8.23-5.el8.x86_64 1/1 Verifying : haproxy-1.8.23-5.el8.x86_64 1/1 Installed products updated. Installed: haproxy-1.8.23-5.el8.x86_64 Complete!

    HAProxy load balancer is installed on your Linux server.

     

    Configure HAProxy Log in RHEL 8:

    Now you need to configure the HAProxy log. This log is very helpful in troubleshooting HAProxy on Linux server.

    Open rsyslog configuration file in vim text editor.

    [root@haproxy-01 ~]# vi /etc/rsyslog.conf

    Locate and uncomment following two lines in this file.

    module(load="imudp") # needs to be done just once input(type="imudp" port="514")

    Create a rsyslog configuration file for setting HAProxy logfile location.

    [root@haproxy-01 ~]# vi /etc/rsyslog.d/haproxy.conf

    Add following directives in this file.

    # HAProxy Logging local2.* /var/log/haproxy.log

    Restart rsyslog service to load changes.

    [root@haproxy-01 ~]# systemctl restart rsyslog.service

     

    Configure HTTP Load Balancer in HAProxy:

    Open HAProxy configuration file in vim text editor.

    [root@haproxy-01 ~]# vi /etc/haproxy/haproxy.cfg

    Add following directives at the end of this file.

    # HAProxy Load Balancer for Apache Web Server frontend http-loadbalancer bind 192.168.116.238:9000 default_backend web-servers backend web-servers mode http balance roundrobin stats enable stats auth centlinux:Ahmer@1234 server webserver-01 192.168.116.238:80 check server webserver-02 192.168.116.238:8080 check

    In above configurations, we have setup a HAProxy load balancer named http-loadbalancer, running on the network port 9000/tcp.

    Our HTTP load balancer will perform round robin load balancing between two web servers. i.e. webserver-01 and webserver-02.

    Here, we have used Apache virtual hosts, therefore we are using same IP address with different ports. You can also use the IP Address or Hostname of your web servers, if running services on different machines.

    You must allow the service port 9000/tcp in your Linux firewall, to make it accessible across the computer network.

    [root@haproxy-01 ~]# firewall-cmd --permanent --add-port=9000/tcp success [root@haproxy-01 ~]# firewall-cmd --reload success

    Make sure you have completed above configurations correctly. Thereafter, enable and start the HAProxy service.

    [root@haproxy-01 ~]# systemctl enable --now haproxy.service Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service â /usr/lib/systemd/system/haproxy.service.

    Execute curl command to access your HAProxy load balancer multiple times.

    [root@haproxy-01 ~]# curl http://haproxy-01.centlinux.com:9000 <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-80</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1> </BODY> </HTML> [root@haproxy-01 ~]# curl http://haproxy-01.centlinux.com:9000 <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-8080</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 8080</H1> </BODY> </HTML> [root@haproxy-01 ~]# curl http://haproxy-01.centlinux.com:9000 <HTML> <HEAD> <TITLE>TEST PAGE AT HOST-80</TITLE> <BODY> <H1>TEST PAGE AT VIRTUAL HOST RUNNING AT PORT 80</H1> </BODY> </HTML>

    You can see that the HAProxy load balancer is rotating the service requests between the two web servers in round robin fashion.

    However, you can see the HAProxy statistics and logs in /var/log/haproxy.log file. But you can also view the Haproxy integrated statistics report via your web browser.

    For this purpose, you can open http://haproxy-01.centlinux.com:9000/haproxy?stats in a web browser.

    01-install-haproxy-load-balancer-rhel-8-statistics-report

    Feel difficulty in understanding the above configurations, then your should buy and read RHCSA Red Hat Enterprise Linux 8 (UPDATED): Training and Exam Preparation Guide (EX200), Second Edition by Asghar Ghori.

     

    Conclusion:

    In this article, we have successfully installed and configured HAProxy load balancer on RHEL 8 server.

    No comments:

    Post a comment