HAProxy is a freely distributed, open source load balancer. It load balances TCP service.

It is free, reliable and fast, offering high-availability, load-balancing and proxy-ing for TCP & HTTP applications. It’s an excellent choice for high traffic or bandwidth-consuming web sites.

For the last few years it has been a standard load balancer, bundled with many Linux distributions, and is also by default deployed with cloud platforms.

Basic diagram of an HAProxy setup:

How to install HAproxy in ubuntu?

Open terminal and type

To check version:

We will have to enable High Availability Proxy
 /etc/ default/ haproxy.
Set ENABLED option to 1 i.e. ENABLED is equal 1

For verification, execute init script with out parameters. You will see as follows:

With this step, we have installed HAProxy.

Now we will have to create a setup with two Apache Web Server instances and one HAProxy instance.

Below is the setup information:

Now, we will configure HAproxy

First, let’s backup the original file. Now create your own haproxy.cf using any text editor in the path /etc -> haproxy -> haproxy.cfg  as:

Here, the log means the syslog server in which the log messages are delivered.
The maxconn mentions the number of concurrent connections on front end. Default value = 2000 & needs to be tuned to your system configurations.
User & group directive alters HAProxy to the specified group/user.

This shouldn’t be altered.

These are default values. The redispatch opens the session distribution if connection failures happens. Session is overridden in case the web server goes down.
Retries sets the number of retries to be performed on the web server if there’s a connection failure.
Timeout directives modifies value. The contimeout  defines the time required to wait for webserver to make a successful connection.
Clitimeout and srvtimeout is applied when client/server acknowledges or even send a data during TCP.

These are the settings that HAProxy recommends :

Please note, the above configuration is for both front end and back end. We configured HAProxy in order to listen to port 80 – webfarms (a name to identify applications).

Stats tracks the connection of statistic page. It can be viewed with stats uri here it’s

The directive – balance shows the load-balancing algorithm in use. Various algorithms are:

  • rr – Round Robin
  • static-rr (Static Round Robin)
  • Least Connection
  • Source
  • URI
  • URL parameter

The syntax of server directive is

In logs & alerts, the name mentioned here will appear.

After you complete configuring, start HAProxy:

Test Load-Balancing & Fail-over

Now, we will have to append name of server in default index.html located default at var -> www -> index.html

On Instance 2 (webserver01 IP- 192.168.205.16), insert line as:

On Instance 3 (webserver02 IP- 192.168.205.17), insert line as:

Next, open any browser and navigate through IP of HAproxy i.e. http://192.168.205.15

When you reload, you’ll find load is distributed to the web server.

For initial time if I check http://192.168.205.15 , I get:

For 2nd time, if I reload the page then I get :

Also, check HAproxy by navigating to

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail