Serverspace Black Friday
VB
October 17, 2022
Updated October 21, 2022

Install HAProxy load balancer on Ubuntu 20.04 LTS

Networks Ubuntu Web server

What is HAProxy

HAProxy is lightweight load-balancer with open-source code. You could use it for fault-tolerance infrastructure built or "hiding" project's real "location" for security reasons. In this article I'll describe you how to deploy load-balancer for web-project, located on two independent servers.

Basic requirements

If you want to use HAProxy, you should ensure that your infrastructure is satisfies the following conditions:

  • You need at least three servers - two of them as your project's "core servers" and last as HAProxy balancer;
  • Domain name with A-records "pointed" to the balancer's IP;
  • Your website content should be "deployed" to the both webservers.

Network preparing

Before starting HAProxy setup, we should define some "variables" such as servers IP-addresses and names. So step-by-step instruction is:

  • Create three servers via client area. Two of them will working as "backend", third server is HAProxy balancer. Final infrastructure schema at the picture below:

pic1

  • Change /etc/hosts files on each three servers, associate IPs with servers hostnames. Then check servers accessibility, run SSH-console to the haproxy server and send some ICMP packets to the "backend" workers;

cat <<EOF >> /etc/hosts
<backend_server_one_IP> <backend_server_one_hostname>
<backend_server_two_IP> <backend_server_two_hostname>
<haproxy_server_IP> <haproxy_server_hostname>
EOF

ping <LAN_servers_IPs>

pic2

 

 

  • Create firewall rule to block all incoming traffic to the "backend" servers, besides haproxy. Optional, to make management more comfortable, you can whitelist your own IP. The easiest way to do this - go to your server details settings, add the rules and click to the SAVE button. Remember, allow rules should be set before deny.

pic3

    Website deployment

    • If you whitelisted your own IP as described, the easiest way to deploy webserver and manage sites is install any web-panel e.g. HestiaCP. Install it on the first "backend" server as described here, then authorize in panel and create non-privileged user;

    pic1

    • Login as this account;

    pic4

    • Create website;

    pic5

    pic6

     

    • Repeat website deployment on second "backend" server;

    HAProxy setup

    Now the time to install load balancer. Please follow these:

    • Login to HAProxy server console, then update exist packages;
    apt-get update; apt upgrade -y

    pic7

    • Install balancer software;
    apt-get install haproxy -y

    pic8

    • Edit HAProxy config, just add followed code inside;

    frontend haproxy_web

      bind <haproxy_IP>:80

      default_backend web_back

    mode http

    backend web_back

      balance roundrobin

    server <backend_server_one_hostname> <backend_server_one_IP>:80

    server <backend_server_two_hostname> <backend_server_two_IP>:80

    pic9

    • Enable and start HAProxy service;
    service haproxy start; systemctl enable haproxy

    pic10

    Balancer checking

    Now you can open your website in browser. It will open successfully, but you couldn't ensure that balancer is working well. For test purposes you can do follow:

    • Login to the one "backend" server via SSH, go to website directory and rename index file;

    cd <website_root_dir>

    mv <index_filename> <index_filename.bkp>

    pic11

    • Create new index file with server IP;
    echo $(hostname) > index file

    pic12

    • Login to the other "backend" server via SSH and rename index file too;
    • Open your website in browser, then "refresh" the page a few times. You will see different information. It means balancer is working;

    pic13 pic14

    • Return "original" index files back:

    rm <index file>

    mv <index_filename.bkp> <index_filename>

    pic15 pic16

    Finally

    After this article reading you knew what is HAProxy and may install this load balancer to the server under Ubuntu 20.04 OS.

    Vote:
    1 out of 5
    Аverage rating : 1.4
    Rated by: 18
    1101 CT Amsterdam The Netherlands, Herikerbergweg 292
    +31 20 262-58-98
    700 300
    ITGLOBAL.COM NL
    700 300
    We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
    Use of Cookies and Privacy Policy.