本教學課程說明服務生產者如何建立使用內部直通式網路負載平衡器的簡單服務。根據預設,內部直通式網路負載平衡器只能從代管該負載平衡器的虛擬私有雲網路存取。
本教學課程的適用對象為雲端架構師、網路架構師、網路管理員和 IT 管理員。
目標
- 為服務生產者資源設定網路
- 為負載平衡器後端建立 VM 資源
- 設定負載平衡器元件
- 測試存取負載平衡器
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 建立或選取專案,用於服務生產者資源。
這個專案在後續教學課程中稱為
PRODUCER_PROJECT -
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Compute Engine > Compute Network Admin, Compute Engine > Compute Security Admin, Compute Engine > Compute Instance Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 選取所需專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。
- 按一下「選取角色」,然後搜尋角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下「Save」(儲存)。
-
設定網路
下列各節說明如何為服務建立網路、子網路和防火牆規則。
設定網路和子網路
如要為負載平衡器建立服務網路和子網路,請完成下列各節的步驟。
控制台
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
在「Name」(名稱) 中輸入
service-network。在「子網路建立模式」部分,選取「自訂」。
在「New subnet」(新的子網路) 區段中,輸入以下資訊。
- 在「Name」(名稱) 中輸入
service-subnet。 - 在「區域」部分,選取要使用的區域。
- 在「IP stack type」(IP 堆疊類型),選取「IPv4 (single-stack)」(IPv4 (單一堆疊))。
- 在「IPv4 range」(IPv4 範圍) 中,輸入
10.10.10.0/24。 - 按一下 [完成]。
- 在「Name」(名稱) 中輸入
點選「建立」。
gcloud
建立自訂模式虛擬私有雲網路:
gcloud compute networks create service-network --subnet-mode=custom
在
service-network網路中建立子網路。gcloud compute networks subnets create service-subnet \ --network=service-network \ --range=10.10.10.0/24 \ --region=REGION將
REGION替換為您要使用的區域。
設定防火牆規則
建立防火牆規則,允許下列流量:
fw-allow-subnet:允許來自10.10.10.0/24的流量存取10.10.10.0/24中的其他資源。fw-allow-ssh:允許來自0.0.0.0/0的 SSH 流量連線至具有allow-ssh網路標記的 VM。fw-allow-healthcheck:允許來自 Google Cloud健康狀態檢查系統的流量,抵達具有allow-healthcheck網路標記的 VM。
控制台
前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。
如要允許子網路流量,請按一下「建立防火牆規則」,並使用下列設定:
- 在「Name」(名稱) 中輸入
fw-allow-subnet。 - 在「Network」(網路) 中選取
service-network。 - 在「Priority」(優先順序) 中輸入
1000。 - 在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
- 在「Targets」(目標) 中選取 All instances in the network。
- 在「Source filter」(來源篩選器) 中選取「IPv4 ranges」(IPv4 範圍)。
- 在「Source IPv4 ranges」(來源 IPv4 範圍) 中輸入
10.10.10.0/24。 - 在「Protocols and ports」(通訊協定和通訊埠) 中選取「Allow all」(全部允許)。
- 在「Name」(名稱) 中輸入
按一下 [Create] (建立)。
如要允許連入 SSH 連線,請按一下「建立防火牆規則」,然後使用下列設定:
- 在「Name」(名稱) 中輸入
fw-allow-ssh。 - 在「Network」(網路) 中選取
service-network。 - 在「Priority」(優先順序) 中輸入
1000。 - 在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
- 在「Targets」(目標) 中選取 Specified target tags。
- 在「Target tags」(目標標記) 中輸入
allow-ssh。 - 在「Source filter」(來源篩選器) 中選取「IPv4 ranges」(IPv4 範圍)。
- 在「Source IPv4 ranges」(來源 IPv4 範圍) 中輸入
0.0.0.0/0。 - 在「Protocols and ports」(通訊協定和通訊埠) 中選取「Specified protocols and ports」(指定的通訊協定和通訊埠),然後勾選「TCP」核取方塊。在「Ports」(通訊埠) 部分,輸入
22。
- 在「Name」(名稱) 中輸入
按一下 [Create] (建立)。
如要允許 Google Cloud 健康狀態檢查,請按一下「建立防火牆規則」,並使用下列設定:
- 在「Name」(名稱) 中輸入
fw-allow-healthcheck。 - 在「Network」(網路) 中選取
service-network。 - 在「Priority」(優先順序) 中輸入
1000。 - 在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
- 在「Targets」(目標) 中選取 Specified target tags。
- 在「Target tags」(目標標記) 中輸入
allow-healthcheck。 - 在「Source filter」(來源篩選器) 中選取「IPv4 ranges」(IPv4 範圍)。
- 在「Source IPv4 ranges」(來源 IPv4 範圍) 中,輸入
130.211.0.0/22和35.191.0.0/16。 - 在「Protocols and ports」(通訊協定和通訊埠) 中選取「Allow all」(全部允許)。
- 在「Name」(名稱) 中輸入
按一下 [Create] (建立)。
gcloud
建立
fw-allow-subnet防火牆規則,允許從子網路內進行通訊:gcloud compute firewall-rules create fw-allow-subnet \ --network=service-network \ --action=allow \ --direction=ingress \ --source-ranges=10.10.10.0/24 \ --rules=tcp,udp,icmp
建立
fw-allow-ssh防火牆規則,允許與具有allow-ssh網路標記的 VM 建立 SSH 連線。gcloud compute firewall-rules create fw-allow-ssh \ --network=service-network \ --action=allow \ --direction=ingress \ --source-ranges=0.0.0.0/0 \ --target-tags=allow-ssh \ --rules=tcp:22
建立
fw-allow-healthcheck規則,允許 Google Cloud健康狀態檢查。gcloud compute firewall-rules create fw-allow-healthcheck \ --network=service-network \ --action=allow \ --direction=ingress \ --target-tags=allow-healthcheck \ --source-ranges=130.211.0.0/22,35.191.0.0/16 \ --rules=tcp,udp,icmp
為負載平衡器後端建立 VM 資源
建立 VM 和執行個體群組,以搭配負載平衡器使用。
建立後端 VM
您建立的服務會在兩個 VM 上執行,以提高可用性:vm-1 和 vm-2。這些 VM 位於同一可用區,但在實際工作環境中,建議使用多個可用區。
除了建立 VM 之外,按照下列步驟操作還可完成下列事項:
- 設定 VM,以使用您在防火牆規則中設定為目標的網路標記,以及稍後會用到的額外網路標記 (
allow-nat)。 安裝、設定及啟動 Apache 服務。
控制台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
在「Name」(名稱) 中輸入
vm-1。在「Region」(區域) 和「Zone」(可用區) 部分,選取區域和該區域內的可用區。
在導覽選單中,前往「OS and storage」(OS 和儲存空間)。
在「Operating system and storage」(作業系統和儲存空間) 窗格中,確認已為開機磁碟選取「Debian GNU/Linux 12 (bookworm)」。如要選取其他圖片,請按一下「變更」。
在導覽選單中,前往「網路」。
在「Network tags」(網路標記) 部分,輸入
allow-ssh、allow-healthcheck和allow-nat。在「網路介面」部分選取下列項目:
- 在「Network」(網路) 中選取
service-network。 - 在「Subnet」(子網路) 中,選取
service-subnet。 - 在「IP stack type」(IP 堆疊類型),選取「IPv4 (single-stack)」(IPv4 (單一堆疊))。
- 在「主要內部 IPv4 位址」部分,選取「臨時 (自動)」。
- 在「外部 IPv4 位址」部分,選取「臨時」。
- 在「Network」(網路) 中選取
在導覽選單中,前往「進階」。
在「Automation」(自動化) 專區的「Startup script」(開機指令碼) 欄位中,輸入以下指令碼。
#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completed點選「建立」。
重複上述步驟,在與
vm-1相同的區域和可用區中,建立另一個名為vm-2的 VM。
gcloud
執行下列指令,建立兩個 VM:
gcloud compute instances create vm-1 \ --zone=ZONE \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=allow-ssh,allow-healthcheck,allow-nat \ --subnet=service-subnet \ --metadata=startup-script='#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completed'gcloud compute instances create vm-2 \ --zone=ZONE \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=allow-ssh,allow-healthcheck,allow-nati \ --subnet=service-subnet \ --metadata=startup-script='#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completed'將
ZONE替換為您要使用的區域。
建立執行個體群組
如要在負載平衡器後端使用 VM,必須將 VM 新增至執行個體群組。
控制台
前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。
點選「建立執行個體群組」。
在導覽窗格中,按一下「New unmanaged instance group」(新增非代管的執行個體群組)。
在「Name」(名稱) 中輸入
ig-1。在「Location」(位置) 專區中,選取與 VM 相同的區域和可用區。
在「Network」(網路) 中選取
service-network。在「Subnetwork」(子網路) 中,選取
service-subnet。在「VM instances」(VM 執行個體) 區段中,將
vm-1和vm-2新增至執行個體群組。點選「建立」。
gcloud
建立非代管執行個體群組:
gcloud compute instance-groups unmanaged create ig-1 \ --zone=ZONE將
ZONE替換為包含 VM 的區域。將 VM 新增至執行個體群組:
gcloud compute instance-groups unmanaged add-instances ig-1 \ --zone=ZONE \ --instances=vm-1,vm-2
設定負載平衡器元件
設定所有內部直通式網路負載平衡器元件,先從健康狀態檢查和後端服務著手,然後再設定前端元件:
健康檢查。您使用 HTTP 健康狀態檢查,檢查 HTTP
200 OK狀態碼。後端服務。由於需要透過內部負載平衡器傳送 HTTP 流量,因此必須使用 TCP,而非 UDP。
轉送規則。您會建立單一內部轉送規則,
service-rule。內部 IP 位址。您可以在建立轉送規則時指定內部 IP 位址
10.10.10.99。
控制台
開始設定
前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。
- 點選「建立負載平衡器」。
- 在「負載平衡器類型」部分,選取「網路負載平衡器 (TCP/UDP/SSL)」,然後點選「下一步」。
- 在「直通或使用 Proxy」部分,選取「直通式負載平衡器」,然後點選「下一步」。
- 在「公開或內部」部分,選取「內部」,然後點選「下一步」。
- 按一下 [設定]。
基本設定
在「建立內部直通式網路負載平衡器」頁面中,輸入下列資訊:
- 在「Load balancer name」(負載平衡器名稱) 部分輸入
service-lb。 - 在「Region」(區域),選取與 VM 相同的區域。
- 在「Network」(網路) 中選取
service-network。
設定後端
- 按一下「後端設定」。
- 在「健康狀態檢查」清單中,按一下「建立健康狀態檢查」,然後輸入下列資訊:
- 在「Name」(名稱) 中輸入
hc-http-80。 - 在「Protocol」(通訊協定) 中選取
HTTP。 - 在「Port」(通訊埠) 欄中,輸入
80。 - 在「Proxy 通訊協定」部分,選取
NONE。 - 在「Request path」(要求路徑) 中輸入
/。
- 在「Name」(名稱) 中輸入
- 點選「建立」。
- 在「Backends」(後端) 的「New Backend」(新增後端) 區段中,選取「IPv4 (single-stack)」(IPv4 (單一堆疊))。
- 在「Instance group」(執行個體群組) 中,選取
ig-1執行個體群組,然後按一下「Done」(完成)。 - 繼續操作之前,請先驗證「Backend configuration」(後端設定) 旁是否有藍色勾號。
設定前端
- 按一下「前端設定」。
- 在「New Frontend IP and port」(新增的前端 IP 和通訊埠) 區段中,執行下列操作:
- 在「Name」(名稱) 中輸入
service-rule。 - 在「Subnetwork」(子網路) 中,選取
service-subnet。 - 在「內部 IP 用途」部分,選取「IP 位址」清單中的「建立 IP 位址」,然後輸入下列資訊:
- 在「Name」(名稱) 中輸入
service-rule-ip。 - 將「IP version」(IP 版本) 設為「IPv4」。
- 在「Static IP address」(靜態 IP 位址) 中,選取 Let me choose。
- 在「Custom IP address」(自訂 IP 位址) 中,輸入
10.10.10.99。
- 在「Name」(名稱) 中輸入
- 按一下「保留」。
- 在「Ports」(通訊埠) 部分,選取「Single」(單一),然後在「Port numbers」(通訊埠編號) 部分輸入
80。 - 按一下 [完成]。
- 繼續操作之前,請先驗證「Frontend configuration」(前端設定) 旁是否有藍色勾號。
- 在「Name」(名稱) 中輸入
檢查設定
- 按一下「檢查並完成」。
- 查看負載平衡器設定。
- 點選「建立」。
gcloud
建立新的區域性 HTTP 健康狀態檢查,以測試通訊埠 80 上連至 VM 的 HTTP 連線。
gcloud compute health-checks create http hc-http-80 \ --region=REGION \ --port=80將
REGION替換為後端 VM 所在的地區。針對 HTTP 流量建立後端服務:
gcloud compute backend-services create service-lb \ --load-balancing-scheme=internal \ --protocol=tcp \ --region=REGION \ --health-checks=hc-http-80 \ --health-checks-region=REGION將
REGION替換為後端 VM 所在的地區。將執行個體群組新增至後端服務:
gcloud compute backend-services add-backend service-lb \ --region=REGION \ --instance-group=ig-1 \ --instance-group-zone=ZONE更改下列內容:
REGION:與後端 VM 相同的區域。ZONE:與後端 VM 位於相同可用區。
為後端服務建立轉送規則:
gcloud compute forwarding-rules create service-rule \ --region=REGION \ --load-balancing-scheme=internal \ --network=service-network \ --subnet=service-subnet \ --address=10.10.10.99 \ --ip-protocol=TCP \ --ports=80 \ --backend-service=service-lb \ --backend-service-region=REGION將
REGION替換為後端 VM 所在的地區。
測試存取負載平衡器
如要測試負載平衡器是否正常運作,請建立測試 VM,並從該 VM 將要求傳送至負載平衡器。
建立用戶端 VM 以進行測試
在與後端 (伺服器) VM 相同的地區中建立用戶端 VM。
控制台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
在「Name」(名稱) 中輸入
producer-test。在「Region」(區域),選取與後端 VM 相同的區域。
在「Zone」(可用區),選取該地區中的可用區。
按一下「網路」,然後設定下列欄位:
- 在「Network tags」(網路標記) 部分,輸入
allow-ssh。 - 在「網路介面」中,選取下列項目:
- 在「Network」(網路) 中選取
service-network。 - 在「Subnet」(子網路) 中,選取
service-subnet。
- 在「Network」(網路) 中選取
- 在「Network tags」(網路標記) 部分,輸入
點選「建立」。
gcloud
gcloud compute instances create producer-test \
--zone=ZONE \
--image-family=debian-12 \
--image-project=debian-cloud \
--tags=allow-ssh \
--subnet=service-subnet
將 ZONE 替換為與後端 VM 位於相同區域的可用區。
測試連線能力
這項測試會從用戶端 VM 與負載平衡器聯絡。預期的行為是在負載平衡器的後端 VM 之間分配流量。
- 連線至用戶端 VM 執行個體。
將gcloud compute ssh producer-test --zone=ZONE
ZONE替換為用戶端 VM 的可用區。 - 使用
curl向負載平衡器發出網路要求,以和其 IP 位址聯絡。重複發出要求,系統便會顯示來自不同後端 VM 的回應。由於每個後端 VM 上的/var/www/html/index.html內容不同,因此產生回應的 VM 名稱會顯示在 HTML 回應的文字中。舉例來說,預期回覆看起來會像Page served from: vm-1和Page served from: vm-2。curl -s http://10.10.10.99