Sie sind auf Seite 1von 10

10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.

04

 English |   Deutsch Log in or Sign up

Tutorials Tags Forums Contribute Subscribe ISPConfig News

 Tutorial search 

Tutorials Web Server Load­Balancing with HAProxy on Ubun…

On this page
Web Server
Load­ What is HAProxy?
Installing HAProxy
Balancing Configuring HAProxy
Testing Load­Balancing and Fail­over
with HAProxy
on Ubuntu
14.04

What is HAProxy?

HAProxy(High Availability Proxy) is an open­source load­balancer which can load balance any TCP
service. HAProxy is a free, very fast and reliable solution that offers load­balancing, high­availability,
and proxying for TCP and HTTP­based applications. It is particularly well suited for very high traffic
web sites and powers many of the world's most visited ones.

Since it's existence, it has become the de­facto standard open­source load­balancer. Although it
does not advertise itself, but is used widely. Below is a basic diagram of how the setup looks like:

Installing HAProxy

I am using Ubuntu 14.04 and install it by:

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 1/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

apt­get install haproxy

You can check the version by:

haproxy ­v

We need to enable HAProxy to be started by
the init script /etc/default/haproxy. Set
ENABLED option to 1 as:

ENABLED=1

To verify if this change is done properly, execute the init script of HAProxy without any parameters.
You should see the following:

$ service haproxy <press_tab_key>
reload   restart  start    status   stop

HAProxy is now installed. Let us now create a setup in which we have 2(two) Apache Web Server
instances and 1(one) HAProxy instance. Below is the setup information:

We will be using three systems, spawned virtually through VirtualBox:

Instance 1 ­ Load Balancer
Hostname: haproxy
OS: Ubuntu 
Private IP: 192.168.205.15

Instance 2 ­ Web Server 1
Hostname: webser01
OS: Ubuntu with LAMP
Private IP: 192.168.205.16

Instance 2 ­ Web Server 2
Hostname: webserver02
OS: Ubuntu with LAMP
Private IP: 192.168.205.17

Here is the diagram of how the setup looks like:

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 2/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

Let us now configure HAProxy.

Configuring HAProxy

Backup the original file by renaming it:

mv /etc/haproxy/haproxy.cfg{,.original}

We'll create our own haproxy.cfg file. Using your favorite text editor create the
/etc/haproxy/haproxy.cfg file as:

global
        log /dev/log   local0
        log 127.0.0.1   local1 notice
        maxconn 4096
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout     5000
        clitimeout     50000
        srvtimeout     50000

listen webfarm 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    balance roundrobin
    option httpclose
    option forwardfor
    server webserver01 192.168.205.16:80 check
    server webserver02 192.168.205.17:80 check

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 3/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

Explanation:

global
        log /dev/log   local0
        log 127.0.0.1   local1 notice
        maxconn 4096
        user haproxy
        group haproxy
        daemon

The log directive mentions a syslog server to which log messages will be sent.
The maxconn directive specifies the number of concurrent connections on the front­end. The
default value is 2000 and should be tuned according to your system's configuration.
The user and group directives changes the HAProxy process to the specified user/group. These
shouldn't be changed.

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout     5000
        clitimeout     50000
        srvtimeout     50000

The above section has the default values. The option redispatch enables session redistribution in
case of connection failures. So session stickness is overriden if a web server instance goes down. 
The retries directive sets the number of retries to perform on a web server instance after a
connection failure. 
The values to be modified are the various timeout directives. The contimeout option specifies the
maximum time to wait for a connection attempt to a web server instance to succeed.
The clitimeout and srvtimeout apply when the client or server is expected to acknowledge or
send data during the TCP process. HAProxy recommends setting the client and server timeouts to
the same value.

listen webfarm 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    balance roundrobin
    option httpclose
    option forwardfor
    server webserver01 192.168.205.16:80 check
    server webserver02 192.168.205.17:80 check

Above block contains configuration for both the frontend and backend. We are configuring HAProxy
to listen on port 80 for webfarm which is just a name for identifying an application. 
The stats directives enable the connection statistics page. This page can viewed with the URL
mentioned in stats uri so in this case, it is http://192.168.205.15/haproxy?stats a demo
of this page can be viewed here.

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 4/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

The balance directive specifies the load balancing algorithm to use. Algorithm options available
are:

Round Robin (roundrobin),
Static Round Robin (static­rr),
Least Connections (leastconn),
Source (source),
URI (uri) and
URL parameter (url_param).

Information about each algorithm can be
obtained from the official documentation.

The server directive declares a backend
server, the syntax is:

server <server_name> <server_addre
ss>[:port] [param*]
    

The name we mention here will appear in logs and alerts. There are some more parameters
supported by this directive and we'll be using the check parameter in this article. The check
option enables health checks on the web server instance otherwise, the web server instance is ?
always considered available.

Once you're done configuring start the HAProxy service:

sudo service haproxy start

Testing Load­Balancing and Fail­over

We will append the server name in both the default index.html file located by default at
/var/www/index.html

On the Instance 2 ­ Web Server 1 (webserver01 with IP­ 192.168.205.16), append below line as:

sudo sh ­c "echo \<h1\>Hostname: webserver01 \(192.168.205.16\)\<\/h1\> >>
/var/www/index.html"

On the Instance 3 ­ Web Server 2 (webserver02 with IP­ 192.168.205.17), append below line as:

sudo sh ­c "echo \<h1\>Hostname: webserver02 \(192.168.205.17\)\<\/h1\> >>
/var/www/index.html"

Now open up the web browser on local machine and browse through the haproxy IP i.e.
http://192.168.205.15

Each time you refresh the tab, you'll see the load is being distributed to each web server. Below is
screenshot of my browser:

For the first time when I visit http://192.168.205.15 , I get:
https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 5/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

And for the second time, i.e. when I refresh the page, I get:

You can also check the haproxy stats by visiting http://192.168.205.15/haproxy?stats

There's more that you can do to this setup. Some ideas include:

take one or both web servers offline to test what happens when you access HAProxy
configure HAProxy to serve a custom maintenance page
configure the web interface so you can visually monitor HAProxy statistics
https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 6/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

change the scheduler to something other than round­robin
configure prioritization/weights for particular servers

That's all!

 view as pdf |   print

Share this page: Tweet 67 Recommend 176 15

10 Comment(s)
Add comment
Name *   Email *  

    

p

Submit comment
I'm not a robot
reCAPTCHA
Privacy - Terms

Comments

From: Chono Reply  

Great tutorial, I was thinking about it for some time.. Is that HAproxy has to be installed on extra
VirtualBox instance or can it run on one of the Web Server?
Thanks

From: Ildefonso Reply  

Can this be used as a load balancer for ADSL modems?
I have three ADSL modems with a very bad service from my ISP (mainly due to the distance from the
telco's central to my office) and over twenty PCs. I wish I could use something similar in order to
send my PCs outbond internet trafic to  the ADSLs, in a way that does not saturate the bandwidth of
the single modem we are currently using. I have a Linux bot that works as a router and gateway,

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 7/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

connected to a single modem.
Any advice would be much appreciated.

From: Sanchit Jain Rasiya Reply  

I am not sure of if it can be used for ADSL load balancing. By the way, how many broadband lines
do you have? If you 3 lines, you can configure your Linux Box as a load balancer. Check out­
http://unix.stackexchange.com/questions/138956/implementing­load­balancing­on­any­linux­
distro
You can also have a look at this device­ http://www.amazon.com/TP­LINK­TL­R470T­Broadband­
Changeable­Ethernet/dp/B005SYQBN8/ref=sr_1_22?s=pc&ie=UTF8&qid=1427375224&sr=1­
22&keywords=load+balancer
Hope it helps!

From: Sanchit Jain Rasiya Reply  

Thanks Chono. Not necessarily on VM. But it would be strongly suggested to have it separate on
production so that it is independent of any host. If the host with both services is down, it will be a
problematic situation.

From: Ildefonso Reply  

Thanks Sanchit
I have not three, but four broadband lines, so I will be trying your suggestion as of next weekend.
Regards

From: hipr Reply  

I am now wondering, how to set up an htts and SSL cetrificate, i.e. where goes what and how to
configure it...

From: Zeru Reply  

I'm using similar configuration as in the above but I can't get a reply for high connection rates. For
instance, for ­­rate 500 and num_conn 10, that is, when the number of reuest greater than 5000,
the haproxy has no min, avg, and max reply rate.
Thanks,

From: LandonC Reply  

Thanks ­­ works like a charm for Solr load­balancing with SolrCloud and ZooKeeper.

From: Tom Dings Reply  

Used this basic explanation to balance 64 web servers, and 128 mysql database machines. Works out
very well.

From: Anthony Reply  

I've managed to start up HAProxy successfully. But when I try to access my VIP, I got this error
"503, Service Unavailable"

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 8/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

i am am sure that both my web server is up. I am able to access directly using the web server IP.
 The default web location is /var/www/html/index.html, where should I change the directory to point
to the correct one?
Appreciate your advice.
 
thank you,

Tutorials Web Server Load­Balancing with HAProxy on Ubun…

,,
    A new generation
           of mobility is 
helping our business
REIMAGINE

Sign up now!

     

 Tutorial Info

Author: Sanchit Jain Rasiya
Tags: linux, ubuntu, server

 Share This Page

Tweet 67

Recommend 176

15

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 9/10
10/29/2015 Web Server Load­Balancing with HAProxy on Ubuntu 14.04

Xenforo skin by Xenfocus Contact Help Imprint

Howtoforge © projektfarm GmbH. Terms

https://www.howtoforge.com/tutorial/ubuntu­load­balancer­haproxy/ 10/10

Das könnte Ihnen auch gefallen