HAProxy is a very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It’s free and open source application which is very lightweight as well. I checked few other alternatives like perlbal, pond etc. but found haproxy most competent performer.
I’m describing here the steps I followed to download, install and configure it in Ubuntu Server. We have 2 backend Web servers which will receive traffic from Load balancer host running HAProxy in front of them.
Step 1. Download, compile and install HAproxy from here :
$ cd /usr/src $ wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.11.tar.gz $ tar xzf haproxy-1.4.11.tar.gz $ cd haproxy-1.4.11 $ make install |
You can find an example of config file in /usr/src/haproxy-1.4.11/examples/haproxy.cfg which you copy in /etc/haproxy and update it per your requirement.
Step 2. Update config file per your requirements:
$ mkdir /etc/haproxy $ cp /usr/src/haproxy-1.4.11/examples/haproxy.cfg /etc/haproxy $ cd /etc/haproxy |
you can see configs I used for our server below:
$ cat haproxy.cfg global log 127.0.0.1 local0 log 127.0.0.1 local1 notice log loghost local0 info maxconn 4096 uid 99 gid 99 daemon #debug #quiet defaults log global mode http option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen web-service 0.0.0.0:80 option forwardfor option httpchk OPTIONS /health_check.html stats enable stats refresh 10s stats hide-version stats scope . stats uri /lb?stats stats realm LB2\ Statistics stats auth admin:Adm1nn server web1 10.x.x.x:80 check inter 2000 fall 3 server web2 10.x.x.x:80 check inter 2000 fall 3 |
Most important section in this config file is “listen” where we have several important options. Few are related to display stats and in the end there are entries of backend web server which actually serves the traffic.
I’ve used most of default options and update it little bit to accommodate my requirements. There’s lots of optimization needed and therefore this can be called as a bare configuration. I would write about further enhancements later after some more experiments.
Because backend web servers receive traffic from Load balancer so in usual case they will see IP address of Load balancer itself instead of real user, I’ve written a post earlier to fix this.
You can also use HAProxy to do content switching where it will divert traffic amoung several backend servers available based on specified conditions, I’ve written a detailed post on this topic earlier.
Step 3. Start the daemon:
After initial config, you can start daemon and check things. Let’s check syntax first:
$ haproxy -c -f /etc/haproxy/haproxy.cfg |
If no issues in config, let’s start he process:
$ haproxy -f haproxy.cfg |
HAProxy is started, you can quickly check its status page at http://your-lb-ip/lb?status where you need to supply user and password as per we specified in config file.
You can keep updating your config file and reload the haproxy service without having to stop/start it:
$ haproxy -f haproxy.cfg -p /var/run/haproxy.pid -st $(cat /var/run/haproxy.pid) |