Data Centers

How to install and setup HAProxy on Ubuntu 16.04

Looking to load balance your web servers? Here's how to do it with the help of HAProxy on Ubuntu Server 16.04.

haproxyhero.jpg
Image: Jack Wallen

If you serve up a web site from on premises, and are a looking for a way to add a layer of load balancing and high availability to your offering, HAProxy is an open-source solution that works TCP- and HTTP-based applications. This solution offers transparent connections, server offloading, policy enforcement, connection limiting, and more. If you're afraid adding such a layer to your web servers will be overly complicated, fear not. Installing and setting up HAProxy is remarkably easy.

I want to walk you through the process of installing HAProxy on the Ubuntu 16.04 Server platform and configure it to work with three web servers (each serving up the same content for load balancing purposes). For the sake of our tutorial, we will call these servers:

  • haproxy.local at IP 192.168.1.100
  • web1.local at IP 192.168.1.101
  • web2.local at IP 192.168.1.102
  • web3.local at IP 192.168.1.103

Installation

The first thing we must do is install HAProxy on our Ubuntu server, haproxy.local. This is done with the following command:

sudo apt-get install haproxy

Once the installation completes, you can check to ensure HAProxy is working with the command:

haproxy -v

The above command should output the HAProxy release and copyright information (Figure A).

Figure A

Figure A

HAProxy is ready for configuration.

Configuration

NOTE: You will need to alter the configuration below to suit your needs. The examples I am showing are for instructional purposes only.

The first thing you must do is add an entry for HAProxy and each of the web servers to to haproxy.local's /etc/hosts file. Open that file with the command sudo nano /etc/hosts. The entries will look like:

192.168.1.100 haproxy.local
​192.168.1.101 web1.local
​192.168.1.102 web2.local
​192.168.1.103 web3.local

Save and close that file. Let's also set the hostname of the HAProxy server. Issue the command sudo nano /etc/hostname and change the default hostname to haproxy.local. Now we must enable haproxy to start at boot. Issue the command sudo nano /etc/default/haproxy and add the the following line under the #CONFIG= option (Figure B):

ENABLED=1

Figure B

Figure B

Configuring HAProxy to start at boot.

Start the service with the command:

sudo service haproxy start

Now we must open up the HAProxy configuration file with the command sudo nano /etc/haproxy/haproxy.cfg. You want to skip over the Global and Default sections (leave them as is). At the bottom of this file we're going to add a front-end section (for the HAProxy server) and a back-end section (for our web servers). The front-end section looks like:

frontend Local_Server
bind 192.168.1.100:80
mode http
default_backend My_Web_Servers

The back-end section looks like:

backend My_Web_Servers
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web1.local  192.168.1.101:80
server web2.local  192.168.1.102:80
server web3.local  192.168.1.103:80

Add the above sections and save/close the configuration file.

Now we check the configuration file with the command:

sudo haproxy -c -f /etc/haproxy/haproxy.cfg

The test should come back to say the configuration is valid; you can then restart the service with the command:

sudo service haproxy restart

Your web servers are now being load balanced, in a round-robin fashion, by HAProxy.

Testing the setup

You can test to ensure HAProxy is no balancing the web servers with the command:

while true; do curl http://192.168.1.100; sleep 1; done

This should display the index.html file for each web server, round robin style. I created an easy to use index.html file on each of my web servers with the following contents:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<h1>This is webX</h1>
</body>
</html>

Where X is the number of the web server hosting (so web1, web2, and web3). The output of the test command would look like that in Figure C.

Figure C

Figure C

Testing HAProxy load balancing.

That command will continue running until you issue the key combination [Ctrl]+[c]. You can also point your web browser to the IP address of the HAProxy server and it will redirect you to one of the web servers, round robin style, to display the load balancing in action.

Simple website load balancing

You'd be hard-pressed to find an easier means of adding load balancing to your web servers. With this in place, you'll increase the performance and reliability of your web servers. To learn more of what HAProxy can do, issue the command man haproxy and read through the man page.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox