Tuesday, May 29, 2012

Howto Install and configure HAproxy on CentOS 6

HAproxy offers a great load balancing and high availability solution, being very customizable and easy to implement it became a De-facto standard for HA/LB solution in many production environments.

In this short tutorial I will show how to install and configure HAproxy (basic configuration) to achieve load balancing between two web servers.

Before we start a brief overview of my test environment:
  • HAproxy server - Has 2 NIC's - eth0 is configured with external IP address, eth1 is configured with internal IP address
  • 2 Web servers - each with 1 NIC configured with internal ip addresses (, both listening on port 80
The following diagram summarizes the architecture:

I will not go into the web server configurations themselves, but be sure to check both web server nodes are reachable from HAproxy server node and both are listening on port 80.
A good practice is to create a test html page with the node name on every back-end node, so you will actually see the LB in action when making request to the front-end node.

It's time to get our hands dirty.

You will need the 'epel' repository in order to install HAproxy via yum:
Install 'epel' repository:

# rpm -Uvh http://ftp-stud.hs-esslingen.de/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

Next, install HAproxy and enable it's service on boot:

#yum install haproxy -y
#chkconfig haproxy on

Backup your initial HAproxy configuration file:
#cp /etc/haproxy/haproxy.cfg{,.bak}

Get a sample configuration file:
#wget http://c818095.r95.cf2.rackcdn.com/haproxy.cfg -O /etc/haproxy/haproxy.cfg

It's time to edit our configuration file:
#vi /etc/haproxy/haproxy.cfg 

Under ,the default configuration banner "#HTTP default webfarm" locate the following line:
listen webfarm ...:80

And change it to your public external IP address, for example:
listen webfarm

Next, make sure to add the IP addresses of your web servers :

#replace with web node private ip
       server web01 check
       server web02 check

You may also want to limit the 'maxconn' parameter to limit the maximum allowed parallel connections to the server.
There are of course _much more_ parameters that I will not get into in this tutorial such as: ACL's, LB type's and many others.

Save the file and restart HAproxy:
#/etc/init.d/haproxy restart

Check that HAproxy is indeed listening on it's public address on port 80 via:
#lsof -i :80
 haproxy 32607 haproxy    4u  IPv4 134165      0t0  TCP server01:http (LISTEN)

... your load balancer is ready to receive requests. 

Every http request to will be redirected between the back-end nodes


Elvis1 said...

great tutorial, thanks.
I wish you made another one with the mentioned parameters for a more "in depth" view.

Thanks yet again.

Elvis1 from www.youwatched.com

sabalera said...

Interesting! I want to try this. Thank you for your help.