Rock'n'Hack ブログ

それなりハッカーを目指して、もろもろのメモ。bloggerから引っ越しました。

AWS : Auto Scaling お試しメモ

AWS本に従って Auto Scaling を試した時のメモ。
結構前にやったやつなので、自分へのリマインド的な感じ。

なんとなくのシナリオとしては下記の通り。

0.OS起動時にApacheが起動するAMIを用意しておく(別にApacheである必要はない)
1.ロードバランサを1つ作成
2.スケーリングする条件を設定
3.スケールアウトを試す
4.スケールインを試す
5.もろもろ設定削除して終了

といったところ。

では早速試してみる。ただエビデンスを貼っただけですが、、、
今回はbotoではなく、JavaコマンドラインAPIを使用してます。


◆ロードバランサを作成

>>> elb-create-lb LoadBal
--listener "lb-port=80, instance-port=80, protocol=HTTP"
--availability-zones ap-northeast-1a
--region ap-northeast-1

DNS_NAME LoadBal-476601279.ap-northeast-1.elb.amazonaws.com

訳)
東京リージョンの、aゾーンに、LoadBalというロードバランサを作成。
リクエストをロードバランサの80番ポートで受け付け、バックエンドの80番ポートにHTTPプロトコルで流す。


◆Auto Scaling グループの起動コンフィグレーションを作成する。

>>> as-create-launch-config Configtest
--image-id ami-2c65cf2d
--instance-type t1.micro
--region ap-northeast-1

OK-Created launch config

訳)
東京リージョンの、指定AMI(ami-2c65cf2d)が、t1.microインスタンスで起動する、Configtestという名前の起動設定を作成する。
※指定するAMIはApacheが起動し、デフォルトページを返すようにしてある。


◆Auto Scaling グループを作成する。

>>> as-create-auto-scaling-group AutoScale
--launch-configuration Configtest
--availability-zones ap-northeast-1a
--min-size 1
--max-size 5
--load-balancers LoadBal
--region ap-northeast-1

OK-Created AutoScalingGroup

訳)
Configtest 起動コンフィグレーションを参照し、
ap-northeast-1a AZで
最小1つ、最大5つのLoadBalインスタンスを作成する、
AutoScaleという名前のauto-scaling-groupを作成する。


◆スケーリングするためのトリガを作成する。

>>> as-create-or-update-trigger Trigger1
--auto-scaling-group AutoScale
--namespace "AWS/EC2"
--measure CPUUtilization
--statistic Average
--dimensions "AutoScalingGroupName=AutoScale"
--units "Percent"
--period 60
--lower-threshold 30
--upper-threshold 70
--lower-breach-increment"=-1"
--upper-breach-increment "1"
--breach-duration 120
--region ap-northeast-1

DEPRECATED: This command is deprecated and included only to facilitate migration to the new trigger
mechanism. You should use this command for migration purposes only.
OK-Created/Updated trigger

訳)
このトリガはEC2のCPUUtilization メトリックにより制御される。
AutoScaleグループ内のEC2インスタンスの平均CPU使用率が2分間隔で70%を超えたら、スケールアウトを行う。
同じく、2分間隔で平均CPU使用率が30%を下回ったら、スケールインを行う。
スケールイン・スケールアウトともにインスタンスを1つずつ加減する。


◆何が起きているか確認する
下記は、オートスケール有効後にCPUに負荷をかけて、1つスケールアウトした後の出力。

>>> as-describe-scaling-activities --auto-scaling-group AutoScale --show-long --region ap-northeast-1

ACTIVITY,05d12fe9-355c-434a-a03e-e328413dbf21,2011-05-25T11:00:59Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:59:47Z a breaching trigger explicitly set group desired capacity changing the desired ca
pacity from 1 to 2. At 2011-05-25T10:59:47Z trigger Trigger1 breached high threshold value for CPUU
tilization, 70.0, adjusting the desired capacity from 1 to 2. At 2011-05-25T11:00:12Z an instance w
as started in response to a difference between desired and actual capacity, increasing the capacity
from 1 to 2.",100,Launching a new EC2 instance: i-06167b07,(nil),2011-05-25T11:00:12.868Z
ACTIVITY,f4f00f27-7cb4-497f-9cda-fea15bb64bd1,2011-05-25T10:29:15Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:28:24Z a user request created an AutoScalingGroup changing the desired capacity from 0 t
o 1. At 2011-05-25T10:28:29Z an instance was started in response to a difference between desired an
d actual capacity, increasing the capacity from 0 to 1.",100,Launching a new EC2 instance: i-821a7783,(nil),2011-05-25T10:28:29.681Z


0から1に増えて(最初の起動)、1から2にスケールアウトした感じの出力がされている。
上の方が新しいログの模様。


◆ホスト側の動き

vmstat で CPU使用率を監視していたら、スケールする際に再起動がされるもよう。

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 476488 6924 75980 0 0 0 0 9 12 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 9 15 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 8 10 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 7 11 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 8 9 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 9 12 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 7 11 0 0 100 0 0
0 0 0 476488 6924 75980 0 0 0 0 13 13 0 0 100 0 0

Broadcast message from root@ip-10-146-27-140
(unknown) at 11:08 ...

The system is going down for power off NOW!
Connection to ec2-46-51-225-202.ap-northeast-1.compute.amazonaws.com closed by remote host.
Connection to ec2-46-51-225-202.ap-northeast-1.compute.amazonaws.com closed.


◆スケールインを確認
負荷をかけるスクリプトが止まって一定時間たったため、スケールインが発生しているはず。

>>> as-describe-scaling-activities --auto-scaling-group AutoScale --show-long --region ap-northeast-1

ACTIVITY,586ec3c6-ca82-4fbf-8d14-cb3e150a9a72,2011-05-25T11:08:47Z,AutoScale,Successful,(nil),"At 20
11-05-25T11:06:44Z a breaching trigger explicitly set group desired capacity changing the desired ca
pacity from 2 to 1. At 2011-05-25T11:06:44Z trigger Trigger1 breached low threshold value for CPUUt
ilization, 30.0, adjusting the desired capacity from 2 to 1. At 2011-05-25T11:07:06Z an instance wa
s taken out of service in response to a difference between desired and actual capacity, shrinking th
e capacity from 2 to 1. At 2011-05-25T11:07:06Z instance i-821a7783 was selected for termination.",
100,Terminating EC2 instance i-821a7783,(nil),2011-05-25T11:07:06.148Z
ACTIVITY,05d12fe9-355c-434a-a03e-e328413dbf21,2011-05-25T11:00:59Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:59:47Z a breaching trigger explicitly set group desired capacity changing the desired ca
pacity from 1 to 2. At 2011-05-25T10:59:47Z trigger Trigger1 breached high threshold value for CPUU
tilization, 70.0, adjusting the desired capacity from 1 to 2. At 2011-05-25T11:00:12Z an instance w
as started in response to a difference between desired and actual capacity, increasing the capacity
from 1 to 2.",100,Launching a new EC2 instance: i-06167b07,(nil),2011-05-25T11:00:12.868Z
ACTIVITY,f4f00f27-7cb4-497f-9cda-fea15bb64bd1,2011-05-25T10:29:15Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:28:24Z a user request created an AutoScalingGroup changing the desired capacity from 0 t
o 1. At 2011-05-25T10:28:29Z an instance was started in response to a difference between desired an
d actual capacity, increasing the capacity from 0 to 1.",100,Launching a new EC2 instance: i-821a778
3,(nil),2011-05-25T10:28:29.681Z


とりあえずの動作確認は終了。


◆グループを終了させる。まずはトリガを削除

>>> as-delete-trigger Trigger1 --auto-scaling-group AutoScale --region ap-northeast-1

DEPRECATED: This command is deprecated and included only to facilitate migration to the new trigger
mechanism. You should use this command for migration purposes only.

Are you sure you want to delete this trigger? [Ny]y
OK-Deleted trigger


◆グループの最小サイズと最大サイズを0に設定し、全インスタンスを強制終了する。

>>> as-update-auto-scaling-group AutoScale --min-size 0 --max-size 0 --region ap-northeast-1

OK-Updated AutoScalingGroup


◆終了したか確認してみる。

>>> as-describe-scaling-activities --auto-scaling-group AutoScale --show-long --region ap-northeast-1

ACTIVITY,971a1a47-53d6-4e1d-a9e7-c5cf2a1d36ab,(nil),AutoScale,InProgress,(nil),"At 2011-05-25T11:15:
29Z a user request update of AutoScalingGroup constraints to min: 0, max: 0, desired: 0 changing the
desired capacity from 1 to 0. At 2011-05-25T11:15:37Z an instance was taken out of service in resp
onse to a difference between desired and actual capacity, shrinking the capacity from 1 to 0. At 20
11-05-25T11:15:37Z instance i-06167b07 was selected for termination.",0,Terminating EC2 instance i-0
6167b07,(nil),2011-05-25T11:15:37.271Z
ACTIVITY,586ec3c6-ca82-4fbf-8d14-cb3e150a9a72,2011-05-25T11:08:47Z,AutoScale,Successful,(nil),"At 20
11-05-25T11:06:44Z a breaching trigger explicitly set group desired capacity changing the desired ca
pacity from 2 to 1. At 2011-05-25T11:06:44Z trigger Trigger1 breached low threshold value for CPUUt
ilization, 30.0, adjusting the desired capacity from 2 to 1. At 2011-05-25T11:07:06Z an instance wa
s taken out of service in response to a difference between desired and actual capacity, shrinking th
e capacity from 2 to 1. At 2011-05-25T11:07:06Z instance i-821a7783 was selected for termination.",
100,Terminating EC2 instance i-821a7783,(nil),2011-05-25T11:07:06.148Z
ACTIVITY,05d12fe9-355c-434a-a03e-e328413dbf21,2011-05-25T11:00:59Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:59:47Z a breaching trigger explicitly set group desired capacity changing the desired ca
pacity from 1 to 2. At 2011-05-25T10:59:47Z trigger Trigger1 breached high threshold value for CPUU
tilization, 70.0, adjusting the desired capacity from 1 to 2. At 2011-05-25T11:00:12Z an instance w
as started in response to a difference between desired and actual capacity, increasing the capacity
from 1 to 2.",100,Launching a new EC2 instance: i-06167b07,(nil),2011-05-25T11:00:12.868Z
ACTIVITY,f4f00f27-7cb4-497f-9cda-fea15bb64bd1,2011-05-25T10:29:15Z,AutoScale,Successful,(nil),"At 20
11-05-25T10:28:24Z a user request created an AutoScalingGroup changing the desired capacity from 0 t
o 1. At 2011-05-25T10:28:29Z an instance was started in response to a difference between desired an
d actual capacity, increasing the capacity from 0 to 1.",100,Launching a new EC2 instance: i-821a778
3,(nil),2011-05-25T10:28:29.681Z


0になった!


インスタンスが全て消えたので、グループを削除

>>> as-delete-auto-scaling-group AutoScale --region ap-northeast-1

Are you sure you want to delete this AutoScalingGroup? [Ny]y
OK-Deleted AutoScalingGroup


◆起動コンフィグレーションも削除しとく

>>> as-delete-launch-config Configtest --region ap-northeast-1

Are you sure you want to delete this launch configuration? [Ny]y
OK-Deleted launch configuration


◆最後にロードバランサを削除する

>>> elb-delete-lb LoadBal --region ap-northeast-1

Warning: Deleting a LoadBalancer can lead to service disruption to any
customers connected to the LoadBalancer. Are you sure you want to delete
this LoadBalancer? [Ny]y
OK-Deleting LoadBalancer


ホントにメモですみません。。m(__)m

おしまい。

---
下記の本を参考に行いました。
全体概要を把握するのにとっつきやすかったです。