https://cloud.google.com/compute/docs/tutorials/globally-autoscaling-a-web-service-on-compute-engine?hl=ja#gcloud
https://saitosho.me/blog/2021-05-03-structing-auto-scaling-instance-groups/
https://blog.jicoman.info/2018/10/configure-gcp-autoscaler-by-cli/
-- 1. 前作業
gcloud init
gcloud auth list
gcloud --version
gcloud projects create project01-9999999 \
--name="project01"
gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet
gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111
gcloud services enable compute.googleapis.com --project project01-9999999
-- 2. インスタンステンプレート作成
gcloud compute instance-templates create template01 \
--image-family debian-10 \
--image-project debian-cloud \
--machine-type e2-micro \
--network-interface=network-tier=STANDARD \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--tags tag01 \
--metadata startup-script='apt update && apt -y install apache2'
gcloud compute instance-templates list
gcloud compute instance-templates describe template01
-- 3. マネージドインスタンスグループ作成
gcloud compute instance-groups managed create group01 \
--template template01 \
--size 1 \
--zone asia-northeast1-a
gcloud compute instance-groups list
gcloud compute instance-groups describe group01
-- 4. 接続確認
gcloud compute firewall-rules create fw01 \
--target-tags tag01 \
--allow tcp:80
gcloud compute firewall-rules list
gcloud compute instances list
curl 192.0.2.1
-- 5. スケーリング設定
gcloud compute instance-groups managed set-autoscaling group01 \
--max-num-replicas 2 \
--min-num-replicas 1 \
--cool-down-period 60 \
--target-cpu-utilization 0.3
-- 6. 動作確認
gcloud compute instances list
gcloud compute ssh group01-1111
インスタンスにログインして実行
yes > /dev/null &
インスタンスが2個に増えることを確認
負荷コマンドを停止後インスタンスが1個に戻ることを確認
-- 7. クリーンアップ
gcloud projects list
gcloud projects delete project01-9999999