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:
- 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>
- 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.
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;
- Login as this account;
- Create website;
- 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
- Install balancer software;
apt-get install haproxy -y
- 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
- Enable and start HAProxy service;
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:
- 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>
- Create new index file with server IP;
echo $(hostname) > index file
- 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;
- Return "original" index files back:
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.