Tips on how to set up and setup HAProxy on Ubuntu sixteen.04

Picture: Jack Wallen

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 hundred
  • web1.native at IP 192.168.1.a hundred and one
  • web2.native at IP
  • web3.native at IP

Set up

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:

haproxy -v

The above command ought to output the HAProxy launch and copyright info (Determine A).

Determine A

Figure A

HAProxy is prepared for configuration.


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: hundred haproxy.native&#thirteen;
​ hundred and one web1.native&#thirteen;
​ web2.native&#thirteen;
​ 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):


Determine B

Figure B

Configuring HAProxy to start out at boot.

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_Server&#thirteen;
bind hundred:eighty&#thirteen;
mode http&#thirteen;
default_backend My_Web_Servers

The again-finish part seems to be like:

backend My_Web_Servers&#thirteen;
mode http&#thirteen;
stability roundrobin&#thirteen;
choice forwardfor&#thirteen;
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:localhost&#thirteen;
server web1.native  192.168.1.a hundred and one:eighty&#thirteen;
server web2.native;
server web3.native

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 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="">&#thirteen;
<h1>That is webX</h1>&#thirteen;

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.

Determine C

Figure C

Testing HAProxy load balancing.

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.

Additionally see

Leave a Reply

Your email address will not be published.