When you serve up a website from on premises, and are a on the lookout for a means so as to add a layer of load balancing and excessive availability to your providing, HAProxy is an open-supply answer that works TCP- and HTTP-based mostly purposes. This answer presents clear connections, server offloading, coverage enforcement, connection limiting, and extra. For those who’re afraid including such a layer to your net servers might be overly difficult, worry not. Putting in and establishing HAProxy is remarkably straightforward.
I need to stroll you thru the method of putting in HAProxy on the Ubuntu 16.04 Server platform and configure it to work with three net servers (every serving up the identical content material for load balancing functions). For the sake of our tutorial, we’ll name these servers:
- haproxy.native at IP 192.168.1.one hundred
- web1.native at IP 192.168.1.a hundred and one
- web2.native at IP 192.168.1.102
- web3.native at IP 192.168.1.103
The very first thing we should do is set up HAProxy on our Ubuntu server, haproxy.native. That is carried out with the next command:
sudo apt-get set up haproxy
As soon as the set up completes, you possibly can verify to make sure HAProxy is working with the command:
The above command ought to output the HAProxy launch and copyright info (Determine A).
NOTE: You’ll need to change the configuration under to fit your wants. The examples I’m displaying are for educational functions solely.
The very first thing you should do is add an entry for HAProxy and every of the online servers to to haproxy.native’s /and so forth/hosts file. Open that file with the command sudo nano /and so on/hosts. The entries will seem like:
192.168.1.one hundred haproxy.nativethirteen; 192.168.1.one hundred and one web1.nativethirteen; 192.168.1.102 web2.nativethirteen; 192.168.1.103 web3.native
Save and shut that file. Let’s additionally set the hostname of the HAProxy server. Challenge the command sudo nano /and so on/hostname and alter the default hostname to haproxy.native. Now we should allow haproxy to start out at boot. Difficulty the command sudo nano /and so on/default/haproxy and add the the next line beneath the #CONFIG= choice (Determine B):
Begin the service with the command:
sudo service haproxy begin
Now we should open up the HAProxy configuration file with the command sudo nano /and so forth/haproxy/haproxy.cfg. You need to skip over the International and Default sections (depart them as is). On the backside of this file we will add a entrance-finish part (for the HAProxy server) and a again-finish part (for our net servers). The entrance-finish part seems to be like:
frontend Local_Serverthirteen; bind 192.168.1.one hundred:eightythirteen; mode httpthirteen; default_backend My_Web_Servers
The again-finish part seems to be like:
backend My_Web_Serversthirteen; mode httpthirteen; stability roundrobinthirteen; choice forwardforthirteen; http-request set-header X-Forwarded-Port %[dst_port]thirteen; http-request add-header X-Forwarded-Proto https if ssl_fc thirteen; choice httpchk HEAD / HTTP/1.1rnHost:localhostthirteen; server web1.native 192.168.1.a hundred and one:eightythirteen; server web2.native 192.168.1.102:eightythirteen; server web3.native 192.168.1.103:eighty
Add the above sections and save/shut the configuration file.
Now we verify the configuration file with the command:
sudo haproxy -c -f /and so forth/haproxy/haproxy.cfg
The check ought to come again to say the configuration is legitimate; you’ll be able to then restart the service with the command:
sudo service haproxy restart
Your net servers at the moment are being load balanced, in a spherical-robin trend, by HAProxy.
Testing the setup
You’ll be able to check to make sure HAProxy is not any balancing the online servers with the command:
whereas true; do curl http://192.168.1.one hundred; sleep 1; executed
This could show the index.html file for every net server, spherical robin type. I created a simple to make use of index.html file on every of my net servers with the next contents:
<html xmlns="http://www.w3.org/1999/xhtml">thirteen; <physique>thirteen; <h1>That is webX</h1>thirteen; </physique>thirteen; </html>
The place X is the variety of the online server internet hosting (so web1, web2, and web3). The output of the check command would seem like that in Determine C.
That command will proceed operating till you challenge the important thing mixture [Ctrl]+[c]. You may as well level your net browser to the IP tackle of the HAProxy server and it’ll redirect you to one of many net servers, spherical robin type, to show the load balancing in motion.
Easy web site load balancing
You would be exhausting-pressed to seek out a neater technique of including load balancing to your net servers. With this in place, you will improve the efficiency and reliability of your net servers. To study extra of what HAProxy can do, problem the command man haproxy and skim by way of the person web page.