延續上篇:
AWS Auto Scaling 自動擴展(一),auto scaling 除了可以根據 EC2 instance 狀態,或是 ELB 的健康狀況做擴展,還可以搭配 CloudWatch 使用,CloudWatch 可以 monitor 的狀態有更多了,CPU、disk I/O、network I/O。
Scale up
Create auto scaling policy
$ aws autoscaling put-scaling-policy \
--policy-name as-policy-scale-up \
--auto-scaling-group-name as-group \
--adjustment-type ChangeInCapacity \
--scaling-adjustment 1 \
--cooldown 300
{
"ResponseMetadata": {
"RequestId": "4411e437-9b6a-11e2-933c-010eb2f029e6"
},
"PolicyARN": "arn:aws:autoscaling:us-east-1:332100208493:scalingPolicy:c534dcbe-91c1-4714-907a-b4974bac7c57:autoScalingGroupName/as-group:policyName/as-policy-scale-up"
}
把 PolicyARN 記下來,下一步會用到
Create cloud watch to monitor the CPU utilization
When the average CPU usage more than 60% and keep 600 seconds, then scale up
$ aws cloudwatch put-metric-alarm \
--evaluation-periods 1 \
--namespace "AWS/EC2" \
--comparison-operator GreaterThanThreshold \
--alarm-name alarm-high-cpu \
--statistic Average \
--threshold 60 \
--period 600 \
--metric-name CPUUtilization \
--alarm-actions arn:aws:autoscaling:us-east-1:332100208493:scalingPolicy:c34f62a8-bb82-4430-9390-67468a7a65f9:autoScalingGroupName/as-group:policyName/as-policy-scale-up
可以做個實驗,用 shell script 寫個 while loop 把 CPU 拉高,果然 10 分鐘後 alarm 就發生了,也會順便執行 scale-up 的 action
Scale down
Create auto scaling policy
$ aws autoscaling put-scaling-policy \
--policy-name as-policy-scale-down \
--auto-scaling-group-name as-group \
--adjustment-type ChangeInCapacity \
--scaling-adjustment -1 \
--cooldown 300
{
"ResponseMetadata": {
"RequestId": "c58093f7-9b6b-11e2-82fc-2bb2a883e37c"
},
"PolicyARN": "arn:aws:autoscaling:us-east-1:332100238493:scalingPolicy:141d692b-9ae8-4ef8-8d98-7de605c3fe04:autoScalingGroupName/as-group:policyName/as-policy-scale-down"
}
Create cloud watch to monitor the CPU utilization
When the average CPU usage less than 40% and keep 600 seconds, then scale down
$ aws cloudwatch put-metric-alarm \
--evaluation-periods 1 \
--namespace "AWS/EC2" \
--comparison-operator LessThanThreshold \
--alarm-name alarm-low-cpu \
--statistic Average \
--threshold 40 \
--period 600 \
--metric-name CPUUtilization \
--alarm-actions arn:aws:autoscaling:us-east-1:332100238493:scalingPolicy:141d692b-9ae8-4ef8-8d98-7de605c3fe04:autoScalingGroupName/as-group:policyName/as-policy-scale-down
有一些疑問
Scale-up 會增加到多少台?Scale-down 會減至多少台?
CloudWatch 不會重複 trigger,除非狀態從 alarm 回到 normal。
參考資料
Llovizna: Auto Scaling With Amazon EC2