We will set up a auto scaling group with elastic load balancing. Scale out by adding one instance when the CPU utilization is above 80% for 10 mins. Scale in by subtracting one instance when the CPU utilization is below 40% for 10 mins.
Create an Auto Scaling Group
We will create an auto scaling group called NodeJSGroup.
Check all your available load balancers:
Create a Scale out Policy
We will create a scale out policy such that a new instance will be added to the auto scaling group when the CPU utilization reaches 80% or above for over 10 mins. We will label this policy NodeJSScaleOutPolicy.
You can check the policy by
Create a Scale in Policy
We will create a scale in policy such that an instance will be removed from the auto scaling group when the CPU utilization reaches 40% or below for over 10 mins.
Install CloudWatch API
We will need to associate the alarms with the scale in and scale out policies. To do that, we need to install the CloudWatch Command Line Tools.
Associate Alarms with Policies
Make an alarm called NodeJSHighAlarm and associate it with NodeJSScaleOutPolicy.
Make another alarm called NodeJSLowAlarm and associate it with NodeJSScaleInPolicy.
There will be 6 things we need to do:
- create a launch configuration
- create an auto scaling group
- create a scale out policy
- create a scale in policy
- create a alarm attached to the scale out policy
- create an alarm attached to the scale in policy
Create a Launch Configuration
We will create a launch configuration called NodeJS.
as-create-launch-config NodeJS --image-id ami-87acc4ee --instance-type m1.large --block-device-mapping="/dev/sda1=snap-1f356ee2, /dev/sdf=snap-18356ee5, /dev/sdg=snap-15356ee8, /dev/sdb=ephemeral0" --group your_security_group --key your_key_pairThis is same as what we have at the end of Part 2 - Auto Scaling Based on Fixed Number of Instances.
Create an Auto Scaling Group
We will create an auto scaling group called NodeJSGroup.
as-create-auto-scaling-group NodeJSGroup --launch-configuration NodeJS --availability-zones us-east-1d --min-size 1 --max-size 5 --tag "k=name, v=AsNodeJSProd, p=true" --load-balancers TestNodeBalancerWe have added a elastic load balancer called TestNodeBalancer. You can create a load balancer in the AWS EC2 console.
Check all your available load balancers:
elb-describe-lbs --headersThis is same as what we have at the end of Part 2 - Auto Scaling Based on Fixed Number of Instances.
Create a Scale out Policy
We will create a scale out policy such that a new instance will be added to the auto scaling group when the CPU utilization reaches 80% or above for over 10 mins. We will label this policy NodeJSScaleOutPolicy.
as-put-scaling-policy NodeJSScaleOutPolicy --auto-scaling-group NodeJSGroup --adjustment=1 --type ChangeInCapacity --cooldown 300Take note of the Amazon Resource Name (ARN) when it returns as a response. You will need this to set up the alarm.
arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:f0037fff-0949-4123-8887-f6c7064b8253:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleOutPolicyThe --cooldown = 300 parameter means that there needs to be 300 seconds gap before the policy can be applied again.
You can check the policy by
as-describe-policies
Create a Scale in Policy
We will create a scale in policy such that an instance will be removed from the auto scaling group when the CPU utilization reaches 40% or below for over 10 mins.
as-put-scaling-policy NodeJSScaleInPolicy --auto-scaling-group NodeJSGroup --adjustment=-1 --type ChangeInCapacity --cooldown 300
Install CloudWatch API
We will need to associate the alarms with the scale in and scale out policies. To do that, we need to install the CloudWatch Command Line Tools.
Associate Alarms with Policies
Make an alarm called NodeJSHighAlarm and associate it with NodeJSScaleOutPolicy.
mon-put-metric-alarm NodeJSHighAlarm --comparison-operator GreaterThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 80 --alarm-actions arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:f0037fff-0949-4123-8887-f6c7064b8253:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleOutPolicy --dimensions "AutoScalingGroupName=NodeJSGroup"
Make another alarm called NodeJSLowAlarm and associate it with NodeJSScaleInPolicy.
mon-put-metric-alarm NodeJSLowAlarm --comparison-operator LessThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 40 --alarm-actions arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:631fd578-9517-42e2-a424-8b1ed8dd0874:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleInPolicy --dimensions "AutoScalingGroupName=NodeJSGroup"Check the status of the alarms
mon-describe-alarms --headers
Excellent post! Thanks for posting and keep update with us AWS Online Training
ReplyDelete