Creating an auto scaled system using an Amazon load balancer is an interesting task that I did recently.
Here are the list of commands that I used to setup from the command line :
as-create-launch-config ec2elbconfig --image-id ami-xxxxx --instance-type m1.large --key key_name
Also if you don’t have already setup credentials you may append
-I amazonid -S secretkey
Do not forget to add –key because after the instance is up you won’t be able to log on it.
Next command is:
as-create-auto-scaling-group MyAutoScalingGroup --launch-configuration ec2elbconfig --availability-zones us-east-1c --min-size 2 --max-size 6 --load-balancers MyLoadBalancer
Where : MyLoadBalancer – is the name of your ELB ( loading balancer ) from amazon
Nex we create two policy rules, one for adding server and one for remover:
[root@server]# as-put-scaling-policy HighCpuPolicy --auto-scaling-group MyAutoScalingGroup --adjustment=1 --type ChangeInCapacity --cooldown 300
arn:aws:autoscaling:us-east-1:xxx:scalingPolicy:xxx-xx-xx-xx-xxxxx:autoScalingGroupName/MyAutoScalingGroup:policyName/HighCpuPolicy
[root@server]# as-put-scaling-policy LowCpuPolicy --auto-scaling-group MyAutoScalingGroup --adjustment=-1 --type ChangeInCapacity --cooldown 300
arn:aws:autoscaling:us-east-1:xxx:scalingPolicy:xxx-xx-xx-xx-xxxxx:autoScalingGroupName/MyAutoScalingGroup:policyName/LowCpuPolicy
Here is important to remember the output.
After this we must create two monitor rules that will scale our balancer, so will need two more rules:
mon-put-metric-alarm HighCpuAlarm --comparison-operator GreaterThanThreshold --evaluation-periods 4 --metric-name CPUUtilization --namespace "AWS/EC2" --period 60 --statistic Average --threshold 30 --alarm-actions arn:aws:autoscaling:us-east-1:xxx:scalingPolicy:xxx-xx-xx-xx-xxxxx:autoScalingGroupName/MyAutoScalingGroup:policyName/HighCpuPolicy --dimensions "AutoScalingGroupName=MyAutoScalingGroup"
and
mon-put-metric-alarm LowCpuAlarm --comparison-operator LessThanThreshold --evaluation-periods 4 --metric-name CPUUtilization --namespace "AWS/EC2" --period 60 --statistic Average --threshold 20 --alarm-actions arn:aws:autoscaling:us-east-1:xxx:scalingPolicy:xxx-xx-xx-xx-xxxxx:autoScalingGroupName/MyAutoScalingGroup:policyName/LowCpuPolicy --dimensions "AutoScalingGroupName=MyAutoScalingGroup"
After this you will have allways 2 instance up, and if the load go up then 30% more then 4 minutes the system will go up with one more server, if the load go bellow 20% more then 4 minutes will remove one server from load balancer.
If you need to remove this you must delete rules from bottom to top using bellow commands with name of your rules.
mon-delete-alarms
as-delete-policy
as-delete-auto-scaling-group
as-delete-launch-config