How to configure haproxy load balancing on amazon ec2

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

01global
02 maxconn     4096 # Total Max Connections. This is dependent on ulimit
03 daemon
04 nbproc      4 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.
05 
06#---------------------------------------------------------------------
07# common defaults that all the 'listen' and 'backend' sections will
08# use if not designated in their block
09#---------------------------------------------------------------------
10defaults
11 mode        http
12 log         global
13 option      dontlognull
14 option      httpclose
15 option      httplog
16 option      forwardfor
17 option      redispatch
18 timeout connect 10000 # default 10 second time out if a backend is not found
19 timeout client 300000
20 timeout server 300000
21 maxconn     60000
22 retries     3
23 
24#---------------------------------------------------------------------
25# main frontend which proxys to the backends
26#---------------------------------------------------------------------
27#frontend  main *:80
28frontend http-in
29 bind :80
30 acl url_static       path_beg       -i /static /images /javascript /stylesheets
31 acl url_static       path_end       -i .jpg .gif .png .css .js
32 acl url_stats        path_beg    /haproxy-stats
33 use_backend        be_stats    if url_stats
34 
35 use_backend static          if url_static
36 default_backend             app
37 
38#-------------------------------------------------
39#back-end statistics section
40#----------------------------------------------------------------
41backend be_stats
42 stats uri /haproxy-stats
43#    stats show-node
44 
45#---------------------------------------------------------------------
46# static backend for serving up images, stylesheets and such
47#---------------------------------------------------------------------
48backend static
49 balance     roundrobin
50 server      static 10.211.164.138:80 check
51 #server      static 10.211.185.111:80 check
52 
53#---------------------------------------------------------------------
54# round robin balancing between the various backends
55#---------------------------------------------------------------------
56backend app
57 balance     roundrobin
58 server  app1 10.214.284.241:80 check
59 server  app2 10.208.168.131:80 check
60 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.

1<VirtualHost *:80>
2DocumentRoot /var/www/html
3ServerName www.YOURDOMAIN.COM
4 
5# Other directives here
6 
7</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.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

2 thoughts on “How to configure haproxy load balancing on amazon ec2”

Leave a Reply to Purab Kharat Cancel reply

Your email address will not be published.