okochangの馬鹿でありがとう

ふらふら適当に世間を生きる日々でございます

Multi Load Balancer、Scale Outパターンを組み合わせる場合の構築手順

12月に入ってブログを更新していないかったのですが、CDPのAdvent Calendarで枠が空いていたので参加してみました。
そういう訳で、12月7日のカレンダーは私が担当させて頂きます。
本日書かせて頂く内容は、以下のCDPを組み合わせた場合の構築方法です。

  1. Multi Load Balancerパターン
  2. Scale Outパターン

両パターンの説明は割愛させていただきますが、Multi Load BalancerパターンでもAuto Scalingが使いたい!という状況です。
複数のELB配下に配置されるインスタンスは同じ設定のものなので同じAuto Scalingが使えるわけです。

f:id:okochang:20121206200701p:plain

構築手順

それでは早速構築していきます。
Management Consoleのキャプチャが面倒なので、今回はコマンドラインで手順化したいと思います。
Auto Scalingはコマンドラインでしか設定出来ないですしね:)

①事前にテストで使用するSSLサーバ証明書をアップロードします
SSLサーバ証明書は事前にご用意下さい

$ iam-servercertupload -b ./demo01.crt -k ./demo01.key -c ./demo01-chukan.crt -s ssl-demo01
$ iam-servercertupload -b ./demo02.crt -k ./demo02.key -c ./demo02-chukan.crt -s ssl-demo02
$ iam-servercertupload -b ./demo03.crt -k ./demo03.key -c ./demo03-chukan.crt -s ssl-demo03

②アップロードしたSSLサーバ証明書を確認します

$ iam-servercertlistbypath
arn:aws:iam::************:server-certificate/ssl-demo01
arn:aws:iam::************:server-certificate/ssl-demo02
arn:aws:iam::************:server-certificate/ssl-demo03

③テストで使うELBを作成します
一台目

$ elb-create-lb demo01-lb --headers --listener "lb-port=80,instance-port=80,protocol=http" --listener "lb-port=443,instance-port=80,protocol=https,cert-id=arn:aws:iam::************:server-certificate/ssl-demo01" --availability-zones ap-northeast-1a,ap-northeast-1b

二台目

$ elb-create-lb demo02-lb --headers --listener "lb-port=80,instance-port=80,protocol=http" --listener "lb-port=443,instance-port=80,protocol=https,cert-id=arn:aws:iam::************:server-certificate/ssl-demo02" --availability-zones ap-northeast-1a,ap-northeast-1b

三台目

$ elb-create-lb demo03-lb --headers --listener "lb-port=80,instance-port=80,protocol=http" --listener "lb-port=443,instance-port=80,protocol=https,cert-id=arn:aws:iam::************:server-certificate/ssl-demo03" --availability-zones ap-northeast-1a,ap-northeast-1b

④Launch Configを作成します

$ as-create-launch-config demo-launch-config --image-id ami-******** --instance-type t1.micro --key demo-key --group demo-group

⑤Auto Scaling Groupを作成します
※ココがポイント--load-balancersオプションで複数のELBを指定する

$ as-create-auto-scaling-group demo-autoscaling-group \
--launch-configuration demo-launch-config \
--availability-zones ap-northeast-1a ap-northeast-1b \
--min-size 2 \
--max-size 4 \
--termination-policies "NewestInstance,Default" \
--load-balancers "demo01-lb,demo02-lb,demo03-lb" \
--health-check-type ELB \
--grace-period 60

⑥Scale Outのポリシーを設定します

$ as-put-scaling-policy demo-scaleout-policy --auto-scaling-group demo-autoscaling-group --adjustment=2 --type ChangeInCapacity --cooldown 600

arn:aws:autoscaling:ap-northeast-1:*************:scalingPolicy:********-****-****-****-************:autoScalingGroupName/demo-autoscaling-group:policyName/demo-scaleout-policy

⑦Scale Outのトリガを設定します

$ mon-put-metric-alarm demo-scaleout-alerm \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--metric-name CPUUtilization \
--namespace "AWS/EC2" \
--period 180 \
--statistic Average \
--threshold 70 \
--alarm-actions arn:aws:autoscaling:ap-northeast-1:*************:scalingPolicy:********-****-****-****-************:autoScalingGroupName/demo-autoscaling-group:policyName/demo-scaleout-policy \
--dimensions "AutoScalingGroupName=demo-autoscaling-group"

⑧Scale Inのポリシーを設定します

$ as-put-scaling-policy demo-scalein-policy --auto-scaling-group demo-autoscaling-group --adjustment=-2 --type ChangeInCapacity --cooldown 600

arn:aws:autoscaling:ap-northeast-1:************:scalingPolicy:********-****-****-****-************:autoScalingGroupName/demo-autoscaling-group:policyName/demo-scalein-policy

⑨Scale Inのトリガを設定します

$ mon-put-metric-alarm demo-scalein-alerm \
--comparison-operator LessThanThreshold \
--evaluation-periods 1 \
--metric-name CPUUtilization \
--namespace "AWS/EC2" \
--period 180 \
--statistic Average \
--threshold 20 \
--alarm-actions arn:aws:autoscaling:ap-northeast-1:************:scalingPolicy:********-****-****-****-************:autoScalingGroupName/demo-autoscaling-group:policyName/demo-scalein-policy \
--dimensions "AutoScalingGroupName=demo-autoscaling-group"

無事に起動されたEC2インスタンスが複数のELBに登録されていると思います。
事前にEC2のWebサーバ側に名前ベースのVirtualHostを設定しておいて下さいね。

削除方法(追記)

使用したAutoScaligのリソース削除方法があった方がいいなと思ったので追記します。

①Scale InとScale Outのトリガを削除します

$ mon-delete-alarms demo-scalein-alerm
$ mon-delete-alarms demo-scaleout-alerm

②Scale InとScale Outのポリシーを削除します

$ as-delete-policy demo-scalein-policy --auto-scaling-group demo-autoscaling-group
$ as-delete-policy demo-scaleout-policy --auto-scaling-group demo-autoscaling-group

③Auto Scaling Groupを削除します(起動しているインスタンスも削除されます)

$ as-delete-auto-scaling-group demo-autoscaling-group --force-delete

④Launch Configを削除します

$ as-delete-launch-config demo-launch-config

感想とかとか

Auto Scalingを使うときにELBが複数になったとしても、その分指定するだけなので簡単です。
AMIを起動する時にアプリケーションを最新化したい場合は、12月3日を担当された@ijinさんエントリーがとても参考になると思います。
起動時にアプリケーションを最新化する場合は、デプロイが完了してからELBの監視が成功するようにしたいですね。
また、IPベースのバーチャルホストを使って大量のSSLをホストする場合は@j3tm0t0さんオクトパスパターンクラーケンパターンをご覧下さい!

それでは引き続きCDP Advent Calendarをお楽しみ下さい。

Amazon Web Services クラウドデザインパターン 設計ガイド

Amazon Web Services クラウドデザインパターン 設計ガイド