1. 簡介
歡迎使用進階負載平衡最佳化程式碼研究室!
在本程式碼研究室中,您將瞭解如何為全域外部應用程式負載平衡器設定進階負載平衡選項。開始之前,建議您先參閱雲端負載平衡說明文件 ( https://cloud.google.com/load-balancing/docs/load-balancing-overview)
圖 1. 使用全域外部應用程式負載平衡器選擇目的地端點的工作流程。
程式碼研究室拓撲和用途
圖 2. HTTP 負載平衡器路由拓樸
在這個程式碼研究室中,您將設定兩個代管執行個體群組。您將建立全域外部 HTTPS 負載平衡器。負載平衡器會使用 Envoy 負載平衡器支援的進階功能清單中的多項功能。部署完成後,您將產生一些模擬負載,並驗證設定是否正常運作。
課程內容
- 如何設定 ServiceLbPolicy 以微調負載平衡器。
軟硬體需求
- 具備外部 HTTPS 負載平衡的相關知識。本程式碼研究室的前半部與「具備進階流量管理功能的 External HTTPs LB」程式碼研究室 (https://codelabs.developers.google.com/codelabs/externalhttplb-adv) 非常相似。建議您先詳閱該文件。
2. 事前準備
在 Cloud Shell 中,確認您已設定專案 ID
gcloud config list project gcloud config set project [YOUR-PROJECT-NAME] prodproject=YOUR-PROJECT-NAME echo $prodproject
啟用 API
啟用所有必要服務
gcloud services enable compute.googleapis.com gcloud services enable logging.googleapis.com gcloud services enable monitoring.googleapis.com gcloud services enable networkservices.googleapis.com
3. 建立虛擬私有雲網路
建立虛擬私有雲網路
透過 Cloud Shell
gcloud compute networks create httplbs --subnet-mode=auto
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/httplbs]. NAME SUBNET_MODE BGP_ROUTING_MODE IPV4_RANGE GATEWAY_IPV4 httplbs AUTO REGIONAL
建立 VPC 防火牆規則
建立虛擬私有雲網路後,您現在可以建立防火牆規則。防火牆規則會用於允許所有 IP 存取測試應用程式網站的 80 號通訊埠外部 IP,以便傳送 HTTP 流量。
透過 Cloud Shell
gcloud compute firewall-rules create httplb-allow-http-rule \ --allow tcp:80 \ --network httplbs \ --source-ranges 0.0.0.0/0 \ --priority 700
輸出
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/httplb-allow-http-rule]. Creating firewall...done. NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED httplb-allow-http-rule httplbs INGRESS 700 tcp:80 False
在本程式碼研究室中,我們將調整 VM 的健康狀態。因此,我們也會建立防火牆規則來允許 SSH。
透過 Cloud Shell
gcloud compute firewall-rules create fw-allow-ssh \ --network=httplbs \ --action=allow \ --direction=ingress \ --target-tags=allow-ssh \ --rules=tcp:22
輸出
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/fw-allow-ssh]. Creating firewall...done. NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED fw-allow-ssh httplbs INGRESS 1000 tcp:22 False
4. 設定代管執行個體群組
您需要設定代管執行個體群組,其中包含 HTTP 負載平衡器使用的後端資源模式。首先,我們會建立執行個體範本,定義各個區域要建立的 VM 設定。接下來,我們會為每個地區的後端建立參照執行個體範本的代管執行個體群組。
代管執行個體群組的範圍可以是區域性或地區性。在本研究室練習中,我們將建立區域代管執行個體群組。
在本節中,您可以看到預先建立的啟動指令碼,這會在建立執行個體時參照。這個開機指令碼會安裝並啟用網路伺服器功能,我們會用來模擬網路應用程式。歡迎查看這個指令碼。
建立執行個體範本
第一步是建立執行個體範本。
透過 Cloud Shell
gcloud compute instance-templates create test-template \ --network=httplbs \ --tags=allow-ssh,http-server \ --image-family=debian-9 \ --image-project=debian-cloud \ --metadata=startup-script='#! /bin/bash apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html systemctl restart apache2'
輸出
NAME MACHINE_TYPE PREEMPTIBLE CREATION_TIMESTAMP test-template n1-standard-1 2021-11-09T09:24:35.275-08:00
您現在可以使用下列 gcloud 指令,驗證是否已成功建立執行個體範本:
透過 Cloud Shell
gcloud compute instance-templates list
輸出
NAME MACHINE_TYPE PREEMPTIBLE CREATION_TIMESTAMP test-template n1-standard-1 2021-11-09T09:24:35.275-08:00
建立執行個體群組
我們現在必須根據先前建立的執行個體範本,建立代管執行個體群組。
透過 Cloud Shell
gcloud compute instance-groups managed create us-east1-a-mig \ --size=1 \ --template=test-template \ --zone=us-east1-a
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-a/instanceGroupManagers/us-east1-a-mig]. NAME LOCATION SCOPE BASE_INSTANCE_NAME SIZE TARGET_SIZE INSTANCE_TEMPLATE AUTOSCALED us-east1-a-mig us-east1-a zone us-east1-a-mig 0 1 test-template no
透過 Cloud Shell
gcloud compute instance-groups managed create us-east1-b-mig \ --size=5 \ --template=test-template \ --zone=us-east1-b
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-b/instanceGroupManagers/us-east1-b-mig]. NAME LOCATION SCOPE BASE_INSTANCE_NAME SIZE TARGET_SIZE INSTANCE_TEMPLATE AUTOSCALED us-east1-b-mig us-east1-b zone us-east1-b-mig 0 5 test-template no
我們可以使用下列 gcloud 指令,確認執行個體群組是否已成功建立:
透過 Cloud Shell
gcloud compute instance-groups list
輸出
NAME LOCATION SCOPE NETWORK MANAGED INSTANCES us-east1-a-mig us-east1-a zone httplbs Yes 1 us-east1-b-mig us-east1-b zone httplbs Yes 5
驗證 Web 伺服器功能
每個執行個體都會設定為執行 Apache 網路伺服器,並搭配簡單的 PHP 指令碼,以便顯示類似以下內容的內容:
網頁由以下位置提供:us-east1-a-mig-ww2h
如要確保網路伺服器運作正常,請前往「Compute Engine」>「VM 執行個體」。請確認新執行個體 (例如 us-east1-a-mig-xxx) 已根據執行個體群組定義建立。
現在,請在瀏覽器中發出網路要求,確保網路伺服器正在執行 (可能需要一分鐘的時間)。在「Compute Engine」的「VM 執行個體」頁面中,選取由執行個體群組建立的執行個體,然後按一下該執行個體的外部 (公開) IP。
或在瀏覽器中前往 http://<IP_Address>
5. 設定負載平衡器
建立健康狀態檢查
首先,我們必須建立基本的健康狀態檢查,確保服務已成功啟用及執行。我們將建立基本健康狀態檢查,還有許多進階自訂選項可供使用。
透過 Cloud Shell
gcloud compute health-checks create http http-basic-check \ --port 80
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/healthChecks/http-basic-check]. NAME PROTOCOL http-basic-check HTTP
保留外部 IP 位址
在這個步驟中,您需要預留全球可用的靜態 IP 位址,稍後會將該位址附加至負載平衡器。
透過 Cloud Shell
gcloud compute addresses create lb-ipv4-2 \ --ip-version=IPV4 \ --global
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/addresses/lb-ipv4-2].
請務必記下已保留的 IP 位址。
gcloud compute addresses describe lb-ipv4-2 \ --format="get(address)" \ --global
建立後端服務
接下來,我們必須為先前建立的代管執行個體群組建立後端服務。
透過 Cloud Shell
gcloud compute backend-services create east-backend-service \ --load-balancing-scheme=EXTERNAL_MANAGED \ --protocol=HTTP \ --port-name=http \ --health-checks=http-basic-check \ --global
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/east-backend-service]. NAME BACKENDS PROTOCOL east-backend-service HTTP
將 MIG 新增至後端服務
建立後端服務後,您必須將先前建立的代管執行個體群組新增至每個後端服務。
透過 Cloud Shell
gcloud compute backend-services add-backend east-backend-service --instance-group us-east1-a-mig --instance-group-zone us-east1-a --global
透過 Cloud Shell
gcloud compute backend-services add-backend east-backend-service --instance-group us-east1-b-mig --instance-group-zone us-east1-b --global
您可以執行下列指令,確認已新增後端。
透過 Cloud Shell
gcloud compute backend-services list
輸出
NAME BACKENDS PROTOCOL east-backend-service us-east1-a/instanceGroups/us-east1-a-mig,us-east1-b/instanceGroups/us-east1-b-mig HTTP
建立網址對應
接下來,我們將建立網址對應。
gcloud compute url-maps create web-map-http \ --default-service=east-backend-service \ --global
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/web-map-http]. NAME DEFAULT_SERVICE web-map-http backendServices/east-backend-service
建立 HTTP 前端
建立負載平衡器的最後一步是建立前端。這會將先前保留的 IP 位址對應至您建立的負載平衡器網址對應。
透過 Cloud Shell
gcloud compute target-http-proxies create http-lb-proxy-adv \ --url-map=web-map-http
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetHttpProxies/http-lb-proxy-adv]. NAME URL_MAP http-lb-proxy-adv web-map-http
接下來,您需要建立通用轉送規則,將先前保留的 IP 位址對應至 HTTP Proxy。
透過 Cloud Shell
gcloud compute forwarding-rules create http-content-rule \ --load-balancing-scheme EXTERNAL_MANAGED \ --address=lb-ipv4-2 \ --global \ --target-http-proxy=http-lb-proxy-adv \ --ports=80
此時,您可以確認負載平衡器是否使用先前記下的 IP 位址。
6. 確認負載平衡器是否運作正常
如要確認負載平衡功能是否正常運作,您需要產生一些負載。為此,我們會建立新的 VM 來模擬負載。
建立 Siege-vm
接下來,您將建立用於產生負載的 siege-vm
透過 Cloud Shell
gcloud compute instances create siege-vm \ --network=httplbs \ --zone=us-east1-a \ --machine-type=e2-medium \ --tags=allow-ssh,http-server \ --metadata=startup-script='sudo apt-get -y install siege'
輸出
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-a/instances/siege-vm]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS siege-vm us-central1-ir1 e2-medium 10.132.0.15 34.143.20.68 RUNNING
接下來,您可以透過 SSH 連線至所建立的 VM。建立完成後,按一下「SSH」即可啟動終端機並建立連線。
連線後,請執行下列指令產生負載。使用您先前為外部 http 負載平衡器保留的 IP 位址。
透過 Cloud Shell
siege -c 20 http://$lb-ipv4-2
輸出
New configuration template added to /home/cloudcurriculumdeveloper/.siege Run siege -C to view the current settings in that file
檢查負載分配
Siege 執行完畢後,請檢查流量是否平均分配給兩個代管執行個體群組。
停止 Siege
您現在已證實進階流量拆分功能運作正常,現在該停止評估了。如要停止測試,請返回 siege-vm 的 SSH 終端機,然後按下 CTRL+C 鍵停止測試。
7. 設定服務 Lb 政策
建立服務 LB 政策
基本設定完成後,我們將建立 Service Lb 政策,並試用進階功能。舉例來說,我們會設定服務使用一些進階負載平衡設定。在這個範例中,我們只會建立政策來啟用自動容量耗盡功能。但歡迎試試其他功能。
透過 Cloud Shell
gcloud beta network-services service-lb-policies create http-policy \ --auto-capacity-drain --location=global
您可以使用下列 gcloud 指令驗證政策是否已成功建立:
透過 Cloud Shell
gcloud beta network-services service-lb-policies list --location=global
輸出
NAME http-policy
將服務負載平衡政策附加至後端服務
我們現在會將新政策附加至上述現有的後端服務。
透過 Cloud Shell
gcloud beta compute backend-services update east-backend-service \ --service-lb-policy=http-policy --global
8. 調整後端健康狀態
此時,新的服務負載平衡政策已套用至後端服務。因此嚴格來說,您可以直接前往清除所用資源。不過在這個程式碼研究室中,我們還將進行其他實際工作環境的調整,向您說明新政策的運作方式。
當健康的後端總數低於某個門檻 (25%) 時,自動容量耗盡功能會自動從負載平衡器中移除後端 MIG。為了測試這項功能,我們將透過 SSH 連線至 us-east1-b-mig 中的 VM,並讓這些 VM 處於不健康狀態。在 25% 的門檻下,您必須透過 SSH 連線至四個 VM,然後關閉 Apache 伺服器。
如要這樣做,請選取四個 VM,然後按一下 SSH 圖示來啟動終端機並連線。然後執行下列指令。
sudo apachectl stop
此時,系統會觸發自動容量耗盡功能,us-east1-b-mig 就不會收到新的要求。
9. 確認自動耗盡容量功能是否正常運作
重新開始圍攻
為了驗證新功能,我們會再次重複使用 siege VM。讓我們透過 SSH 連線至先前步驟中建立的 VM。建立完成後,按一下「SSH」即可啟動終端機並連線。
連線後,請執行下列指令產生負載。使用您先前為外部 http 負載平衡器保留的 IP 位址。
透過 Cloud Shell
siege -c 20 http://$lb-ipv4-2
輸出
New configuration template added to /home/cloudcurriculumdeveloper/.siege Run siege -C to view the current settings in that file
此時,您會發現所有要求都會傳送至 us-east1-a-mig。
Stop the Siege
您已證明進階流量分配功能運作正常,現在可以停止圍攻了。如要這麼做,請返回 siege-vm 的 SSH 終端機,然後按下 CTRL+C 鍵來停止 siege 執行。
10. 清理步驟
研究室環境現已結束,接下來該拆除。請執行下列指令,刪除測試環境。
透過 Cloud Shell
gcloud compute instances delete siege-vm --zone=us-east1-a gcloud compute forwarding-rules delete http-content-rule --global gcloud compute target-http-proxies delete http-lb-proxy-adv gcloud compute url-maps delete web-map-http gcloud compute backend-services delete east-backend-service --global gcloud compute addresses delete lb-ipv4-2 --global gcloud compute health-checks delete http-basic-check gcloud beta network-services service-lb-policies delete http-policy --location=global gcloud compute instance-groups managed delete us-east1-a-mig --zone=us-east1-a gcloud compute instance-groups managed delete us-east1-b-mig --zone=us-east1-b gcloud compute instance-templates delete test-template gcloud compute firewall-rules delete httplb-allow-http-rule gcloud compute firewall-rules delete fw-allow-ssh gcloud compute networks delete httplbs
11. 恭喜!
恭喜您完成程式碼研究室!
涵蓋內容
- 建立具有 Service lb 政策的外部應用程式負載平衡器。
- 使用自動容量排除功能設定後端服務。
後續步驟
- 試用服務 lb 政策提供的其他功能。