使用外部 HTTP(s) 混合型負載平衡器連上網路端點群組

1. 簡介

混合策略是務實的解決方案,可協助您因應不斷變化的市場需求,並逐步將應用程式現代化。Google Cloud 外部和內部 HTTP(S) 負載平衡器支援混合式環境,可將雲端負載平衡功能擴展至地端和其他雲端的後端,是混合式策略的關鍵推手。這可能是暫時性的,目的是為了遷移至新式雲端解決方案,也可能是貴機構 IT 基礎架構的永久配置。

3312e69c63b02f73.png

在本實驗室中,您將瞭解如何使用可從外部 HTTP (s) 全域負載平衡器存取的兩部虛擬機器,建立網路端點群組(NEG)。雖然實驗室中的 NEG 位於 GCP 內,但與可透過 IP 位址連線的公開或地端資源通訊時,會使用相同程序。

課程內容

  • 建立自訂 VPC
  • 建立兩個虛擬機器 (VM),做為網路端點群組 (NEG)
  • 建立混合型負載平衡器、後端服務和相關聯的健康狀態檢查
  • 建立防火牆規則,允許存取負載平衡器
  • 系統會建立 Cloud Router 和 NAT,允許從網際網路更新套件
  • 驗證網路端點群組的可連線性

軟硬體需求

  • 負載平衡器相關知識

自修實驗室環境設定

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

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

  • 專案名稱是這個專案的個人識別碼。只要遵守命名慣例,您可以使用任何名稱,並隨時更新。
  • 專案 ID 在所有 Google Cloud 專案中不得重複,且一經設定即無法變更。Cloud 控制台會自動產生專屬字串,通常您不需要在意該字串為何。在大多數程式碼研究室中,您需要參照專案 ID (通常會標示為 PROJECT_ID),因此如果您不喜歡該字串,可以產生另一個隨機字串,或嘗試使用自己的字串,看看是否可用。專案建立後,系統就會「凍結」該值。
  1. 接著,您必須在 Cloud 控制台中啟用帳單,才能使用 Google Cloud 資源。

完成本程式碼研究室的費用應該不高,甚至完全免費。請務必按照「清除」部分的指示操作,瞭解如何停用資源,避免在本教學課程結束後繼續產生帳單費用。Google Cloud 新使用者可參加價值$300 美元的免費試用計畫。

啟動 Cloud Shell

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

在 GCP 主控台,按一下右上角工具列的 Cloud Shell 圖示:

bce75f34b2c53987.png

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到如下的內容:

f6ef2b5f13479f3a.png

這部虛擬機器搭載各種您需要的開發工具,並提供永久的 5GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本實驗室的所有工作都可在瀏覽器上完成。

2. 事前準備

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

gcloud config list project
gcloud config set project [YOUR-PROJECT-ID]

Perform setting your projectID:
projectid=YOUR-PROJECT-ID
echo $projectid

3. 建立新的自訂模式虛擬私有雲網路

在這項工作中,您將建立虛擬私有雲 (VPC),做為網路的基礎。

虛擬私有雲網路

透過 Cloud Shell

gcloud compute networks create hybrid-network-lb --subnet-mode custom

建立子網路

透過 Cloud Shell

gcloud compute networks subnets create network-endpoint-group-subnet --network hybrid-network-lb --range 192.168.10.0/24 --region us-west1

建立 Cloud NAT 執行個體

雖然混合式網路並非必要條件,但運算執行個體必須連上網際網路,才能下載應用程式和更新。

在這項工作中,您會建立 Cloud Router 和 NAT 執行個體,允許 VM 執行個體連上網際網路。

建立 Cloud Router

透過 Cloud Shell

gcloud compute routers create crnat --network hybrid-network-lb --region us-west1

建立 Cloud NAT

透過 Cloud Shell

gcloud compute routers nats create cloudnat --router=crnat --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region us-west1

4. 建立兩個 VM 執行個體

在這項工作中,您將建立兩個執行 Apache 的 VM 執行個體,稍後在實驗室中,這些 VM 執行個體會成為網路端點群組 (NEG)。

透過 Cloud Shell 建立第一個地端執行個體 on-prem-neg-1

gcloud compute instances create on-prem-neg-1 \
    --zone=us-west1-a \
    --tags=allow-health-check \
    --image-family=debian-9 \
    --image-project=debian-cloud \
    --subnet=network-endpoint-group-subnet --no-address \
    --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)"
filter="{print \$NF}"
vm_zone="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/zone \
| awk -F/ "${filter}")"
echo "Page on $vm_hostname in $vm_zone" | \
tee /var/www/html/index.html
systemctl restart apache2'

透過 Cloud Shell 建立第一個地端執行個體 on-prem-neg-2

gcloud compute instances create on-prem-neg-2 \
    --zone=us-west1-a \
    --tags=allow-health-check \
    --image-family=debian-9 \
    --image-project=debian-cloud \
    --subnet=network-endpoint-group-subnet --no-address \
    --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)"
filter="{print \$NF}"
vm_zone="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/zone \
| awk -F/ "${filter}")"
echo "Page on $vm_hostname in $vm_zone" | \
tee /var/www/html/index.html
systemctl restart apache2'

5. 建立包含內部部署端點的 NEG

首先,建立名為 on-prem-neg-1 和 on-prem-neg-2 的 NEG。您也會指定負載平衡器應考量這些端點位於 us-west1-a GCP 區域,以利進行傳輸和負載平衡。建議您設定的區域與互連網路連結/VPN 閘道區域相關聯的任何區域相符,以利負載平衡器根據鄰近程度進行測量。

從 Cloud Shell 建立 on-prem-neg-1

gcloud compute network-endpoint-groups create on-prem-neg-1 \
    --network-endpoint-type NON_GCP_PRIVATE_IP_PORT \
    --zone "us-west1-a" \
    --network hybrid-network-lb

從 Cloud Shell 建立 on-prem-neg-2

gcloud compute network-endpoint-groups create on-prem-neg-2 \
    --network-endpoint-type NON_GCP_PRIVATE_IP_PORT \
    --zone "us-west1-a" \
    --network hybrid-network-lb

在本程式碼研究室中,網路端點群組是在 GCP 中執行 Apache 的 GCE 執行個體。或者,您也可以將內部部署或網際網路端點指定為網路端點

在 Cloud Shell 中找出 GCE IP 位址

gcloud compute instances list | grep -i on-prem

將網路端點群組與先前在上一步驟中識別的 GCE 執行個體 IP 位址建立關聯;針對每個 NEG,on-prem-neg-1 & on-prem-neg-2.

在 Cloud Shell 中,將 on-prem-neg-1 建立關聯,並將 x.x.x.x 更新為您識別的 IP

gcloud compute network-endpoint-groups update on-prem-neg-1 \
    --zone="us-west1-a" \
    --add-endpoint="ip=x.x.x.x,port=80"

在 Cloud Shell 中,從 on-prem-neg-2 建立關聯,並將 x.x.x.x 更新為您識別的 IP

gcloud compute network-endpoint-groups update on-prem-neg-2 \
    --zone="us-west1-a" \
    --add-endpoint="ip=x.x.x.x,port=80"

6. 建立 HTTP 健康狀態檢查、後端服務和防火牆

在這個步驟中,您會建立名為 on-prem-backend-service 的全域後端服務。這個後端服務會定義資料層將流量傳送至 NEG 的方式。

首先,請建立名為 on-prem-health-check 的健康狀態檢查,監控屬於這個 NEG 的任何端點 (也就是您的內部部署端點) 健康狀態。

透過 Cloud Shell

gcloud compute health-checks create http on-prem-health-check

建立名為 on-prem-backend-service 的後端服務,並與健康狀態檢查建立關聯。

透過 Cloud Shell

gcloud compute backend-services create on-prem-backend-service \
    --global \
    --load-balancing-scheme=EXTERNAL \
    --health-checks on-prem-health-check

HTTP(S) 外部負載平衡器和後端會從 35.191.0.0/16 和 130.211.0.0/22 子網路執行健康狀態檢查;因此,您必須建立防火牆規則,允許負載平衡器將流量轉送至後端。

透過 Cloud Shell

gcloud compute firewall-rules create fw-allow-health-check \
    --network=hybrid-network-lb \
    --action=allow \
    --direction=ingress \
    --source-ranges=130.211.0.0/22,35.191.0.0/16 \
    --target-tags=allow-health-check \
    --rules=tcp:80

7. 建立 NEG 與後端服務之間的關聯

將 on-prem-neg-1 NEG 新增至這個後端服務

透過 Cloud Shell

gcloud compute backend-services add-backend on-prem-backend-service \
    --global \
    --network-endpoint-group on-prem-neg-1 \
    --network-endpoint-group-zone us-west1-a \
    --balancing-mode RATE \
    --max-rate-per-endpoint 5

將 on-prem-neg-2 NEG 新增至這個後端服務

透過 Cloud Shell

gcloud compute backend-services add-backend on-prem-backend-service \
    --global \
    --network-endpoint-group on-prem-neg-2 \
    --network-endpoint-group-zone us-west1-a \
    --balancing-mode RATE \
    --max-rate-per-endpoint 5

保留用於存取網路端點的 IPv4 靜態 IP 位址

透過 Cloud Shell

gcloud compute addresses create hybrid-lb-ip --project=$projectid --global

CLI 設定完成後,接著在 Cloud 控制台完成設定。

8. 建立外部 HTTP 負載平衡器並關聯後端服務

前往 Cloud 控制台的「Load Balancing」(負載平衡),然後選取「Create load balancer」(建立負載平衡器)

找出 HTTP(S) 負載平衡,然後按一下「啟動設定」

70ccd168957e89d9.png

如下方螢幕截圖所示,選取「From Internet to my VMs」(從網際網路到我的 VM),允許公開存取 VM

a55cd31dbeadfecc.png

將負載平衡器命名為「xlb」,然後選取先前建立的後端服務「on-prem-backend-service」,最後按一下「確定」,如螢幕截圖所示

f1589df43bf9e3e8.png

選取前端設定,更新名稱「xlb-fe」,然後選取先前建立的靜態 IPv4 位址,請務必參照提供的螢幕截圖 b47cd48c7c1ccfc3.png

按照螢幕截圖選取「檢查並完成」,然後選取「建立」

bfa39f7dc3ad91e1.png

後端健康狀態驗證

在 Cloud 控制台中,確認後端「xlb」運作正常,如提供的螢幕截圖所示為綠色

131bbfc955d6166c.png

9. 驗證可從網際網路連上 NEG

回想一下,建立負載平衡器時使用的外部靜態 IP 位址,現在是網路端點的前端 IP。請先驗證 IP 位址,再執行最終測試。

透過 Cloud Shell

gcloud compute forwarding-rules describe xlb-fe --global | grep -i IPAddress:

輸出內容 (您的 IP 位址會有所不同)

Cloud Shell 的輸出內容

$ gcloud compute forwarding-rules describe xlb-fe --global | grep -i IPAddress:
IPAddress: 34.96.103.132

您可以使用全域負載平衡器前端 IP 位址存取網路端點後端。請注意,在本程式碼研究室中,端點是 GCE 執行個體,但您會將此端點與內部部署端點搭配使用。

本機工作站啟動終端機,並對負載平衡器 IP 位址執行 curl

從工作站對前端 IP 位址執行 curl。觀察 200 OK,以及包含 NEG 執行個體名稱和區域的頁面詳細資料。

myworkstation$ curl -v 34.96.103.132

* Trying 34.96.103.132...

* TCP_NODELAY set

* Connected to 34.96.103.132 (34.96.103.132) port 80 (#0)

> GET / HTTP/1.1

> Host: 34.96.103.132

> User-Agent: curl/7.64.1

> Accept: */*

>

< HTTP/1.1 200 OK

< Date: Tue, 10 Aug 2021 01:21:54 GMT

< Server: Apache/2.4.25 (Debian)

< Last-Modified: Tue, 10 Aug 2021 00:35:41 GMT

< ETag: "24-5c929ae7384f4"

< Accept-Ranges: bytes

< Content-Length: 36

< Content-Type: text/html

< Via: 1.1 google

<

Page on on-prem-neg-2 in us-west1-a

* Connection #0 to host 34.96.103.132 left intact

* Closing connection 0

恭喜!您已成功部署含有 NEG 的第 7 層混合式負載平衡器

恭喜您完成本程式碼研究室!

涵蓋內容

  • 建立自訂 VPC
  • 建立兩個虛擬機器 (VM),做為網路端點群組 (NEG)
  • 建立混合型負載平衡器、後端服務和相關聯的健康狀態檢查
  • 建立防火牆規則,允許存取負載平衡器
  • 驗證網路端點群組的可連線性

10. 清除步驟

在 Cloud 控制台 UI 中找出並勾選「xlb」負載平衡器,然後依序選取「網路服務」→「負載平衡」,即可刪除。選取後,勾選「on-premise-backend service」(內部部署後端服務) 和「on-premise-health-check」(內部部署健康狀態檢查),然後選取「delete」(刪除)

53d7463fe354fe66.png

在 Cloud 控制台 UI 中,依序前往「Compute Engine」→「Network Endpoint Groups」(網路端點群組)。選取後,勾選「on-prem-neg-1」和「on-prem-neg-2」,然後選取「刪除」

4d8f04264b44d03c.png

從 Cloud Shell 刪除實驗室元件

gcloud compute routers nats delete cloudnat --router=crnat --region us-west1 --quiet

gcloud compute routers delete crnat  --region us-west1 --quiet

gcloud compute instances delete on-prem-neg-1 --zone=us-west1-a --quiet

gcloud compute instances delete on-prem-neg-2 --zone=us-west1-a --quiet

gcloud compute firewall-rules delete fw-allow-health-check --quiet

gcloud compute networks subnets delete network-endpoint-group-subnet --region=us-west1 --quiet

gcloud compute networks delete hybrid-network-lb --quiet

gcloud compute addresses delete hybrid-lb-ip --global --quiet