How to configure haproxy load balancing on amazon ec2

Confused how to configure load balancer on Amazon. Here we given you detail instructions about How to configure haproxy load balancing on amazon EC2

Many people take amazon EC2 cloud hosting. Amazon does gives the load balancer for application load balancing. But Amazon load balancer is not that much good. Amazon load balancer is not working on hardware or software base. That balancer just sending request to server one by one. Many people confused how to configure load balancer on Amazon EC2.

Here I am going to give you detail instructions about setting up the HaProxy load balancer on Amazon EC2.

How to configure haproxy load balancing on amazon ec2

What is Haproxy?

AProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation makes its integration into existing architectures very easy and riskless.

How to configure haproxy load balancing on amazon ec2

How to configure haproxy load balancing on amazon ec2

For Haproxy load balancing I recommend to create the micro instance first. Then login to micro instance and install haproxy on that machine. I taken the Centos 5.0 virgin os. Use following command for install the haproxy.

#yum install haproxy

Then Open the haproxy config file.

# vi /etc/haproxy.cfg

For detail configuration you should read the Haproxy config documentation.

http://haproxy.1wt.eu/download/1.3/doc/configuration.txt

Haproxy’s default load balancing style is roundrobin. Which is trusted and good. With this load balancing I am serving around two million pages daily. Means I am handling 6 to 7 million requests per day. I am not having any issues.

I used the three backend server for delivery of my application and one server for serving the static data which is having images and js and css and application files.

Here is my configuration file


global
 maxconn     4096 # Total Max Connections. This is dependent on ulimit
 daemon
 nbproc      4 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
 mode        http
 log         global
 option      dontlognull
 option      httpclose
 option      httplog
 option      forwardfor
 option      redispatch
 timeout connect 10000 # default 10 second time out if a backend is not found
 timeout client 300000
 timeout server 300000
 maxconn     60000
 retries     3

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:80
frontend http-in
 bind :80
 acl url_static       path_beg       -i /static /images /javascript /stylesheets
 acl url_static       path_end       -i .jpg .gif .png .css .js
 acl url_stats        path_beg    /haproxy-stats
 use_backend        be_stats    if url_stats

 use_backend static          if url_static
 default_backend             app

#-------------------------------------------------
#back-end statistics section
#----------------------------------------------------------------
backend be_stats
 stats uri /haproxy-stats
#    stats show-node

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
 balance     roundrobin
 server      static 10.211.164.138:80 check
 #server      static 10.211.185.111:80 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
 balance     roundrobin
 server  app1 10.214.284.241:80 check
 server  app2 10.208.168.131:80 check
 server  app3 10.212.158.126:80 check

You can use the above code in haproxy.cfg file. You just need to change the IP address which are listed under backend static and backend app section. You just put your static server(amazon EC2 instance’s internal IP address).

You should use internal IP address of server because that will reduce the bandwidth uses of Amazon EC2 server. Do not use the public IP address for load balance because that will cost you.

Put application database and application files on all the server. Point your domain to your load balancer machine’s Public IP address.

Let say your application is running on following domain.

www.YOURDOMAIN.COM – point to Load balancer macine.

Open your all application and static server’s apache configuration file. put following entry in that file.


<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.YOURDOMAIN.COM

# Other directives here

</VirtualHost>

after this just restart the apache server of all servers.

If you are having any issues or questions about setting up the haproxy on amazon EC2 server Please write to me.

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *