具備進階流量管理 (Envoy) 的外部 HTTPS 負載平衡器

1. 簡介

歡迎使用具備進階流量管理 (Envoy) 的外部 HTTP 負載平衡器程式碼研究室!

最新版 HTTP(S) 外部負載平衡器具備進階流量管理功能,除了提供現有的傳統版全域外部 HTTP(S) 負載平衡器的所有功能,我們也將陸續增加進階流量管理功能。其中有些功能屬於負載平衡器新加入,有些則是強化現有功能。這些功能的部分清單包括:

課程內容

  • 如何設定代管執行個體群組以及相關聯的虛擬私有雲和防火牆規則
  • 如何使用新負載平衡器的進階流量管理功能
  • 如何確認進階流量管理功能正常運作。

軟硬體需求

  • 基本網路與 HTTP 知識
  • 基本的 Unix/Linux 指令列知識

程式碼研究室拓撲與用途

bfc43fd0ca39047a.png

圖 1 - HTTP 負載平衡器轉送拓撲

在這個程式碼研究室中,您會設定三個代管執行個體群組,一個位於東部、西部和中部。您將建立全域外部 HTTPS 負載平衡器。負載平衡器會使用以 Envoy 為基礎的負載平衡器所支援的進階功能清單中的幾項功能。部署完成後,您會產生一些模擬負載,並驗證您設定的設定是否正常運作。

2. 設定和需求

自修環境設定

  1. 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

  • 「專案名稱」是這項專案參與者的顯示名稱。這是 Google API 不使用的字元字串,您可以隨時更新。
  • 所有 Google Cloud 專案的專案 ID 均不得重複,且設定後即無法變更。Cloud 控制台會自動產生一個不重複的字串。但通常是在乎它何在在大部分的程式碼研究室中,您必須參照專案 ID (通常稱為 PROJECT_ID),因此如果您不喜歡的話,請隨機產生一個,或者,您也可以自行嘗試看看是否可用。是「凍結」建立專案後
  • 還有第三個值,也就是部分 API 使用的專案編號。如要進一步瞭解這三個值,請參閱說明文件
  1. 接下來,您需要在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。執行這個程式碼研究室並不會產生任何費用,如果有的話。如要關閉資源,以免產生本教學課程結束後產生的費用,請按照任「清除所用資源」操作請參閱本程式碼研究室結尾處的操作說明。Google Cloud 的新使用者符合 $300 美元免費試用計畫的資格。

啟動 Cloud Shell

雖然 Google Cloud 可以從筆記型電腦遠端操作,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是一種在 Cloud 中執行的指令列環境。

在 GCP 控制台的右上方,按一下「Cloud Shell」圖示:

bce75f34b2c53987.png

佈建並連線至環境的作業只需幾分鐘的時間。完成後,您應該會看到類似下方的內容:

f6ef2b5f13479f3a.png

這部虛擬機器都裝載了您需要的所有開發工具。提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作,大幅提高網路效能和驗證能力。這個研究室中的所有工作都可以透過瀏覽器完成。

事前準備

在 Cloud Shell 中,確認您已設定專案 ID

gcloud config list project

gcloud config set project [YOUR-PROJECT-NAME]

PROJECT_ID=[YOUR-PROJECT-NAME]

echo:$PROJECT_ID

啟用 API

啟用所有必要服務

gcloud services enable compute.googleapis.com
gcloud services enable logging.googleapis.com
gcloud services enable monitoring.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: httplbs
SUBNET_MODE: AUTO
BGP_ROUTING_MODE: REGIONAL
IPV4_RANGE:
GATEWAY_IPV4:

建立虛擬私有雲防火牆規則

建立虛擬私有雲後,您現在要建立防火牆規則。系統會使用防火牆規則允許所有 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: httplb-allow-http-rule
NETWORK: httplbs
DIRECTION: INGRESS
PRIORITY: 700
ALLOW: tcp:80
DENY:
DISABLED: False

4. 設定代管執行個體群組

您必須設定代管執行個體群組,其中包含 HTTP 負載平衡器所用後端資源的模式。首先來建立執行個體範本,藉此定義要為每個區域建立的 VM 設定。接著,針對各個區域的後端,我們會建立參照執行個體範本的代管執行個體群組。

代管執行個體群組可以是「可用區」或「區域性」範圍。在這個研究室練習中,我們會建立三個區域性代管執行個體群組,一個位於 us-east1、一個位於 us-west1,另一個則位於 us-central1。

在這個部分中,您可以看到在建立執行個體時,系統會參照的預先建立的開機指令碼。這種開機指令碼會安裝並啟用網路伺服器功能,用來模擬網頁應用程式。歡迎探索這個腳本。

建立東、西和中央執行個體範本

首先,請建立 us-east-1 執行個體範本。

透過 Cloud Shell

gcloud compute instance-templates create us-east1-template \
   --region=us-east1 \
   --network=httplbs \
   --tags=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'

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/instanceTemplates/us-east1-template].
NAME: us-east1-template
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
CREATION_TIMESTAMP: 2021-11-11T11:02:37.511-08:00

下一步是建立 us-west-1 執行個體範本。

透過 Cloud Shell

gcloud compute instance-templates create us-west1-template \
   --region=us-west1 \
   --network=httplbs \
   --tags=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'

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/instanceTemplates/us-west1-template].
NAME: us-west1-template
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
CREATION_TIMESTAMP: 2021-11-11T11:03:08.577-08:00

下一步是建立 us-central-1 執行個體範本。

透過 Cloud Shell

gcloud compute instance-templates create us-central1-template \
   --region=us-central1 \
   --network=httplbs \
   --tags=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'

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/instanceTemplates/us-central1-template].
NAME: us-central1-template
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
CREATION_TIMESTAMP: 2021-11-11T11:03:44.179-08:00

您現在可以使用下列 gcloud 指令,確認是否成功建立執行個體範本:

透過 Cloud Shell

gcloud compute instance-templates list

輸出

NAME                  MACHINE_TYPE   PREEMPTIBLE  CREATION_TIMESTAMP
us-central1-template   n1-standard-1         2021-11-09T09:25:37.263-08:00
us-east1-template      n1-standard-1         2021-11-09T09:24:35.275-08:00
us-west1-template      n1-standard-1         2021-11-09T09:25:08.016-08:00

建立東部、西部和中央代管執行個體群組

現在,我們必須使用先前建立的執行個體範本,建立代管執行個體群組。

透過 Cloud Shell

gcloud compute instance-groups managed create us-east1-mig \
--base-instance-name=us-east1-mig \
--size=1 \
--template=us-east1-template \
--zone=us-east1-b 

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-b/instanceGroupManagers/us-east1-mig].
NAME: us-east1-mig
LOCATION: us-east1-b
SCOPE: zone
BASE_INSTANCE_NAME: us-east1-mig
SIZE: 0
TARGET_SIZE: 1
INSTANCE_TEMPLATE: us-east1-template
AUTOSCALED: no

透過 Cloud Shell

gcloud compute instance-groups managed create us-west1-mig \
--base-instance-name=us-west1-mig \
--size=1 \
--template=us-west1-template \
--zone=us-west1-a  

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-west1-a/instanceGroupManagers/us-west1-mig].
NAME: us-west1-mig
LOCATION: us-west1-a
SCOPE: zone
BASE_INSTANCE_NAME: us-west1-mig
SIZE: 0
TARGET_SIZE: 1
INSTANCE_TEMPLATE: us-west1-template
AUTOSCALED: no

透過 Cloud Shell

gcloud compute instance-groups managed create us-central1-mig \
--base-instance-name=us-central1-mig \
--size=1 \
--template=us-central1-template \
--zone=us-central1-a 

輸出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-central1-a/instanceGroupManagers/us-central1-mig].
NAME: us-central1-mig
LOCATION: us-central1-a
SCOPE: zone
BASE_INSTANCE_NAME: us-central1-mig
SIZE: 0
TARGET_SIZE: 1
INSTANCE_TEMPLATE: us-central1-template
AUTOSCALED: no

我們可以使用下列 gcloud 指令,確認是否成功建立執行個體群組:

透過 Cloud Shell

gcloud compute instance-groups list

輸出

NAME                  LOCATION      SCOPE   NETWORK         MANAGED INSTANCES
us-central1-mig       us-central1   zone    httplbs          Yes      1
us-west1-mig          us-west1      zone    httplbs          Yes      1
us-east1-mig          us-east1      zone    httplbs          Yes      1

驗證網路伺服器功能

每個執行個體皆設定成透過簡單的 PHP 指令碼執行 Apache 網路伺服器,該指令碼會轉譯:

a6ab2f8c3b5d5680.png

如要確保網路伺服器正常運作,請前往「Compute Engine」->VM 執行個體確認已根據執行個體群組定義建立新的執行個體 (例如 us-east1-mig-xxx)。

現在,請在瀏覽器中發出網路要求,確保網路伺服器正在執行 (可能需要一分鐘的時間)。在 Compute Engine 的「VM 執行個體」頁面中,選取執行個體群組建立的執行個體,然後按一下「外部 (公開) IP」。

或在瀏覽器中前往 http://<IP_Address>

5. 設定負載平衡器

建立健康狀態檢查

首先,我們必須建立基本的健康狀態檢查,以確保服務順利執行。我們將建立基本的健康狀態檢查,還有許多進階自訂選項。

透過 Cloud Shell

gcloud compute health-checks create http http-basic-check \
    --port 80

保留外部 IP 位址

在這個步驟中,您必須保留全域可用的靜態 IP 位址,稍後會附加至負載平衡器。

透過 Cloud Shell

gcloud compute addresses create lb-ipv4-2 \
    --ip-version=IPV4 \
    --global

請務必記下預留的 IP 位址。

gcloud compute addresses describe lb-ipv4-2 \
    --format="get(address)" \
    --global

建立後端服務

現在,我們必須為稍早建立的各個代管執行個體群組建立後端服務。一個代表東部、西部和中部區域。

為東部代管執行個體群組建立後端服務。

透過 Cloud Shell

gcloud beta compute backend-services create east-backend-service \
    --load-balancing-scheme=EXTERNAL_MANAGED \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=http-basic-check \
    --global

為 West 代管執行個體群組建立後端服務。

透過 Cloud Shell

gcloud beta compute backend-services create west-backend-service \
    --load-balancing-scheme=EXTERNAL_MANAGED \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=http-basic-check \
    --global

為中央代管執行個體群組建立後端服務。

透過 Cloud Shell

gcloud beta compute backend-services create central-backend-service \
    --load-balancing-scheme=EXTERNAL_MANAGED \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=http-basic-check \
    --global

將 MIG 新增至後端服務

現在,我們已為每個應用程式叢集建立個別的後端服務,現在必須將先前建立的代管執行個體群組,新增至每個後端服務。

將東部 MIG 新增至後端服務。

透過 Cloud Shell

gcloud beta compute backend-services add-backend east-backend-service \
    --balancing-mode='UTILIZATION' \
    --instance-group=us-east1-mig \
    --instance-group-zone=us-east1-b \
    --global

將 West MIG 新增至後端服務。

透過 Cloud Shell

gcloud beta compute backend-services add-backend west-backend-service \
    --balancing-mode='UTILIZATION' \
    --instance-group=us-west1-mig \
    --instance-group-zone=us-west1-a \
    --global

將 West MIG 新增至後端服務。

透過 Cloud Shell

gcloud beta compute backend-services add-backend central-backend-service \
    --balancing-mode='UTILIZATION' \
    --instance-group=us-central1-mig \
    --instance-group-zone=us-central1-a \
    --global

建立網址對應

本研究室課程的進階流量管理功能就在網址對應中。我們必須建立含有設定的 .yaml 檔案。在 .yaml 檔案中,我們已在 /roundrobbin 建立前置字串比對項目,因此只有符合 /roundrobbin 的流量會受到這些設定影響。我們已指定將 50% 的流量傳送至 east-backend-service,而 50% 的流量應傳送至 west-backend-service。我們還新增了回應標頭值:{test},這個標頭值會顯示在所有回應中。最後,我們已將所有流量都應鏡射到 central-backend-service。系統會複製流量並傳送至此處,僅供測試之用。

將範例儲存為電腦上的 .yaml 檔案。

defaultService: https://www.googleapis.com/compute/v1/projects/[project_id]/global/backendServices/east-backend-service
kind: compute #urlMap
name: web-map-http
hostRules:
- hosts:
  - '*'
  pathMatcher: matcher1
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/[project_id]/global/backendServices/east-backend-service
  name: matcher1
  routeRules:
  - matchRules:
    - prefixMatch: /roundrobbin
    priority: 2
    headerAction:
        responseHeadersToAdd:
          - headerName: test
            headerValue: value
            replace: True
    routeAction:
        weightedBackendServices:
        - backendService: https://www.googleapis.com/compute/v1/projects/[project_id]/global/backendServices/east-backend-service
          weight: 50
        - backendService: https://www.googleapis.com/compute/v1/projects/[project_id]/global/backendServices/west-backend-service
          weight: 50
        retryPolicy:
            retryConditions: ['502', '504']
            numRetries: 3
            perTryTimeout:
                seconds: 1
                nanos: 50
        requestMirrorPolicy:
          backendService: https://www.googleapis.com/compute/v1/projects/[project_id]/global/backendServices/central-backend-service

建立從電腦匯入文件的網址對應。請注意,視 .yaml 檔案儲存位置而定,來源路徑會有所不同。

透過 Cloud Shell

gcloud beta compute url-maps import web-map-http \
   --source /Users/[USERNAME]/Documents/Codelab/lbconfig.yaml \
   --global

建立 HTTP 前端

建立負載平衡器的最後一個步驟是建立前端。這會將您先前保留的 IP 位址對應至您建立的負載平衡器網址對應。

透過 Cloud Shell

gcloud beta compute target-http-proxies create http-lb-proxy-adv \
    --url-map=web-map-http

接著,您必須建立通用轉送規則,將稍早保留的 IP 位址對應至 HTTP Proxy。

透過 Cloud Shell

gcloud beta 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

6. 確認進階流量功能運作正常

為驗證實作的流量拆分功能是否正常運作,您必須產生一些負載。為此,我們會建立新的 VM 來模擬負載。

建立允許 SSH 防火牆規則

為了透過 SSH 登入由我們產生流量的 VM,您必須先建立允許 SSH 流量到 VM 的防火牆規則。

透過 Cloud Shell

gcloud compute firewall-rules create fw-allow-ssh \
    --network=httplbs \
    --action=allow \
    --direction=ingress \
    --target-tags=allow-ssh \
    --rules=tcp:22

輸出

NAME          NETWORK  DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
fw-allow-ssh  httplbs  INGRESS    1000      tcp:22        False

建立 Siege-vm

現在您將建立 siege-vm 以產生負載

透過 Cloud Shell

gcloud compute instances create siege-vm \
    --network=httplbs \
    --zone=us-east4-c \
    --machine-type=e2-medium \
    --tags=allow-ssh,http-server \
    --metadata=startup-script='sudo apt-get -y install siege'

輸出

NAME     ZONE        MACHINE_TYPE INTERNAL_IP  EXTERNAL_IP    STATUS
siege-vm us-east4-c  e2-medium    10.150.0.3   34.85.218.119  RUNNING

接下來,請透過 SSH 登入您建立的 VM。建立後,按一下「SSH」來啟動終端機並連線。

連線後,請執行下列指令來產生載入項目。使用您先前為外部 http 負載平衡器保留的 IP 位址。

透過 Cloud Shell

siege -c 250 http://$lb-ipv4-2/roundrobbin

輸出

New configuration template added to /home/cloudcurriculumdeveloper/.siege
Run siege -C to view the current settings in that file
[alert] Zip encoding disabled; siege requires zlib support to enable it: No such file or directory
** SIEGE 4.0.2
** Preparing 250 concurrent users for battle.
The server is now under siege...

檢查負載分配

現在 Siege 正在執行,檢查流量是否平均分配給東部和西部代管執行個體群組,您還可以檢查流量鏡像是否正常運作,以及流量是否傳送至中央代管執行個體群組。

在 Cloud 控制台的導覽選單中,依序點選「網路服務」>「網路服務」>「負載平衡按一下「後端」。點按下方螢幕截圖所示的進階選單。

b40b0a9f0373f907.png

前往「後端服務」分頁並選取 east-backend-service

6972d9d311ed2b5c.png

您可以看到這個 MIG 的流量即時拆分。請注意這項比率,您可以立即將其與 west-backend-service 進行比較。

b1301b304c461069.png

同樣地,請前往 west-backend-service。您應該也會看到流量流向這項服務。這個比率應與您在 east-backend-service 中看到的比率類似,因為您設定了 50/50 循環的流量分配。

1674c04b73ea2e00.png

如要檢查您建立的流量鏡像政策是否正常運作,請檢查 central-backend-service 代管執行個體群組的使用率。方法是前往「Compute」、「Compute Engine」、「執行個體群組」,然後選取「us-central1-mig」。接著,前往「Monitoring」分頁。

4cf569efb3ba3c2b.png

您會看到已填入的圖表,證明流量已鏡像射到這個代管執行個體群組。

停止 Siege

您現在已證實進階流量拆分功能運作正常,現在該停止評估了。方法是返回 siege-vm 的 SSH 終端機,然後按下 CTRL+C 來停止執行斷層。

驗證已傳送的回應標頭

在清除所用資源之前,您可以快速驗證 HTTP 負載平衡器是否傳送了適當的回應標頭。您已設定傳送含有內容值的標頭測試。透過 Cloud Shell 執行 curl 指令,即可取得預期的回應。

透過 Cloud Shell

curl -svo /dev/null http://lb-ipv4-2/roundrobbin

輸出

*   Trying lb-ipv4-2..
* TCP_NODELAY set
* Connected to  lb-ipv4-2 ( lb-ipv4-2) port 80 (#0)
> GET /roundrobbin HTTP/1.1
> Host:  lb-ipv4-2
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< date: Wed, 10 Nov 2021 17:05:27 GMT
< server: envoy
< Content-Length: 273
< content-type: text/html; charset=iso-8859-1
< via: 1.1 google
< test: value
<
{ [273 bytes data]
* Connection #0 to host 34.149.2.26 left intact
* Closing connection 0

7. 清理研究室

研究室環境現已結束,接下來該拆除。請執行下列指令,刪除測試環境。

透過 Cloud Shell

gcloud compute instances delete siege-vm

gcloud alpha compute forwarding-rules delete http-content-rule --global
gcloud alpha compute target-http-proxies delete http-lb-proxy-adv
gcloud alpha compute url-maps delete web-map-http
gcloud alpha compute backend-services delete east-backend-service --global
gcloud alpha compute backend-services delete west-backend-service --global
gcloud alpha compute backend-services delete central-backend-service --global

gcloud compute addresses delete lb-ipv4-2 --global
gcloud compute health-checks delete http-basic-check 

gcloud compute instance-groups managed delete us-east1-mig --zone us-east1-b
gcloud compute instance-groups managed delete us-west1-mig --zone us-west1-a
gcloud compute instance-groups managed delete us-central1-mig --zone us-central1-a

gcloud compute instance-templates delete "us-east1-template" 
gcloud compute instance-templates delete "us-west1-template" 
gcloud compute instance-templates delete "us-central1-template" 

gcloud compute firewall-rules delete httplb-allow-http-rule
gcloud compute firewall-rules delete fw-allow-ssh

gcloud compute networks delete httplbs 

8. 恭喜!

您已完成具備進階流量管理 (Envoy) 程式碼研究室的外部 HTTP 負載平衡器!

涵蓋內容

  • 如何設定代管執行個體群組以及相關聯的虛擬私有雲和防火牆規則
  • 如何使用新負載平衡器的進階流量管理功能
  • 如何確認進階流量管理功能正常運作。

後續步驟

  • 嘗試其他進階轉送功能,例如網址重寫、新增 CORS 標頭等等 ( 連結)