Pages

Monday, August 26, 2013

Auto-scaling your Amazon EC2 instances


(NewRelic diagram illustrating autoscaling from CodePen.IO)



In the past, companies would have to purchase 10 hardware servers, if at peak time 10 servers were needed to handle the traffic volume. For the rest of the day, the servers would sit idle. This is no longer needed. One big advantage of the cloud (like Amazon EC2) is elasticity, an ability to scale up and down the number of your server instances on the fly depending on the traffic load. In this article, I describe how you can automate spinning up instances when a CPU usage on your web servers exceeds 80%. I assume that you installed a typical EC2 Linux instance with a web server running on it (I used Ubuntu OS + installed Apache/MySql/PHP/Wordpress combo).

 The steps to auto-scale your instance are as follows:

STEP  1.   Preliminaries
Unless, your EC2 instance already contains Autoscaling command-line tool, you should download it: http://aws.amazon.com/developertools/2535  You can build the toolkit through the ./configure followed by make followed by make install incantations. Go to that directory and type.
AWS_AUTO_SCALING_HOME=`pwd`
export PATH=${AWS_AUTO_SCALING_HOME}/bin;$PATH

Set up a new EC2 key + certificate and copy them to the directory. You can generate them in your management console http://aws.amazon.com/.
declare -x EC2_CERT="/home/ec2-user/AutoScaling-1.0.61.2/cert-amazon.pem"
declare -x EC2_PRIVATE_KEY="/home/ec2-user/AutoScaling-1.0.61.2/pk-amazon.pem"

STEP 2     Create a launch configuration

- Here, the input types are : image-id   (AMI ID pointing to a disk image that you'd like to clone) and instance-type (which EC2 instance type you want to spawn);  finally, you can reference an existing security key and security group when you create this instance.

as-create-launch-config alexLC --image-id ami-05355a6c --instance-type t1.micro --key alexkey --group AlexGroup


STEP 3     Create a group. 

The key things you specify is minimum number of EC2 instances to launch upfront and maximum number. You also specify the availability zone in which to launch.

as-create-auto-scaling-group alexG --launch-configuration alexLC --availability-zones us-east-1d --min-size 2 --max-size 10


STEP 4      Policy
In this step, you specify how many instances at once do you launch, and what's the cool-down period (when no changes are made). The cool down is designed to prevent against sporadic traffic spikes.
Here, I scale up by 1 and wait 5 minutes to cool down.

as-put-scaling-policy --auto-scaling-group alexG --name scale-up --adjustment 1 --type ChangeInCapacity --cooldown 300

STEP 5   Trigger

Finally you need to pair this up with Cloudwatch trigger to invoke the policy at an opportune moments.
The key input variables are 
a) period of an event measured in seconds - here we require that a spike in CPU usage is observed for 60 seconds
b) number of evaluation periods (1 period only is enough for our purposes; again this is designed to prevent against sporadic traffic spikes resulting in instance launches)
c) threshold for CPU (70%). I highly recommend not to exceed 70% usage for production instances
d) reference to the policy URI identifier. This URI identifier will be emitted by command in step #4.
mon-put-metric-alarm --alarm-name sample-scale-up --alarm-description "Scale up at 70% load" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 60 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions InstanceId=i-8a3441ef --evaluation-periods 1 --unit Percent arn:aws:autoscaling:us-east-1:032853316404:scalingPolicy:0674edb1-6a2f-4ccb-9a66-3ec9c0d4cd72:autoScalingGroupName/alexG:policyName/scale-up
This is it! If you followed all the steps, correctly then you've created a policy and a trigger which will be fired when a CPU on a web server reaches 70%.

TESTING IT
Option 1) One way to simulate a CPU spike is to launch a CPU-heavy activity like a clone of a large GIT/Mercurial repository.
yum install hg  (install Mercurial)
hg clone http://xenbits.xensource.com/xen-unstable.hg        (clone entire XEN VM repository - this will keep CPU at > 90% for minutes)

Option 2) Launch a simulated load test using Apache Benchmark, JMeter, Soasta, SIEGE or many other freely available tools.
For example, you could download SIEGE from http://www.joedog.org/siege-home/ and hammer your EC2 instance siege -c25 -t10M http://ec2-54-242-122-87.compute-1.amazonaws.com/




Reactions:

78 comments:

htop is great for visualizing CPU usage

Travel and tourism - the two enchanting fields, contrive their pattern of services from time to time.They are an indispensable part of both professional and personal life. Assuring services to the point of ultimate subtlety and due reference, Blue Bird Travels proposes itself as a travel agent of the 21st century. Our output is calmly in itself and between the implications and performance - there is a satisfying equipoise of response. ,states that our vision sees shinning in and through the competition - the partition lines of excellence and dedicated teamwork leading to the actuality of success.
Auto Travel Guide
Travel Trip Plan
Travel car insurance
Hotel Reservation Tips

In order to catch some of the best glimpses of this technology, you need an authentic website like http://www.cloudwedge.com, wherein you can find loads of technological stuff on a regular basis.

International trade in several specialised ALONG WITH bulk commodities is frequently conducted using chartered (hired) vessels which permits with regard to better flexibility IN ADDITION TO control of any trade chain to be able to both seller ALONG WITH buyer. insurance news

International trade within several specialised ALONG WITH bulk commodities is frequently conducted employing chartered (hired) vessels which enables regarding greater flexibility AND ALSO control of your trade chain to help both seller AND buyer. insurance agents directory

Pre-production is actually very important in order to video production. Begin every video clip production from identifying, in the course of extensive research, your company’s competitive advantage AS WELL AS advantages to include Making use of your video production. professional-video-recording-services

Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post.

When you are given any task about writing an essay or preparing assignments for your case study, this is the best chance for you to get maximum marks and have good grades in your academic record. I have hired writers and enjoyed the way they did my homework so I recommend all students to get benefit from their services.

This is a very useful post, I was looking for this info. thanks for sharing the great ideas...
Mindfulness Courses Dublin

It is a very profitable post for me. I've enjoyed reading the post. It is very informative and useful post. I would like to visit the post once more its valuable content.

spiegelklemI just wanted to add a comment here to mention thanks for you very nice ideas. I appreciate when I see well written material.

This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
Mindfulness Courses Dublin

I really enjoyed reading this post, I always appreciate topics like this being discussed to us. Thanks for sharing.
Pre Marriage Courses

This is a wonderful article, So much info given in it, These type of articles keeps the users interest in the website, keep on sharing more... good luck.
verhuizen hoorn

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

I truly appreciated perusing this post and I generally acknowledge themes like this being examined to us. Much obliged concerning offering.

Visit : Online High School Diploma

such a great article u have written i have never seen such a fantastic work

I am happy when reading your blog with updated information! thanks alot and hope that you will post more site that are related to this site.
KLEBE BH

great work has done thanks for sharing and keep it up

This is a fantastic website and I can not recommend you guys enough. I really appreciate your post. It is very helpful for all the people on the web.

Very intresting post you have shared here i love your post.

You can't really say what is beautiful about a place, but the image of the place will remain vividly with you.

purchase valium
You can't really say what is beautiful about a place, but the image of the place will remain vividly with you.

Very nice I accept as true with your thoughts. Many thanks a great deal I’ve benefited therefore usually I’ll visit your website.

We want to reinvent the phone. What's the killer app? The killer app is making calls! It's amazing how hard it is to make calls on most phones. We want to let you use contacts like never before - sync your iPhone with your PC or mac.

Thanks for sharing everything here... Jika anda membutuhkan Tempat rental mobil di Kampung Inggris Pare, di internet juga ada rental motor di Pare atau Rental motor matic di Pare, bisa juga anda ketik di google sewa mobil di Pare Kediri, dan jika anda membutuhkan jasa Guru les privat di Surabaya atau Guru les privat di Jakarta, kadang kita juga ingin tau Kursus bahasa Inggris di Pare Kediri

adidas schuhe
Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.

Every successful person must have a failure. Do not be afraid to fail because failure is a part of success.
GED Online Diploma

Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.

buying diazepam online
Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.

Hi

Does any one tried www.pharmastores.com or www.111meds.com

Any comments?

I gathered useful information on this point as I am working on a business project. Thank you posting relative information and its now becoming easier to complete this assignment
diazepam online cheap

Regalos Originales
It is a very profitable post for me. I've enjoyed reading the post. It is very informative and useful post. I would like to visit the post once more its valuable cont

We want to reinvent the phone. What's the killer app? The killer app is making calls! It's amazing how hard it is to make calls on most phones. We want to let you use contacts like never before - sync your iPhone with your PC or mac.
HayCompras

Productos hasta 85% de Descuento. Envío en 24-48h. Productos de Cocina, Electrónica y Electrodomésticos, Hogar, Perfumes y Cosmética, Regalos, Salud y Belleza, Fitness y Deportes

Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became a kid on that day in my thought.
Buy Facebook Fans

If you wish for to take a great deal from this article then you have to apply these strategies to your won web site.
Buy Sleeping Tablets Online

Aw, this is a incredibly great post. In idea I would like to setup writing this way moreover – taking time and actual work to manufacture an incredible article… but what / issues I say… I procrastinate alot through no indicates look to go carried out.
Buy Sleeping Tablets

I just called to replace the motor on my blender because I was using it so much. It is actually a 100 year guarentee and all I had to pay was shipping. So, I am not sure where you got the 90 day warrenty from.
Buy Facebook Likes


Thank you very much! My friends and family members will be happy after hearing about this
jam tangan online
jam tangan online murah

Thanks a lot for sharing. Will check back later for more of your articles.
jam tangan online
grosir baju anak
tas wanita branded

Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.
glenn colton
pretty social
jual jam tangan

Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.

Jam Tangan Online Murah
toko tas wanita online

Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.

Jam Tangan Online Murah
toko tas wanita online

Its amazing post. I blog often and I truly appreciate your content. The article has truly peaked my interest. I’m going to take a note of your website and keep checking for new details about once a week. in IB World Academy

Be a yardstick of quality. Some people aren't used to an environment where excellence is expected.

Oh, this is a magical post. It’s life the most majestic post there is on this planet right now. This is really good work from your side. I strongly recommend you keep doing this kind of work.
buy stilnox online

This blog is great i love reading your posts. Keep up the great work! You know, a lot of people are hunting around for this info, you could help them greatly.

Oh, this is a magical post. It’s life the most majestic post there is on this planet right now. This is really good work from your side. I strongly recommend you keep doing this kind of work.
buy stilnoct


Thanks for sharing nice information with us. i like your post.
Toko Jam Tangan Online


Thanks for sharing nice information with us. i like your post.
Toko Jam Tangan Online

Very Useful information, this is both good reading for, have quite a few good key points and I learn some new stuff from it too.
stilnoct where to buy

Thanks for your great post. This post obviously very helpful. Already I'm bookmark this website. Again Thanks . If you update your facebook account, please visit Buy Facebook Likes

This is great post. I was very impressed by this post, this site has always been pleasant news. I want to say thanks for this informative post.
fashion shows

salt lake city snowboarding
Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.

It’s nearly impossible climbing shoe resole
to find knowledgeable folks on this topic, however, you appear to be there’s more you are talking about! Thanks

Wow, Excellent post. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative. If you update your facebook account, please visit Buy Facebook Likes/Fans

I am satisfied that you shared this helpful information with us. Please stay us up to date like this. Thanks for sharing. Buy Facebook Followers

Great place, great time. Getting strange experienced with orphanages. I learned how to hold an excelent progam for kids, hope more activities. I became amodern mehndi designs
kid on that day in my thought.

Hello dear, Excellent read, Positive site, where did u come up with the information on this posting? In Future reference really bookmark your blog.

It is a very profitable post for me. I've enjoyed reading the post. It is very informative and useful post. I would like to visit the post once more its valuable content.
floral mehndi collection

unique mehndi designs
There is no question that instantaneous water heaters are good options to trust in terms of their water heating services. However, you need to know that not all kinds of this water heater will work best for you.

Keep posting the good work. Some really helpful information in there.

I’m delighted that I’ve observed this weblog. Finally something not a junk, which we go through incredibly frequently. The web site is lovingly serviced and saved as much as date. So it should be, thanks for sharing this with us.

A person without ambition is like an lifeless object and when you work with an ambition, no goal is difficult to achieve. Goa trips india

To find a greater publicity for your facebook account, People expend money to Buy Facebook Followers to conveniently obtain exposure on internet. buy followers for facebook

Post a Comment