Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/articles/engineering-concepts-tutorials/)
-   -   Load Balancing with HAProxy (http://www.go4expert.com/articles/load-balancing-haproxy-t29548/)

pradeep 15Mar2013 13:47

Load Balancing with HAProxy
 
HAProxy stands for High Availability Proxy, is an open-source TCP/HTTP load balancer. In case you are new to the idea of a load balancer, the work of a load balancer is to distribute incoming requests to an array of upstream servers so as no single upstream server is overworked/overloaded by the incoming requests.

HAProxy has been reported to be highly efficient and fault tolerant, and it is also claimed that HAProxy has never crashed in a production environment. HAProxy is very configurable, you can chose for a variety of load balancing algorithms like Round-Robin, Source, LeastConn, etc., it also provides support for sticky session, provides detailed logs and monitoring stats. In this article we'll look at installing and setting up a basic HAProxy load balancer.

Installing HAProxy



Firstly, download the source tarball from HAProxy official website (http://haproxy.1wt.eu/), then untar, build and install:

Code:

$ tar -zvxf haproxy-1.4.22.tar.gz
$ cd haproxy-1.4.22
$ make
$ cp haproxy /usr/sbin/haproxy


Now, you have built and copied the standalone HAProxy executable to the sbin directory, you can run haproxy as a command.

Configuring HAProxy



There are lots of configuration options, it can be as easy as the installation or something very complicated, it all depends on your requirements. Here we'll look at a very simple configuration example, I've added comments for easier understanding of the configuration directives, you can find the complete configuration guide on the HAProxy website (http://haproxy.1wt.eu/#docs)

You can put the HAProxy configuration file wherever you want, for example I put it in /etc/conf/haproxy.conf

Code:

## global options
global
    ## maximum allowed connections
    maxconn 10000
    ## path to the PID file
    pidfile /var/run/haproxy.pid
    ## specify to run as a daemon
    daemon

defaults
    ## conifgure to server HTTP requests
    mode http
    ## maintain HTTP requests log
    option httplog
    ## add X-Forwarded-For header
    option forwardfor
   
listen http haproxy.go4expert.com:80
    mode tcp
    option tcplog
    ## specify algorithm
    balance roundrobin
    maxconn 10000
    ## specify server array
    server appserver1 192.168.1.1:80
    server appserver2 192.168.1.2:80


To start HAProxy with the configuration issue the following command:

Code:

$ haproxy -f /etc/conf/haproxy.conf


To view the stats, the URL would be haproxy.go4expert.com/haproxy?stats

I hope it has been helpful to you, post your experiences so that we all can share and learn.


All times are GMT +5.5. The time now is 18:24.