水曜日は仕事帰りに新宿L15でもくもくっていうのが段々習慣になってきて最近イイ感じです。
https://twitter.com/shinodogg/status/334614812929695744
今日はAuto Scalingにチャレンジしてみようかな、と。 ■ Auto Scalingとは 日本語の公式ドキュメントがある〜ということで、 ↓を軽くチェックしてみます。 http://aws.amazon.com/jp/autoscaling/ 思いっきりデフォルメすると↓こんな感じ。 ・EC2のインスタンス数を状況によって増減させるためのもの ・CloudWatchで定義した条件に従って振る舞う ■ 開発者用ツールの設定 以下2つのコマンドラインツールをダウンロードします。 1. Auto Scaling Command Line Tool (http://aws.amazon.com/developertools/2535) 2. Amazon CloudWatch Command Line Tool (http://aws.amazon.com/developertools/2534) パス通してやります。 #ダウンロードしてきたのをマンマって本来はあんまりよくないですけど…
$ export PATH=$PATH:/Users/eiji/Downloads/AutoScaling-1.0.61.2/bin $ export PATH=$PATH:/Users/eiji/Downloads/CloudWatch-1.0.13.4/bin $ echo $PATH /usr/local/opt/ruby/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/eiji/Downloads/AutoScaling-1.0.61.2/bin:/Users/eiji/Downloads/CloudWatch-1.0.13.4/bin
で、叩いたら、HOME設定しなきゃイカンのね、と。
$ as-create-launch-config AWS_AUTO_SCALING_HOME is not set $ mon-put-metric-alarm Amazon AWS_CLOUDWATCH_HOME is not set $ export AWS_CLOUDWATCH_HOME=/Users/eiji/Downloads/CloudWatch-1.0.13.4 $ export AWS_AUTO_SCALING_HOME=/Users/eiji/Downloads/AutoScaling-1.0.61.2
また大雑把に設定しちゃいましたが、とりあえずコマンドを蹴れるようにはなりましたw ・Auto Scaling
$ as-create-launch-config as-create-launch-config: Malformed input-Missing argument(s): --launch-config --image-id --instance-type Usage: as-create-launch-config LaunchConfigurationName --image-id value --instance-type value [--block-device-mapping "key1=value1,key2=value2..." ] [--ebs-optimized ] [--iam-instance-profile value ] [--monitoring-enabled/monitoring-disabled ] [--kernel value ] [--key value ] [--ramdisk value ] [--group value[,value...] ] [--spot-price value ] [--user-data value ] [General Options] For more information and a full list of options, run "as-create-launch-config --help"
・Amazon CloudWatch Command Line Tool
$ mon-put-metric-alarm mon-put-metric-alarm: Malformed input-Missing argument(s): --alarm-name --comparison-operator --evaluation-periods --metric-name --namespace --period --statistic --threshold Usage: mon-put-metric-alarm AlarmName --comparison-operator value --evaluation-periods value --metric-name value --namespace value --period value --statistic value --threshold value [--actions-enabled value ] [--alarm-actions value[,value...] ] [--alarm-description value ] [--dimensions "key1=value1,key2=value2..." ] [--insufficient-data-actions value[,value...] ] [--ok-actions value[,value...] ] [--unit value ] [General Options] For more information and a full list of options, run "mon-put-metric-alarm --help"
■ Launch Configの設定(as-create-launch-config) ちょっとコレでやりくりするのはちょっと大変そうかな、と。。 ということで、先日CDP本にサインしていただいた@suz_labさんのブログを参考に。 suz-lab - blog: コマンドラインツール使ってVPCで"Auto Scaling" とりあえず↓のように叩いてみると、、
as-create-launch-config vpc-lc --image-id ami-3a7cfe3b --group sg-e795e5e6 --instance-type t1.micro
ですよねー的なw
as-create-launch-config: Malformed input-No Credentials were provided - cannot access the service
↓で叩いてもダメ。。
$ export AWS_ACCESS_KEY=AKIAJGYRKA7PV7L2XYAA $ export AWS_SECRET_KEY=xxxxxxxxxx $ as-create-launch-config vpc-lc --image-id ami-3a7cfe3b --group sg-e795e5e6 --instance-type t1.micro as-create-launch-config: Malformed input-No Credentials were provided - cannot access the service
↓どうもファイルに書いてソレを環境変数に設定するみたい。
$ cat credential-as.txt AWSAccessKeyId=AKIAJGYRKA7PV7L2XYAA AWSSecretKey=xxxxxxxxx $ export AWS_CREDENTIAL_FILE=/Users/eiji/Downloads/AutoScaling-1.0.61.2/credential-as.txt
↓X.509の設定も必要みたい。。
$ export EC2_PRIVATE_KEY=/Users/eiji/Downloads/pk-xxxx.pem $ export EC2_CERT=/Users/eiji/Downloads/cert-xxxx.pem
でもって↓の設定も必要でしたよ、と。。
$ export AWS_AUTO_SCALING_URL=https://autoscaling.ap-northeast-1.amazonaws.com
ということでようやく…
$ as-create-launch-config vpc-lc --image-id ami-99d55b98 --group sg-e795e5e6 --instance-type t1.micro OK-Created launch config
■ AutoScalingGroup(as-create-auto-scaling-group)の設定 Availability-Zonesで"a"と"c"それぞれに該当するsubnetの設定。
$ as-create-auto-scaling-group vpc-ag --launch-configuration vpc-lc --tag "k=Name, v=VPC-AS, p=true" --load-balancers test --health-check-type ELB --grace-period 60 --availability-zones ap-northeast-1a,ap-northeast-1c --min-size 0 --max-size 2 --desired-capacity 0 --vpc-zone-identifier subnet-5b9b8f32,subnet-c9889ca0 OK-Created AutoScalingGroup
でもって、Desired Capacityを増やすと、増やした値までEC2インスタンスが増加する、 ということで、
$ as-set-desired-capacity vpc-ag --desired-capacity 2 OK-Desired Capacity Set
が、、残念。。
$ as-describe-auto-scaling-instances No instances found
-- ココで勉強会の時間終了 -- 家に持ち帰ってもう一度チャレンジすることにしました。。 ■ AMIイメージを作る 実は上記勉強会で使ったAMIは以前チョロっと作ったものだったのですが、 ブラウザでindex.htmlがちゃんと再起動してもOKか〜とかってのを設定してなかったような。。 AMI作るところからやってみることにしました。 ポイントは以下 ・Apacheをインストールして ・chkconfig httpd on して起動時にApacheが起動するようにする ・ELBからのindex.htmlのヘルスチェックが通るようにする sshでログインしてyumでApache入れます。
[ec2-user@ip-10-134-128-23 ~]$ sudo yum install httpd Failed to set locale, defaulting to C Loaded plugins: priorities, security, update-motd, upgrade-helper Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.2.24-1.30.amzn1 will be installed --> Processing Dependency: httpd-tools = 2.2.24-1.30.amzn1 for package: httpd-2.2.24-1.30.amzn1.x86_64 〜略〜
続いてサーバー起動時についでにApacheも起動するように設定してから 手動でApacheのプロセスを起動します。80番ポートがリスンしてる感じ。
[ec2-user@ip-10-134-128-23 ~]$ sudo chkconfig httpd on [ec2-user@ip-10-134-128-23 ~]$ netstat -an | grep 80 [ec2-user@ip-10-134-128-23 ~]$ sudo /etc/init.d/httpd start Starting httpd: [ OK ] [ec2-user@ip-10-134-128-23 ~]$ netstat -an | grep 80 tcp 0 0 :::80 :::* LISTEN
再起動してもApache上がる〜的な確認ができたら、次はロードバランサのヘルスチェック用の index.htmlファイルをDocumentRootに作ります。 DocumentRootは↓こんな感じだったので、
[ec2-user@ip-10-132-100-246 html]$ cat /etc/httpd/conf/httpd.conf | grep DocumentRoot # DocumentRoot: The directory out of which you will serve your DocumentRoot "/var/www/html" # This should be changed to whatever you set DocumentRoot to. # DocumentRoot /www/docs/dummy-host.example.com
↓ココにこんな感じで作ってやります。
[ec2-user@ip-10-132-100-246 html]$ pwd /var/www/html [ec2-user@ip-10-132-100-246 html]$ cat index.html <html> <body>hello</body> </html>
で↓のようにBrowserから見えればOKかな、と。 そしたらCreate Imageするわけなんですが、 ↓のようにrebootがかかります。
[ec2-user@ip-10-132-100-246 html]$ Broadcast message from root@ip-10-132-100-246 (unknown) at 14:48 ... The system is going down for reboot NOW! Control-Alt-Delete pressed Connection to ec2-54-248-7-65.ap-northeast-1.compute.amazonaws.com closed by remote host. Connection to ec2-54-248-7-65.ap-northeast-1.compute.amazonaws.com closed.
■ AutoScalingの設定 Launch Configの設定で --group でセキュリティグループの設定をするのですが、 as-describe-scaling-activities をみてみたら、そんなの無いって怒られてました。
$ as-describe-scaling-activities ACTIVITY 32a6ce〜略〜 vpc-ag Failed The security group 'sg-e795e5e6' does not exist
コレはセキュリティグループを作る時にVPCを指定していなかったためです。 ということで、VPCを指定したセキュリティグループを設定します。
as-create-launch-config vpc-lc --image-id ami-3a7cfe3b --group sg-bff3ebd3 --instance-type t1.micro --key xxx
AutoScalingグループの設定では--desired-capacityでデフォルト状態の インスタンス数を決められます。
as-create-auto-scaling-group vpc-ag --launch-configuration vpc-lc --tag "k=Name, v=VPC-AS, p=true" --load-balancers AS --health-check-type ELB --grace-period 60 --availability-zones ap-northeast-1a,ap-northeast-1c --min-size 0 --max-size 2 --desired-capacity 1 --vpc-zone-identifier subnet-c9889ca0,subnet-6690840f
--desired-capacityの値は後からでも変えられるので、
as-set-desired-capacity vpc-ag --desired-capacity 2 as-set-desired-capacity vpc-ag --desired-capacity 0
とかしながら、コマンドやマネージメントコンソールでインスタンスの上がり具合をみて キャッキャしたりして。
$ as-describe-auto-scaling-instances INSTANCE i-269a2524 vpc-ag ap-northeast-1a InService HEALTHY vpc-lc INSTANCE i-c49a25c6 vpc-ag ap-northeast-1c Pending HEALTHY vpc-lc
次回はCloudWatchで設定入れて、JMeterとかで負荷かけて マシンリソースがxxだったら〜とかってのやってみよっと。
相変わらずAppleTV&AirPlayはナイスですし、 今回はエルタワー地下の銀座ライオンで軽く打ち上げたりもしてみましたw ということで、ブログアップが遅くなってしまいましたが、第5回お疲れ様でした。 次回は参加出来ないのですが、興味深いレポートを心待ちにしておりますmm