11.12.2024

Install HAProxy load balancer on Ubuntu 20.04 LTS

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:

Network preparing

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

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>

 

 

Website deployment

 

HAProxy setup

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

apt-get update; apt upgrade -y

apt-get install haproxy -y

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

service haproxy start; systemctl enable haproxy

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:

cd <website_root_dir>

mv <index_filename> <index_filename.bkp>

echo $(hostname) > index file

rm <index file>

mv <index_filename.bkp> <index_filename>

Finally

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