前回 では、AWS Direct Connectを利用したハイブリッド構成で、オンプレミスのWebサーバをAWSにオフロードする基本構成をご紹介しました。
今回は、前回の構成をベースにAuto Scalingを使って自動的にスケールアウトを行う
方法をご紹介します。 Auto Scaling Group内のEC2インスタンスの平均CPU使用率が70%を超えたらAWSのVPC上にインスタンスを追加し、ロードバランサ vThunderに自分自身をWebサーバとして登録します。CPU使用率の監視にはAmazon CloudWatchを利用します。
処理の流れは以下のとおりです。
CloudWatchでAuto Scaling Group内のインスタンスのCPU使用率を監視
CPU使用率が70%を超えらたらアラートを生成
あらかじめ設定されたAuto Scalingにより、新規でインスタンスが起動
新規で起動したインスタンスが自分自身をロードバランサのサービスへ追加
API(aXAPI)によるロードバランサの設定
今回使用したロードバランサ vThunder は aXAPI と呼ばれるRESTful APIを搭載しており、こちらを利用することでリモートからコマン
ドが実行できます。今回は、EC2インスタンスのメタデータから自身のプライベートIPア
ドレスを取得し、aXAPIを利用して自身をロードバランサへ追加するサンプルスクリプト
を準備しました。
#!/bin/bash
a10_ip=192.168.10.10 #vThunderのIPアドレス
service_group=websvgrp #vThunderで設定されているサービスグループ
username=******** #vThunderのログインユーザ名
password=******** #vThunderのログインパスワード
url="https://$a10_ip/services/rest/V2/"
# TLSv1と証明書チェックをスキップするためのオプション
cmd_options='--tlsv1.0 --insecure'
# セッションIDの取得
session_id=`curl -v $cmd_options $url \
-d method=authenticate \
-d username=******** \
-d password=******** | \
sed -n -e 's/.*<session_id>\(.*\)<\/session_id>.*/\1/p'`
#自分のプライベートIPアドレス取得
my_ip=`curl http://169.254.169.254/latest/meta-data/local-ipv4`
my_instanceid=`curl http://169.254.169.254/latest/meta-data/instance-id`
# サーバ登録
curl -v -X POST $cmd_options $url \
-d session_id=$session_id \
-d method=slb.server.create \
-d name=$my_instanceid \
-d host=$my_ip \
-d status=1 \
-d health_monitor=http_index \
-d port_list=port1 \
-d port1=port_num,protocol \
-d port_num=80 \
-d protocol=2
# サービスグループへメンバ登録
curl -v -X POST $cmd_options $url \
-d session_id=$session_id \
-d method=slb.service_group.member.create \
-d name=$service_group \
-d member=server,port \
-d server=$my_ip \
-d port=80
実際の運用時はエラー処理などを考慮してスクリプトを作成してください。 A10ネッ
トワークス様のページでPythonコードが解説されていますので、こちらもあわせてご参考ください。 A10 Networks aXAPI with Python
後のステップで、作成したスクリプトをインスタンス起動時に実行するためUserData
に登録します。
AMI化
Auto Scalingでは、自動的に起動するインスタンスのテンプレートイメージにAMI(Amazon Machine Image)を利用します。前項で作成したインスタンスからAMIを作成します。
$ aws ec2 create-image --instance-id i-12345678 --name "ec2websv" --description "AMI for web server running on EC2"
Auto Scalingの設定
まずはLaunch Configurationで、起動するWebサーバを定義します。前項で作成したAMIを利用し、UserDataでは起動時にロードバランサに追加するスクリプトを登録します。
$ aws autoscaling create-launch-configuration --launch-configuration-name ec2websv_launch_conf --image-id ami-14c6d315 --key-name key4tokyo --security-groups sg-7e4fe91b --user-data file:///filepath/userdata.txt --instance-type t2.micro --no-associate-public-ip-address
作成したLaunchConfigurationを利用し、2つのサブネット上に最小2台、最大4台までWebサーバを起動するようにAutoSacling Groupを設定します。
$ aws autoscaling create-auto-scaling-group --auto-scaling-group-name ec2websv_asgrp --launch-configuration-name ec2websv_launch_conf --min-size 2 --max-size 4 --vpc-zone-identifier subnet-33946c44,subnet-451df91c
AutoScaling Group内のインスタンスの平均CPU使用率が70%を超えたらインスタンスを1つ追加するようにScaling PolicyとCloudWatchを設定します。CloudWatchの設定にはScaling PolicyのARNが必要なため、コマンドの結果を控えておきます。
$ aws autoscaling put-scaling-policy --policy-name ec
2websv_scaleout --auto-scaling-group-name ec2websv_asgrp --scaling-adjustment 1 --ad
justment-type ChangeInCapacity
{
"PolicyARN": "arn:aws:autoscaling:ap-northeast-1:<AWS Account ID>:scalingPolicy:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxx:autoScalingGroupName/ec2websv_asgrp:policyName/ec2websv_scaleout"
}
$ aws cloudwatch put-metric-alarm \
--period 300 \
--alarm-name scalout_alerm \
--dimensions Name=AutoScalingGroupName,Value=ec2websv_asgrp \
--namespace "AWS/EC2" \
--metric-name CPUUtilization \
--evaluation-periods 1 \
--statistic Average \
--threshold 70 \
--comparison-operator GreaterThanThreshold \
--alarm-actions arn:aws:autoscaling:ap-northeast-1:<AWS AccountID>:arn:aws:autoscaling:ap-northeast-1:<AWS Account ID>:scalingPolicy:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxx:autoScalingGroupName/ec2websv_asgrp:policyName/ec2websv_scaleout
動作確認
実際に負荷をかけてみて、AWS上のWebサーバ用のインスタンスが自動的に起動するか
、起動したインスタンスがロードバランサ上にメンバーとして追加されているかを確認しましょう。Webクライアントから以下のabコマンドで負荷をかけてみます。
$ ab -c 3 -n 1000 http://10.0.0.101/
コマンド実行後しばらくすると、EC2インスタンスが追加で2台起動し、合計4台となっていることが確認できます。
次回
次回の第3部では、負荷が下がったときに自動的にスケールインをする方法について
ご紹介します。
-ソリューションアーキテクト 吉田