主題教學三

客戶問題

我們要如何設定自動擴展的伺服器叢集,以利達到高可用性呢?

操作步驟

執行初始設定工作

gcloud init --console-only

建立自動啟動伺服器範本

gcloud compute instance-templates create leoyeh-instance-templates --image-family centos-8 --image-project centos-cloud --boot-disk-type=pd-standard --boot-disk-size=10GB --tags=http-server --metadata=startup-script=sudo\ yum\ install\ nginx\ -y\ \&\&\ echo\ Hello\ \$HOSTNAME\ \|\ sudo\ tee\ /usr/share/nginx/html/index.html\ \>\ /dev/null\ \&\&\ sudo\ systemctl\ start\ nginx

設定防火牆

gcloud compute firewall-rules create demo-allow-http --source-ranges "0.0.0.0/0" --allow tcp:80 --target-tags http-server

建立實體群組

gcloud compute instance-groups managed create leoyeh-instance-group-ae1a --zone asia-east1-a --base-instance-name leoyeh-instance-group-ae1a --template leoyeh-instance-templates --size 1
gcloud compute instance-groups managed create leoyeh-instance-group-ae1b --zone asia-east1-b --base-instance-name leoyeh-instance-group-ae1b --template leoyeh-instance-templates --size 1

建立 HTTP 健康狀態檢查

gcloud compute http-health-checks create demo-http-health-check --port 80 --request-path "/" --check-interval 5 --timeout 5 --unhealthy-threshold 2 --healthy-threshold 2

建立 HTTP 後端服務

gcloud compute backend-services create demo-http-backend-service --http-health-checks demo-http-health-check --global --protocol HTTP --port-name demo-port --timeout 30

建立實體群組對應的連接埠

gcloud compute instance-groups set-named-ports leoyeh-instance-group-ae1a --zone asia-east1-a --named-ports demo-port:80
gcloud compute instance-groups set-named-ports leoyeh-instance-group-ae1b --zone asia-east1-b --named-ports demo-port:80

建立實體群組對應 HTTP 後端服務

gcloud compute backend-services add-backend demo-http-backend-service --instance-group leoyeh-instance-group-ae1a --instance-group-zone asia-east1-a --balancing-mode UTILIZATION --max-utilization "0.8" --capacity-scaler "0.75" --global
gcloud compute backend-services add-backend demo-http-backend-service --instance-group leoyeh-instance-group-ae1b --instance-group-zone asia-east1-b --balancing-mode UTILIZATION --max-utilization "0.8" --capacity-scaler "0.75" --global

建立 URL map ,預設將所有請求皆導向到 HTTP 後端服務

gcloud compute url-maps create demo-http-load-balancer --default-service demo-http-backend-service

建立目標 HTTP proxy

gcloud compute target-http-proxies create demo-target-http-proxy --url-map demo-http-load-balancer

保留 global IP address 供 HTTP load balancing 的 global forwarding rule 使用

gcloud compute addresses create demo-http-load-balancing-address --global

查詢 global IP address

gcloud compute addresses describe demo-http-load-balancing-address --global

取得負載平衡器的 IP 位置為 35.227.203.215,之後用於測試負載平衡器。

建立 HTTP load balancing 的 global forwarding rule

gcloud compute forwarding-rules create demo-http-forwarding-rule --address 35.227.203.215 --global --ip-protocol TCP --ports 80 --target-http-proxy demo-target-http-proxy

查看兩個 managed instance group 裡面有哪些 instance

gcloud compute instance-groups managed list-instances leoyeh-instance-group-ae1a --zone asia-east1-a
gcloud compute instance-groups managed list-instances leoyeh-instance-group-ae1b --zone asia-east1-b

測試負載平衡器

watch -n 1 'curl http://35.227.203.215 2>/dev/null'

設定 managed instance group autoscaler

gcloud compute backend-services update-backend demo-http-backend-service --instance-group leoyeh-instance-group-ae1a --instance-group-zone asia-east1-a --balancing-mode RATE --max-rate-per-instance 10 --capacity-scaler "0.9" --global
gcloud compute instance-groups managed set-autoscaling leoyeh-instance-group-ae1a --zone asia-east1-a --max-num-replicas 5 --min-num-replicas 1 --target-load-balancing-utilization "0.8" --cool-down-period 60

測試 managed instance group autoscaler

sudo su -
yum install apr-util -y
yum install yum-utils -y
cd /tmp
yum install yum-utils.noarch
yumdownloader httpd-tools*
rpm2cpio httpd-*.rpm | cpio -idmv
mv usr/bin/ab /usr/bin/ab
gcloud compute instance-groups managed list-instances leoyeh-instance-group-ae1a --zone asia-east1-a

等待幾分鐘之後,當流量沒有這麼龐大時,就會降為 1 台伺服器。

刪除作業

gcloud compute forwarding-rules delete demo-http-forwarding-rule --global
gcloud compute addresses delete demo-http-load-balancing-address --global
gcloud compute target-http-proxies delete demo-target-http-proxy
gcloud compute url-maps delete demo-http-load-balancer
gcloud compute backend-services delete demo-http-backend-service --global
gcloud compute http-health-checks delete demo-http-health-check
gcloud compute instance-groups managed delete leoyeh-instance-group-ae1a --zone asia-east1-a
gcloud compute instance-groups managed delete leoyeh-instance-group-ae1b --zone asia-east1-b
gcloud compute instance-templates delete leoyeh-instance-templates
gcloud compute firewall-rules delete demo-allow-http