HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world's most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions.

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can quickly deploy your cloud servers.

Once you have signed up, log into your Cloudwafer Client Area and deploy your Cloudwafer cloud server.

Updating System Packages

It is recommended that you update the system to the latest packages before beginning any major installations. Issue the command below:

sudo apt-get update && sudo apt-get upgrade

Installation:
To install HAProxy, type the command below followed by y when prompted to continue:

sudo apt-get install haproxy

Configuring HAProxy
The HAProxy configuration process involves three(3) major sources of parameters :

  • The arguments from the command-line, which always take precedence
  • The "global" section, which sets process-wide parameters
  • The proxies sections which can take form of "defaults", "listen", "frontend" and "backend".

Initial Configuration:
HAProxy's configuration file is stored in the /etc/haproxy/haproxy.cfg file. This file is created automatically during installation. This file defines a standard HAProxy setup without any load balancing.

Opening this file with an editor (In this guide, we use nano), you will notice the predefined sections: "global" and "defaults".

The global section defines system-level parameters including file locations, the user and group under which HAProxy is executed. The user haproxy and group haproxy are both created during installation.

The defaults section defines additional logging parameters and options related to timeouts and errors. By default, HAProxy logs both normal and error messages. You are allowed to keep the error logs in a separate log file by adding the following line to your configuration file.

option log-separate-errors

Configuring Load Balancing:

Configuring load balancing using HAProxy requires two types of nodes which are defined as frontend and backend.

  • frontend: This is the node by which HAProxy listens for connections.

Open /etc/haproxy/haproxy.cfg in a text editor and add the following configuration for the front end:

sudo nano /etc/haproxy/haproxy.cfg

Enter the following:

frontend cloudwaferfrontend
bind *:80
mode http
default_backend backendnodes

frontend

  • backend: HAProxy forward requests through the backend nodes.

Open /etc/haproxy/haproxy.cfg in a text editor and add the following configuration for the front end:

sudo nano /etc/haproxy/haproxy.cfg

backend backendnodes
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.1\r\nHost:localhost
server node1 196.61.217.241:8080 check
server node2 196.61.217.247:8080 check

The check option makes the load balancer perform health checks on the server.

Note: In this guide, 196.61.217.241 and 196.61.217.247 are used as the IP addresses for the backend. Replace these IP addresses with yours while carrying out your configurations.

backend-config

The stats node which is optional is used to monitor the load balancer and the other two nodes.

listen stats
bind :32700
stats enable
stats uri /
stats hide-version
stats auth user:pass

stats

The stats node will:

  • Listen on port 32700 for connections
  • Configured to hide the version of HAProxy
  • Require a password login. Replace pass123 with a more secure password. Also, it is recommended to disable stats login in production.

Restart the HAProxy service so that the new configuration can take effect:

sudo service haproxy restart

If haproxy successfully restarts, then your configurations are correct and all incoming requests to the HAProxy node at IP address public_ip will be forwarded to an internally networked node with an IP address of either 196.61.217.241 or 196.61.217.247.

These backend nodes will serve the HTTP requests and if either of these nodes fails the health check, they will not be used to serve any requests until they pass the test.

To view statistics and monitor the health of the nodes, visit the public_ip IP address or domain name of the frontend node in a web browser at the assigned port, e.g., http://public_ip:32700.

This will display statistics such as the number of times a request was forwarded to a particular node as well the number of current and previous sessions handled by the frontend node.

You can read more about HAProxy from the official documentation here.