AWS Elastic Load Balancer Tutorial
Jun10
We’ve been using the new Amazon Load Balancers (ELB) for Socialmod, and since there’s not much information out there on the subject, I thought a blog post would be in order.
The load balancers are charged at $0.025 per hour, plus $0.008 per GB of data transferred through them. Personally I think this is very reasonable.
They’re hardware based, and can balance both HTTP and TCP traffic. This means you can balance both the traffic to the web server, and the database traffic (although there are issues with the latter that I’ll talk about later).
Some of the following instructions are specific to OSX/Linux, check the docs for information about Windows.
Setting up the tools.
Download the tools from Amazon, unzip them and place them somewhere logical (in your home directory for example).
Edit your .bash_profile file (or .profile), adding the following line:
export ELB_HOME=~/path/to/elastic_load_balancing export PATH=$PATH:$ELB_HOME/bin
If you’re not using EC2, you’ll have to go through the extra step of creating/downloading a private key, and adding EC2_CERT/EC2_PRIVATE_KEY to your bash file.
I’ll assume you are, since you can’t use ELB with any other server setup.
Create the load balancer.
Execute the following command. Only include the zones you actually have instances in – I made the mistake of including extra which meant the balancer kept on droppingn requests.
elb-create-lb default --zones us-east-1a --listener "protocol=http, lb-port=80, instance-port=80"
You’ll be given the URL of your new load balancer in return which you’ll need for configuring the DNS.
Register instances.
Now to actually register any EC2 instances with the load balancer.
elb-register-instances-with-lb default --instances i-12345678
If you navigate to the load balancer’s URL you’ll probably see an Apache “It Works!” sign.
You could add the Load Balancer’s URL to your virtual host’s domain alias in order to actually see the web page.
Configure DNS.
Ok, so here comes the kicker – you can’t use ELB for the root of your domain. This is because the load balancers can only be referenced by a domain name, not an IP address. You can’t have a CNAME on the root of a domain – it has to be on a subdomain.
Amazon are busy adding this ‘feature’, but in the meantime you should forward everybody to a subdomain, such as ‘www’.
Here’s an example of an Apache rewrite condition that forwards everybody to www:
RewriteCond %{HTTP_HOST} ^socialmod.com$ [NC]
RewriteRule ^(.*)$ http://www.socialmod.com$1 [R=301,L]
Advanced Load Balancing
You can load balance your Mysql cluster – however ELB is outside Amazon’s firewall and isn’t integrated with it. This means that to load balance Mysql you need to open it up to the world and rely on strong credentials to keep your data secure, rather than firewall rules.
ELB can handle SSL traffic, just set the protocol to TCP and the port to 443 when you create the balancer. Currently, Elastic Load Balancing does not have SSL termination capability.
ELB is certainly in it’s infancy, but has been beta tested and is ready for production. I just hope that Amazon add ELB management to their AWS console.
Enjoy this article?
Consider subscribing to our RSS feed!
9:46 pm on June 24th, 2009
You made big thing …. simple.
Thanks,
Yogesh
5:48 am on July 8th, 2009
Great!i have just few months experience working with launching ,making and sharing images . i did not get even in documents how to begin , Your work is so simple and to the point with little modification i was successful and boosted confidence for advanced steps.
Thanks a lot.
Regards
Vinod
10:59 am on October 13th, 2009
Actually, you can use it on the root of your domain. Just CNAME yourdomain.com
3:50 am on October 19th, 2009
Martin, unfortunately you can’t use a CNAME at the root of your domain.
8:04 pm on January 18th, 2010
I just had a look at the http://aws.amazon.com/console/ and it does look like ELB has been added…
Cheers,
Anders
5:40 am on March 9th, 2010
Now we are using AWS elastic load balancer and to create the load balancer and setting up the tools is a main thing in doing this work. Thanks for this post.
3:28 am on June 11th, 2010
we can balance both HTTP and TCP traffic. because they give some trouble and other problem to us so please avoid the HTTP and TCP traffic control.
5:44 am on June 11th, 2010
The Elastic Load Balancing detects unhealthy instances within a pool and automatically reroutes traffic to healthy instances until the unhealthy instances have been restored.