在 Compute Engine 中,以全球為範圍自動調度網路服務的資源


本教學課程說明如何透過可依容量需求自動調度資源的地區性 Compute Engine 代管執行個體群組,設定全球通用的網路服務。您可以運用本課程提及的技術,在 Compute Engine 中實際建立規模遍及全球的可擴充專案。

目標

  • 部署多個啟用自動調度資源的地區性 Compute Engine 代管執行個體群組。
  • 建立跨地區負載平衡器。
  • 從世界上各個不同地區產生測試流量。
  • 透過 Google Cloud 主控台,以視覺化的方式瞭解負載平衡器如何提出要求,以及執行個體群組如何視需求自動調度資源。

費用

本教學課程使用 Google Cloud 的計費元件,包括:

  • Compute Engine

事前準備

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project.

  3. Enable the Compute Engine API.

    Enable the API

應用程式架構

應用程式包含以下 Compute Engine 元件:

  1. 執行個體範本:這類範本是用於在執行個體群組中建立各個執行個體。
  2. 執行個體群組:多個執行個體群組,會根據連入流量自動調度資源。
  3. 負載平衡器:HTTP 負載平衡器,會將流量分配至所有執行個體群組。
  4. 執行個體:多個測試執行個體,會從全球各地產生測試流量。

系統架構圖表,顯示內含多個地區性執行個體群組的負載平衡器

設定網路服務

建立執行個體群組

主控台

  1. 建立執行個體群組網路。

    1. 在 Google Cloud 控制台中,前往「虛擬私有雲網路」頁面。

      前往「VPC networks」(虛擬私有雲網路)

    2. 按一下「建立虛擬私有雲網路」

    3. 將「Name」(名稱) 設為 fortressnet

    4. 將「Subnet creation mode」(子網路建立模式) 設為 [Automatic] (自動)

    5. 按一下頁面底部的 [建立]

  2. 建立網路防火牆規則。這項規則會允許所有傳送至執行個體的 HTTP 要求。

    1. 在 Google Cloud 控制台中,前往「防火牆規則」頁面。

      前往「防火牆規則」

    2. 按一下「建立防火牆規則」

    3. 將「Name」(名稱) 設為 fortressnet-allow-http

    4. 在「Network」中選取 fortressnet

    5. 在「Targets」中選取 All instances in the network

    6. 將「Source IPv4 ranges」(來源 IPv4 範圍) 設為 0.0.0.0/0

    7. 在「Protocols and ports」(通訊協定和通訊埠) 中,選擇「Specified protocols and ports」(指定的通訊協定和通訊埠),然後選取「tcp」核取方塊並輸入 80

    8. 按一下 [建立]。

  3. 建立執行個體範本,並在其中加入開機指令碼,以便在每個執行個體上啟動簡易 Apache 網路伺服器。

    1. 前往 Google Cloud 控制台的「Instance templates」(執行個體範本) 頁面。

      前往「Instance templates」(執行個體範本) 頁面

    2. 點選「建立執行個體範本」

    3. 將「Name」(名稱) 設為 fort-template

    4. 在「Machine configuration」中選取 e2-micro (2 個 vCPU,1 GB 記憶體)。

    5. 在「Advanced options」專區中,展開「Networking」,然後執行下列操作:

      1. 在「Network interfaces」(網路介面) 區段中,展開要編輯的網路介面。
      2. 在「Network」(網路) 中選取 fortressnet
    6. 在「Management」(管理) 區段的「Automation」(自動) 下方,輸入以下「開機指令碼」

      apt update && apt -y install apache2
      

    7. 按一下 [建立]。

  4. 利用執行個體範本建立多個地區性代管執行個體群組,並對各個執行個體群組設定自動調度資源。

    1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

      前往「Instance groups」(執行個體群組)

    2. 按一下「Create instance group」,建立新的受管理執行個體群組。

    3. 選取「New managed instance group (stateless)」(新增代管執行個體群組 (無狀態))

    4. 將「Name」(名稱) 設為 us-central1-pool

    5. 在「Instance template」(執行個體範本) 中選取 fort-template

    6. 在「Location」(位置) 中,選取「Multiple zones」(多可用區)

    7. 在「區域」部分,選取 us-central1。在「Zones」(可用區) 的部分,保留選取的預先定義值。

    8. 針對「Autoscaling mode」(自動調度資源模式),選取「On: add and remove instances to the group」(開啟:在群組中新增和移除執行個體)

    9. 將「Minimum number of instances」(執行個體數量下限) 設為 1

    10. 將「Maximum number of instances」(執行個體數量上限) 設為 5

    11. 針對「Autoscaling signals」,編輯預設選項 (CPU 使用率),並將「信號類型」設為「HTTP 負載平衡使用率」

    12. 將「Target HTTP load balancing utilization」(目標 HTTP 負載平衡使用率) 設為 80

    13. 按一下 [完成]

    14. 按一下「建立」,對話方塊會顯示訊息,指出您還必須將執行個體群組指派至 HTTP 負載平衡器的後端服務。

    15. 在確認對話方塊中,按一下「確認」。建立所有執行個體群組後,您可以設定負載平衡器

    16. 重複上述步驟建立另外兩個執行個體群組,過程中須留意以下差異:

      • 將其中一個群組的「名稱」設為 europe-west1-pool,且「地區」設為 europe-west1
      • 將其中一個群組的「名稱」設為 asia-east1-pool,且「地區」設為 asia-east1
  5. (選用) 確認執行個體處於良好的健康狀態並能提供 HTTP 流量,同時測試一或多個執行個體的外部 IP 位址。請稍候片刻,等待執行個體完成啟動程序。

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往 VM 執行個體

    2. 確認每個執行中的執行個體在「狀態」欄中,其名稱旁有綠色勾號。

    3. 複製執行個體的「外部 IP」,並貼到網路瀏覽器中。

    瀏覽器應該會顯示「Apache2 Debian Default Page」(Apache2 Debian 預設頁面) 網頁。

    如果您並未看到上述內容,請稍等幾分鐘後再試一次。

gcloud

  1. 建立執行個體群組網路。

    gcloud compute networks create fortressnet --subnet-mode auto
    
  2. 建立網路防火牆規則。這項規則會允許所有傳送至執行個體的 HTTP 要求。

    gcloud compute firewall-rules create fortressnet-allow-http \
        --network fortressnet \
        --allow tcp:80
    
  3. 建立執行個體範本,並在其中加入開機指令碼,以便在每個執行個體上啟動簡易 Apache 網路伺服器。

    gcloud compute instance-templates create fort-template \
        --machine-type e2-micro \
        --network fortressnet \
        --metadata startup-script='apt update && apt -y install apache2'
    
  4. 利用執行個體範本建立多個地區性代管執行個體群組,並對各個執行個體群組設定自動調度資源。

    gcloud compute instance-groups managed create us-central1-pool \
        --region us-central1 \
        --template fort-template \
        --size 1
    gcloud compute instance-groups managed set-autoscaling us-central1-pool \
        --region us-central1 \
        --min-num-replicas 1 \
        --max-num-replicas 5 \
        --scale-based-on-load-balancing \
        --target-load-balancing-utilization .8
    
    gcloud compute instance-groups managed create europe-west1-pool \
        --region europe-west1 \
        --template fort-template \
        --size 1
    gcloud compute instance-groups managed set-autoscaling europe-west1-pool \
        --region europe-west1 \
        --min-num-replicas 1 \
        --max-num-replicas 5 \
        --scale-based-on-load-balancing \
        --target-load-balancing-utilization .8
    
    gcloud compute instance-groups managed create asia-east1-pool \
        --region asia-east1 \
        --template fort-template \
        --size 1
    gcloud compute instance-groups managed set-autoscaling asia-east1-pool \
        --region asia-east1 \
        --min-num-replicas 1 \
        --max-num-replicas 5 \
        --scale-based-on-load-balancing \
        --target-load-balancing-utilization .8
    
  5. (選用) 確認執行個體處於良好的健康狀態並能提供 HTTP 流量,同時測試一或多個執行個體的外部 IP 位址。請稍候片刻,等待執行個體完成啟動程序。

    1. 列出執行個體。

      gcloud compute instances list
      

    2. STATUS 一欄下方,確認執行個體處於 RUNNING 狀態。

    3. 如要檢查執行個體,請在 EXTERNAL_IP 欄下方查詢執行個體的 IP 位址。

      curl http://EXTERNAL_IP | head
      

    系統應該會顯示一些 HTML 文字,其中包括以下這列內容:<title>Apache2 Debian Default Page: It works</title>

    如果您並未看到上述內容,請稍等幾分鐘後再試一次。

設定負載平衡器

負載平衡器會將用戶端要求分配至多個後端。

主控台

開始設定

  1. 在 Google Cloud 控制台中,前往「Load balancing」(負載平衡) 頁面。

    前往「Load balancing」(負載平衡) 頁面

  2. 點選「建立負載平衡器」
  3. 在「Type of load balancer」(負載平衡器類型)部分,選取「Application Load Balancer (HTTP/HTTPS)」(應用程式負載平衡器 (HTTP/HTTPS)),然後點選「Next」(下一步)
  4. 在「公開或內部」部分,選取「公開 (外部)」,然後點選「下一步」
  5. 在「全域或單一區域部署」部分,選取「最適合全域工作負載」,然後點選「Next」
  6. 在「Load balancer generation」(負載平衡器代別) 部分,選取「Global external Application Load Balancer」(全域外部應用程式負載平衡器),然後點選「Next」(下一步)
  7. 按一下 [設定]

基本設定

  1. 將「負載平衡器名稱」設為 fortressnet-balancer

後端設定

  1. 在「Create global external Application Load Balancer」(建立全域外部應用程式負載平衡器) 頁面上,按一下「Backend configuration」(後端設定)
  2. 在「Create or select backend services & backend buckets」(建立或選擇後端服務與後端 Bucket) 下拉式選單中,依序選取「後端服務」和「建立後端服務」。系統會隨即顯示「建立後端服務」對話方塊。
  3. 將後端服務的「Name」設為 fortressnet-backend-service
  4. 在「新增後端」對話方塊下方,將「執行個體群組」設為 asia-east1-pool
  5. 「Balancing mode」(平衡模式) 請選取 [Rate] (速率)
  6. 將每個執行個體的「Maximum RPS」(每秒要求數上限) 設為 100 個要求。
  7. 按一下 [完成]
  8. 點選「新增後端」
  9. 在「新增後端」對話方塊下方,將「執行個體群組」設為 europe-west1-pool
  10. 「Balancing mode」(平衡模式) 請選取 [Rate] (速率)
  11. 將每個執行個體的「Maximum RPS」(每秒要求數上限) 設為 100 個要求。
  12. 按一下 [完成]
  13. 點選「新增後端」
  14. 在「新增後端」對話方塊下方,將「執行個體群組」設為 us-central1-pool
  15. 「Balancing mode」(平衡模式) 請選取 [Rate] (速率)
  16. 將每個執行個體的「Maximum RPS」(每秒要求數上限) 設為 100 個要求。
  17. 按一下 [完成]
  18. 在「健康狀態檢查」下方,選取 [建立健康狀態檢查]
  19. 將「Name」(名稱) 設為 http-basic-check
  20. 在「Protocol」(通訊協定) 中選取 HTTP
  21. 將「Port」(通訊埠) 設為 80
  22. 按一下 [儲存並繼續]
  23. 按一下 [建立]。

主機與路徑規則

  1. 在「Create global external Application Load Balancer」(建立全球外部應用程式負載平衡器)頁面的左側面板上,按一下「Host and path rules」(主機與路徑規則)
    在這個範例中,由於所有流量都會遵循預設規則,因此不需要設定任何主機或路徑規則,只要接受系統預先填入的預設值即可。

前端設定

  1. 在「Create global external Application Load Balancer」(建立全域外部應用程式負載平衡器)頁面的左側面板中,按一下「Frontend configuration」(前端設定)
  2. 將「Name」(名稱) 設定為 fortressnet-http-rule
  3. 針對「IP 版本」,選取 [IPv4]
  4. 針對「IP 位址」,選取 [建立 IP 位址]
  5. 在「預約新的靜態 IP 位址」對話方塊中,將「名稱」設為 fortressnet-ip
  6. 按一下 [Reserve] (預約),然後稍等幾分鐘。
  7. 在「New Frontend IP and port」(新增的前端 IP 和通訊埠) 對話方塊底部,按一下 [Done] (完成)
  8. 按一下 [Add frontend IP and port] (新增前端 IP 和通訊埠)
  9. 將「Name」(名稱) 設定為 fortressnet-http-ipv6-rule
  10. 針對「IP 版本」,選取 [IPv6]
  11. 針對「IP 位址」,選取 [建立 IP 位址]
  12. 在對話方塊中,將「Name」設為 fortressnet-ipv6
  13. 按一下 [Reserve] (預約),然後稍等幾分鐘。
  14. 在「New Frontend IP and port」(新增的前端 IP 和通訊埠) 對話方塊底部,按一下 [Done] (完成)

檢查並完成

  1. 在「Create global external Application Load Balancer」(建立全域外部應用程式負載平衡器)頁面的左側面板上,按一下「Review and finalize」(檢查並完成)
  2. 將您的設定與您打算建立的內容比較。
  3. 如果設定正確無誤,請按一下左側面板底部的 [Create] (建立),接著您會回到「負載平衡」畫面。建立完成的負載平衡器如果正在運作中,它的旁邊會顯示綠色勾號。

gcloud

後端設定

  1. 建立基本的健康狀態檢查,以便確認負載平衡器後端是否正在回應 HTTP 要求。

    gcloud compute health-checks create http http-basic-check
    
  2. 建立全域後端服務,這項後端服務會接收負載平衡器傳送的 HTTP 流量。

    gcloud compute backend-services create fortressnet-backend-service \
        --protocol HTTP \
        --health-checks http-basic-check \
        --global
    
  3. 將執行個體群組新增為後端服務的地區性後端。這項設定會根據每個執行個體的每秒要求數上限,將流量分配至所有後端。

    gcloud compute backend-services add-backend fortressnet-backend-service \
        --balancing-mode RATE \
        --max-rate-per-instance 100 \
        --instance-group us-central1-pool \
        --instance-group-region us-central1 \
        --global
    gcloud compute backend-services add-backend fortressnet-backend-service \
        --balancing-mode RATE \
        --max-rate-per-instance 100 \
        --instance-group europe-west1-pool \
        --instance-group-region europe-west1 \
        --global
    gcloud compute backend-services add-backend fortressnet-backend-service \
        --balancing-mode RATE \
        --max-rate-per-instance 100 \
        --instance-group asia-east1-pool \
        --instance-group-region asia-east1 \
        --global
    

主機與路徑規則

  1. 定義網址對應,網址對應會將不同網址轉送至不同的後端服務。由於本範例中只有一項後端服務,因此可直接將該後端服務設為所有網址的預設服務。

    gcloud compute url-maps create fortressnet-balancer \
        --default-service fortressnet-backend-service
    
  2. 建立 HTTP Proxy 路徑。HTTP Proxy 路徑會接受 HTTP 要求,並根據網址對應加以轉送。在本範例中,HTTP Proxy 路徑會將所有要求傳送至您唯一的後端服務。

    gcloud compute target-http-proxies create fortressnet-http-proxy \
        --url-map fortressnet-balancer
    

前端設定

  1. 建立兩個全域靜態外部 IP 位址,分別用於 IPv4 和 IPv6。這兩者將成為負載平衡器的全域外部 IP 位址。

    gcloud compute addresses create fortressnet-ip \
        --ip-version IPV4 \
        --network-tier=PREMIUM \
        --global
    gcloud compute addresses create fortressnet-ipv6 \
        --ip-version IPV6 \
        --network-tier=PREMIUM \
        --global
    
  2. 查詢負載平衡器的外部 IP 位址。

    gcloud compute addresses list
    
  3. 針對外部 IP 位址建立通用轉送規則,這項規則會將 IPV4 和 IPv6 HTTP 要求轉送至您的 HTTP Proxy。

    gcloud compute forwarding-rules create fortressnet-http-rule \
        --load-balancing-scheme=EXTERNAL \
        --network-tier=PREMIUM \
        --global \
        --target-http-proxy fortressnet-http-proxy \
        --ports 80 \
        --address LOAD_BALANCER_IP_ADDRESS
    
    gcloud compute forwarding-rules create fortressnet-http-ipv6-rule \
        --load-balancing-scheme=EXTERNAL \
        --network-tier=PREMIUM \
        --global \
        --target-http-proxy fortressnet-http-proxy \
        --ports 80 \
        --address LOAD_BALANCER_IPV6_ADDRESS
    

(選用) 確認負載平衡器正在運作,過程中您可能會需要等待幾分鐘。

控制台

  1. 在 Google Cloud 控制台中,前往「Load balancing」(負載平衡) 頁面。

    前往「負載平衡」

  2. 等待 fortressnet-balancer 在「後端」一欄下方顯示綠色勾號。

  3. 按一下 fortressnet-balancer

  4. 在「前端」的「IP:Port」欄下方,複製 IPv4 位址 (IPv4 位址的格式應為 www.xxx.yyy.zzz)。您不需複製結尾的通訊埠編號 :nn)。如果找不到「前端」區段,請稍等幾分鐘後再試著重新載入網頁。

  5. 在網路瀏覽器中輸入 IP 位址。

瀏覽器應該會顯示「Apache2 Debian Default Page」(Apache2 Debian 預設頁面) 網頁。

如果網頁顯示「404 錯誤 (找不到網頁)」,請試著再等幾分鐘。

gcloud

  1. 查詢負載平衡器的外部 IP 位址。

    gcloud compute addresses list
    
  2. 查詢 IPv4 位址。(IPv4 位址的格式應為 www.xxx.yyy.zzz)。

    curl http://LOAD_BALANCER_IP_ADDRESS | head
    

系統應該會顯示一些 HTML 文字,其中包括以下這列內容:<title>Apache2 Debian Default Page: It works</title>

如果畫面顯示 <title>Error 404 (Not Found)!!1</title>,請再等候幾分鐘。

最佳做法:建立安全的防火牆,僅允許負載平衡器和健康狀態檢查傳送的內部流量。接著,將允許所有 HTTP 要求的原有防火牆刪除。這種做法可以防止個別執行個體對外部用戶端開放存取。

控制台

  1. 建立新的防火牆,僅允許負載平衡器和健康狀態檢查傳送的流量。

    1. 在 Google Cloud 控制台中,前往「防火牆規則」頁面。

      前往「防火牆規則」

    2. 按一下「建立防火牆規則」

    3. 將「Name」(名稱) 設為 fortressnet-allow-load-balancer

    4. 在「Network」中選取 fortressnet

    5. 在「Targets」中選取 All instances in the network

    6. 針對「來源 IP 範圍」,輸入 130.211.0.0/22 並按下 Enter 鍵,然後輸入 35.191.0.0/16 並再次按下 Enter 鍵。

    7. 在「Protocols and ports」(通訊協定和通訊埠) 下方選取「tcp」,然後輸入 80

    8. 按一下 [建立]。

  2. 將允許所有流量的原有防火牆刪除。

    1. 選取 fortressnet-allow-http 旁邊的勾號。
    2. 按一下頁面頂端的 [Delete] (刪除)
    3. 在對話方塊中按一下 [刪除]

gcloud

  1. 建立新的防火牆,僅允許負載平衡器和健康狀態檢查傳送的流量。

    gcloud compute firewall-rules create fortressnet-allow-load-balancer \
        --network fortressnet \
        --source-ranges 130.211.0.0/22,35.191.0.0/16 \
        --allow tcp:80
    
  2. 將允許所有流量的原有防火牆刪除。

    gcloud compute firewall-rules delete fortressnet-allow-http -q
    

(選用) 確認自動調度資源和負載平衡正常運作

產生一些測試流量

假設歐洲現在是上午,您的網路服務在網際網路上突然出現使用者暴增的情況。以下步驟會根據這個假設,從歐洲瞬間產生大量的用戶端要求。

主控台

  1. 建立透過 Siege 負載測試工具安裝的執行個體。

    1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

      前往「Create an instance」(建立執行個體)

    2. 將「Name」(名稱) 設為 europe-loadtest

    3. 在「區域」部分,選取 europe-west1

    4. 如要設定進階選項,請展開「Advanced options」部分,然後執行下列操作:

      1. 展開「管理」部分。
      2. 在「Automation」欄位中,輸入以下開機指令碼:
        apt -y install siege
        
    5. 如要建立 VM,請按一下「建立」

  2. 取得負載平衡器的 IPv4 位址。

    1. 在 Google Cloud 控制台中,前往「Load balancing」(負載平衡) 頁面。

      前往「負載平衡」

    2. 按一下「fortressnet-balancer」。

    3. 在「前端」的「IP:Port」欄下方,複製 IPv4 位址 (IPv4 位址的格式應為 www.xxx.yyy.zzz)。

  3. 透過 SSH 登入用於測試負載的執行個體。

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往 VM 執行個體

    2. 等待 europe-loadtest 執行個體在「名稱」一欄下方顯示綠色勾號。

    3. 在「Connect」欄下方的 europe-loadtest 上,按一下「SSH」

  4. 啟動 Siege,並將負載平衡器的 IPv4 位址設為目標。

    siege -c150 http://LOAD_BALANCER_IP_ADDRESS
    

gcloud

  1. 建立透過 Siege 負載測試工具安裝的執行個體。

    gcloud compute instances create europe-loadtest \
        --network default \
        --zone europe-west1-c \
        --metadata startup-script='apt -y install siege'
    
  2. 取得負載平衡器的 IPv4 位址。

    gcloud compute addresses list
    
  3. 開啟新的殼層工作階段,您可以在其中使用 gcloud 指令。

    1. 在新的殼層工作階段中,透過 SSH 登入用於測試負載的執行個體。

      gcloud compute ssh --zone europe-west1-c europe-loadtest
      
    2. 啟動 Siege,並將負載平衡器的 IPv4 位址設為目標。

      siege -c150 http://LOAD_BALANCER_IP_ADDRESS
      

執行 siege 指令後,您應該會在輸出結果中看到以下宣告:The server is now under siege...

[alert] Zip encoding disabled; siege requires zlib support to enable it
** SIEGE 4.0.2
** Preparing 150 concurrent users for battle.
The server is now under siege...

監控負載平衡和自動調度資源

  1. 在 Google Cloud 控制台中,前往「Load balancing」(負載平衡) 頁面。

    前往「負載平衡」

  2. 按一下名為 fortressnet-balancer 的負載平衡器。

  3. 按一下 [Monitoring] (監控) 分頁標籤。

  4. 在「Backend」(後端) 下拉式選單中,選取 fortressnet-backend-service

系統最多可能需要 10 分鐘才能顯示足夠的資料,您應該很快就會看到類似下圖的畫面:

Google Cloud 控制台的監控畫面,顯示系統如何將源自歐洲的要求平均分配至三個後端

情況說明:

  1. 負載測試瞬間開始傳送龐大流量。起初,負載平衡器將要求平均分配至三個後端。要求數量很快就超出您設定的自動調度資源限制,這可能會導致伺服器傳回 Backend 5xx errors 並顯示於監控畫面上。自動配置器開始依需求啟動額外的執行個體。

  2. 系統會透過自動調度資源滿足容量需求。為了儘量縮短要求的延遲時間,Compute Engine 負載平衡器會嘗試將要求轉送至最接近用戶端的後端。由於此範例中的負載測試流量是源自歐洲,負載平衡器傾向將較多要求轉送至歐洲後端。因此,自動調度資源程序可能會啟動更多歐洲後端中的執行個體,藉此處理較大部分的要求。

從其他地區產生測試流量

假設亞洲現在現在是下午,這時您的網路服務也吸引了為數眾多網際網路使用者。以下步驟會根據這個假設,從亞洲產生大量要求。

控制台

  1. 如要建立另一個透過 Siege 負載測試工具安裝的執行個體,請按照下列步驟操作:

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往 VM 執行個體

    2. 點選「建立執行個體」

    3. 將「Name」(名稱) 設為 asia-loadtest

    4. 在「區域」部分,選取 asia-east1

    5. 展開「Advanced options」(進階選項) 區段。

    6. 展開「管理」部分。

    7. 在「Automation」(自動化) 專區中,輸入以下開機指令碼:

      apt -y install siege
      

    8. 按一下 [建立]。

  2. 如要取得負載平衡器的 IP 位址,請執行下列操作:

    1. 在 Google Cloud 控制台中,前往「Load balancing」(負載平衡) 頁面。

      前往「負載平衡」

    2. 按一下「fortressnet-balancer」。

    3. 在「前端」的「IP:Port」欄下方,複製 IPv4 位址 (IPv4 位址的格式應為 www.xxx.yyy.zzz)。

  3. 透過 SSH 登入用於測試負載的執行個體。

    1. 等待 asia-loadtest 執行個體在「Name」一欄下方顯示綠色勾號。
    2. 在「Connect」欄下方的 asia-loadtest 上,按一下「SSH」
  4. 啟動 Siege,並將負載平衡器的 IPv4 位址設為目標。

    siege -c150 http://LOAD_BALANCER_IP_ADDRESS
    

gcloud

  1. 在原有的殼層工作階段中,建立另一個透過 Siege 負載測試工具安裝的執行個體。

    gcloud compute instances create asia-loadtest \
        --network default \
        --zone asia-east1-c \
        --metadata startup-script='apt -y install siege'
    
  2. 取得負載平衡器的 IPv4 位址。

    gcloud compute addresses list
    
  3. 開啟新的殼層工作階段,您可以在其中使用 gcloud 指令。

    1. 在新的殼層工作階段中,透過 SSH 登入用於測試負載的執行個體。

      gcloud compute ssh --zone asia-east1-c asia-loadtest
      
    2. 啟動 Siege,並將負載平衡器的 IPv4 位址設為目標。

      siege -c150 http://LOAD_BALANCER_IP_ADDRESS
      

您應該會在輸出結果中看到以下宣告:The server is now under siege...

[alert] Zip encoding disabled; siege requires zlib support to enable it
** SIEGE 4.0.2
** Preparing 150 concurrent users for battle.
The server is now under siege...

監控負載平衡和自動調度資源

返回前文提到過的負載平衡監控畫面。系統最多可能需要 10 分鐘才能顯示足夠的新資料,您應該很快就會看到類似下圖的畫面:

Google Cloud 控制台的監控畫面,顯示系統如何將源自歐洲和亞洲的要求分配至三個後端

情況說明:

  1. 與先前的情況相似,負載測試瞬間開始傳送另一波龐大流量。起初,負載平衡器將要求平均分配至現有的三個後端。隨著要求數量超出您設定的自動調度資源限制,自動配置器開始依需求啟動額外的執行個體。

  2. 系統會透過自動調度資源滿足新的容量需求。負載平衡器同樣傾向將要求盡量轉送至最接近的可用後端。因此,亞洲後端會接收大部分源自亞洲的要求,歐洲後端會接收大部分源自歐洲的要求,而美國後端則會接收其餘的所有要求。

清除所用資源

完成教學課程後,您可以清除所建立的資源,這樣資源就不會占用配額並產生費用。下列各節將說明如何刪除或關閉這些資源。

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除執行個體

如要刪除 Compute Engine 執行個體:

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Select the checkbox for the instance that you want to delete.
  3. To delete the instance, click More actions, click Delete, and then follow the instructions.

後續步驟