本日も乙

ただの自己満足な備忘録。

コマンドラインでGCP Autoscaler(GCE + GCLB)を設定する

コマンドライン(gcloudコマンド)でGCP Autoscaler *1 を設定したときのメモです。詳細の説明は省いています。
Autoscalerを設定するために、インスタンステンプレート、マネージドインスタンスグループ、Google ロードバランサ(GCLB)の作成も行いました。実行リージョンは東京(asia-northeast1)です。

インスタンステンプレートの作成

インスタンステンプレート は AWS の起動設定(Launch Configuration)と起動テンプレート(Launch Templates)を合わせたようなものです。Autoscaler 以外にも使えて、テンプレート化しておくことで一からの GCE インスタンスの作成が楽になります。
今回は basic-template-YYYYMMDDHHMM という名前で作成します。インスタンステンプレートは更新ができず、変更がある場合は都度作り直しになるため、テンプレート名に日時を入れてユニークにしています。

$ gcloud compute instance-templates create basic-template-$(date "+%Y%m%d%H%M") \
  --machine-type=n1-standard-1 \
  --subnet=projects/sample-project/regions/asia-northeast1/subnetworks/dmz \
  --maintenance-policy=MIGRATE \
  --service-account=xxxxxxxxxx-compute@developer.gserviceaccount.com \
  --scopes=https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/trace.append,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/devstorage.read_write \
  --region=asia-northeast1 \
  --tags=web \
  --image=sample-image \
  --image-project=sample-project \
  --boot-disk-size=20GB \
  --boot-disk-type=pd-standard \
  --boot-disk-device-name=basic-template-$(date "+%Y%m%d%H%M")

# インスタンステンプレート名を取得する
$ instance_template=$(gcloud compute instance-templates list | \
  awk '$1 ~ /^basic-template-[0-9]/ { print $1 }' | sort -r | head -n 1)

マネージドインスタンスグループの作成

マネージドインスタンスグループ は AWS の Auto Scaling Groupに相当します。
スケールアウトするしきい値(CPU使用率、1秒あたりのリクエスト数: RPS、Stackdriverのメトリクス)を設定します。
今回はGCEインスタンスのCPU使用率が60%以上になった場合にスケールアウトするようにしました。

$ gcloud beta compute instance-groups managed create "basic-group" \
  --base-instance-name "web" \
  --template $instance_template \
  --size "1" \
  --zones "asia-northeast1-a,asia-northeast1-b,asia-northeast1-c"

# CPU使用率 60% 以上になった場合にスケールアウトする
$ gcloud compute instance-groups managed set-autoscaling "basic-group" \
  --region "asia-northeast1" \
  --cool-down-period "60" \
  --max-num-replicas "10" \
  --min-num-replicas "1" \
  --target-cpu-utilization "0.6"

ヘルスチェックの作成

Googleロードバランサ(GCLB)が配下のGCEインスタンスに対して、死活監視するエンドポイントを作成します。
以下の設定は 30秒ごとに /index.html にアクセスして、2回連続で 200 OKを返せば GCLB にGCEインスタンスがアタッチされます。
逆に連続して5回連続で正常にリクエストが帰ってこない場合は GCLB は GCEインスタンスにリクエストを送信しません。

$ gcloud compute http-health-checks create "basic-check" \
  --port "80" \
  --request-path "/index.html" \
  --check-interval "30" \
  --timeout "10" \
  --unhealthy-threshold "5" \
  --healthy-threshold "2"

GCLBの作成

いよいよロードバランサ(GCLB)を作成していきます。

Backend Serviceの作成・設定

$ gcloud beta compute backend-services create basic-backend \
  --global \
  --connection-draining-timeout=300 \
  --http-health-checks=basic-check \
  --load-balancing-scheme=EXTERNAL \
  --protocol=HTTP \
  --timeout=60

先ほど作成したインスタンスグループを Backend Service に登録します。
分散モードをCPU使用率、Max CPU使用率を80%にすることで、実際には 60% * 80% = 48% になるようにGCEインスタンスの数が調整されます。

$ gcloud beta compute backend-services add-backend basic-backend \
  --global \
  --instance-group=basic \
  --instance-group-region=asia-northeast1 \
  --balancing-mode=UTILIZATION \
  --max-utilization=0.8

セキュリティポリシーを Backend Service にアタッチします。gcloud コマンドによるセキュリティポリシーの作成とアタッチは Alpha 版のみの提供なので、今回はブラウザ上で行いました。
ちなみに gcloud コマンドだと以下のようになります。

$ gcloud alpha compute backend-services set-security-policy basic-backend \
  --global \
  --security-policy=basic-security-policy

Required 'Alpha Access' permission for 'Compute API'

URLマッピング の設定

URLマッピング(=ロードバランサ名)を設定します。
今回はパス毎に Backend Service を分けることをしないのでシンプルな設定になります。

$ gcloud beta compute url-maps create basic-lb \
  --default-service=basic-backend

ターゲットHTTPSプロキシを作成する前にSSLの設定を作成します。
事前にSSL証明書を購入するか Certbot で生成してSSL証明書を発行しておきます。
cert.pem が証明書ファイル、 privatekey.pem が証明書の秘密鍵ファイルです。

$ gcloud beta compute ssl-certificates create example-com-$(date "+%Y%m%d%H%M") \
 --certificate=cert.pem \
 --private-key=privatekey.pem
$ ssl_certificate=$(gcloud beta compute ssl-certificates list | awk '$1 ~ /^example-com-[0-9]/ { print $1 }' | sort -r | head -n 1)

ターゲットHTTPSプロキシを作成します。

$ gcloud beta compute target-https-proxies create basic-target-https-proxy \
  --url-map=basic-lb \
  --ssl-certificates $ssl_certificate

フロントエンドを作成してロードバランサーとしてアクセスできるようにします。

$ gcloud beta compute forwarding-rules create basic-frontends \
  --global \
  --target-https-proxy=basic-target-https-proxy \
  --ip-protocol=TCP \
  --ports=443

# GCLB のグローバルIPアドレスを取得
ipaddr=$(gcloud compute forwarding-rules describe basic-frontends \
  --global | grep 'IPAddress' | awk '{print $2}')

作成したら先ほど取得したグローバルIPアドレスに対してアクセスして 200 OK が返ってくれば完了です。

$ curl https://$ipaddr/

最後に

gcloud コマンドでマネージドインスタンスグループや GCLB 、Autoscaler を作成しました。ロードバランサは AWS の ELB とアーキテクトが異なっており、コマンドで作ろうと思ったら AWS よりも遥かに難しくなります。
GCP のロードバランサもメリットが大きいのですが、理解しなければならない概念が多いので苦労しました。

*1:AWSのAuto Scalingに相当