Load Balancing on DreamCompute with HAProxy

Introduction

Load balancers are designed to host multiple applications behind the
same IP address to save cost and to allow applications to horizontally
scale out instances running on multiple virtual machines.
One of the most popular open source load balancers is
HAProxy. It is well supported,
featureful, high performing, and widely available.

In this tutorial, we will be creating a virtual machine inside of
DreamCompute that acts as a load balancer with HAProxy. We’ll be using
Ubuntu 14.04 as the base operating system image for our load balancer.

Prepping Your Virtual Machine

Create a virtual machine in DreamCompute using your favorite method, a
subsonic flavor should be enough. Make sure to assign an SSH key and
allow access to HTTP (port 80) and SSH (port 22) in the Access &
Security group. Associate a public IPv4 “floating” IP address to this
machine.

Click the More dropdown tab on in the Actions column of your
instance list.

Click Associate Floating IP.

Note

This is only necessary if you have private networking enabled

The following dialog box appears:

Allocate a floating IP with the port associated with the private IP
address of your load balancer virtual machine, and then click the
Associate button.

Once you’ve associated the IP address, give DreamCompute a few
seconds to complete the association, and then you should be able
to SSH into your instance via its IPv4 address.

Note

This is only necessary if you have private networking enabled

4. SSH into your instance via its IPv4 address (the default user is
ubuntu, you can’t login as root).

Now, we’re ready to get HAProxy installed and configured.

Install HAProxy

Updating Packages

Before installing HAProxy, its important to be sure that your
operating system is up to date with the latest security fixes and
packages available from Ubuntu. While logged in as ubuntu, simply
run:

[user@server]$ sudo apt-get upgrade

Follow the prompts, and allow apt to update your system.

Installing HAProxy

[user@server]$ sudo apt-get install haproxy

Configure HAProxy

For the purposes of our tutorial, we’re going to assume that you have
two applications that you would like to deploy behind HAProxy. The
first application will be on a single virtual machine and the second
application will be horizontally scaled across two virtual machines.

When we are complete with our configuration, our deployment will look
something like this: