HAProxy is a free, reliable, high performance load balancing solution capable of proxying TCP and HTTP applications.
This article will outline how to set up a simple HAProxy server to allow you to load balance web site requests to one or more back-end web servers. While this example may not be suitable for a production set up on the internet, its a good start if you just want to set up a simple load balanced web site.
The following example is using Ubuntu, however the configuration will be the same regardless of the distribution. The steps to install HAProxy will vary depending on the distribution. It also assumes that the back end web servers are on different physical machines to the one running HAProxy.
$ sudo apt-get install haproxy
On Ubuntu, by default the HAProxy service is disabled. To enable it you need to modify the /etc/default/haproxy file.
$ sudo nano /etc/default/haproxy
Modify the ‘ENABLED=0’ line to ‘ENABLED=1’ as shown below.
# Set ENABLED to 1 if you want the init script to start haproxy. ENABLED=0 # Add extra flags here. #EXTRAOPTS="-de -m 16"
# Set ENABLED to 1 if you want the init script to start haproxy. ENABLED=1 # Add extra flags here. #EXTRAOPTS="-de -m 16"
Save and exit the editor.
The next step is to modify the HAProxy configuration file.
$ sudo nano /etc/haproxy/haproxy.cfg
In the default configuration file, there will likely be a number of ‘listen’ sections. These can be removed or commented out, as they wont be needed. You just want to keep the ‘global’ and ‘defaults’ sections for now.
Add in the following ‘frontend’ and ‘backend’ sections:
frontend http_in bind *:80 default_backend webservers backend webservers balance roundrobin server webserver1 192.168.50.11:80 server webserver2 192.168.50.12:80
What this does is sets up a proxy frontend called ‘http_in’, listening on port 80, on all IP addresses on the machine. This proxy frontend will pass all traffic through to the ‘backend’ which has been configured with the name ‘webservers’. This is set with the ‘default_backend’ setting pointing to ‘webservers’.
THe ‘backend’ section is where we configure what servers will be avaliable to load balance between, and what type of load balancing model to use. This example uses the round robin model, which basically just rotates evenly between the servers listed. In this example, we have named the server running on 192.168.50.11 as ‘webserver1’ and the server running on 192.168.50.12 as ‘webserver2’. These names dont have to resolve to anything as they are just used for identifiers within HAProxy.
The configuration file should now look similar to this:
global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 #chroot /usr/share/haproxy user haproxy group haproxy daemon #debug #quiet defaults log global mode http option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 frontend http_in bind *:80 default_backend webservers backend webservers balance roundrobin server webserver1 192.168.50.11:80 server webserver2 192.168.50.12:80
Save the configuration, and exit the editor.
Restart/Start HAProxy for the changes to take effect.
$ sudo /etc/init.d/haproxy restart
You should now be able to go to the url of the HAProxy machine, and have the requests redirected to the back end web servers that were set up in the ‘backend’ section.
There are a large number of configuration options avaliable to do things like weighted balancing, server status checking, URL redirection, stats pages, and more. Read the documentation on the HAProxy website (http://haproxy.1wt.eu/) for more information on the various configuration options available.