https://cloud.google.com/compute/docs/instance-groups/autohealing-instances-in-migs?hl=ja
https://cloud.google.com/compute/docs/tutorials/high-availability-autohealing?hl=ja
-- 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 health-checks create http hc01 \
--global \
--check-interval 10 \
--timeout 5 \
--healthy-threshold 2 \
--unhealthy-threshold 3 \
--request-path "/"
gcloud compute health-checks list
gcloud compute firewall-rules create fw01 \
--network default \
--allow tcp:80 \
--source-ranges 130.211.0.0/22,35.191.0.0/16
gcloud compute firewall-rules list
-- 3. インスタンステンプレート作成
gcloud compute instance-templates create template01 \
--image-family debian-11 \
--image-project debian-cloud \
--machine-type e2-micro \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--tags tag01 \
--metadata startup-script='apt update && apt -y install apache2 && systemctl start apache2'
gcloud compute instance-templates list
gcloud compute instance-templates describe template01
-- 4. マネージドインスタンスグループ作成
gcloud compute instance-groups managed create mig01 \
--zone asia-northeast1-a \
--template template01 \
--size 1 \
--health-check hc01 \
--initial-delay 90
gcloud compute instance-groups list
gcloud compute instance-groups describe mig01
gcloud compute firewall-rules create fw02 \
--network default \
--allow tcp:80 \
--target-tags tag01
gcloud compute firewall-rules list
gcloud compute instances list
gcloud compute ssh mig01-llcw
curl 192.0.2.1
-- 5. ヘルスチェック失敗をシミュレートする
while : ; do \
gcloud compute instance-groups managed list-instances mig01 \
--zone asia-northeast1-a \
; sleep 5; date; done
ログインしてApache停止
systemctl stop apache2
systemctl status apache2
gcloud compute instances list
gcloud compute ssh mig01-llcw
-- 6. クリーンアップ
gcloud compute instance-groups managed delete mig01 --zone asia-northeast1-a -q
gcloud compute instance-templates delete template01 -q
gcloud compute health-checks delete hc01 -q
gcloud compute firewall-rules delete fw01 fw02 -q
gcloud projects list
gcloud projects delete project01-9999999
gcloud beta billing projects unlink project01-9999999