本教學課程會透過範例說明如何部署內部直通式網路負載平衡器做為下一個躍點,讓封包沿著路徑轉送至最終目的地。您可以使用網路標記,設定路徑適用的特定用戶端執行個體。
本指南假設您熟悉內部直通式網路負載平衡器的運作方式、相關元件 (例如防火牆規則和健康狀態檢查),以及內部直通式網路負載平衡器如何做為下一個躍點,在路線上轉送封包。
您可以使用內部直通式網路負載平衡器做為下一個躍點功能,以高可用性和橫向擴充的方式整合第三方設備。如要這麼做,您必須設定自訂靜態路徑,並將下一個躍點設為負載平衡器,這樣負載平衡器就會將目的地前置字串的流量分配給已通過健康狀態檢查的第三方 VM 設備集區。您可以選擇下列幾種選項,為這些第三方應用程式提供高可用性:
- 指定 IP 位址做為下一個躍點:使用與轉送規則相關聯的內部 IP 位址做為下一個躍點。這個負載平衡器的虛擬 IP 位址可透過同儕學習,而不需要透過同儕匯出自訂路徑。
- 使用網路標記:您可以指定網路標記,讓內部直通式網路負載平衡器做為下一個躍點路徑,只套用至已使用標記設定的用戶端執行個體。這樣一來,您就能選取要填入哪些用戶端執行個體,以及要將流量路由至哪些標記的內部直通式網路負載平衡器。您不需要將不同的用戶端執行個體分隔至不同的 VPC,每個執行個體都會指向其偏好的內部轉送網路負載平衡器,前端連線至一組設備。標記的路徑不會透過 VPC 網路對等互連匯出或匯入。
- 將多個路徑設為相同目的地前置字串:您可以使用標記,將多個路徑設為相同目的地,並使用不同的內部負載平衡器做為後續躍點。雖然系統不支援 ECMP (相同的目的地前置字串、相同的標記和不同的後續躍點),但您可以為這些相同的目的地路徑使用不同的標記或不同的優先順序。
設定總覽
使用單一 NIC 的 VM 在不同地區定義的代管執行個體群組,Linux 執行個體會設為將所有傳出流量透過 SNAT 轉譯至網際網路 (北向傳出流量流)。區域容錯移轉會手動觸發。本教學課程也示範使用內部直通式網路負載平衡器做為下一個躍點,以對稱雜湊法建立東西向連線。
本節中的步驟說明如何設定以下項目:
- 包含自訂子網路的範例虛擬私有雲網路
- 允許連入連線到後端 VM 的防火牆規則
- 部署 NAT 閘道的後端代管執行個體群組
- 用來測試連線的用戶端 VM
- 以下內部直通式網路負載平衡器元件:
- 一個用於後端服務的健康狀態檢查
- 內部後端服務
- 一個用於負載平衡器前端的內部轉送規則和 IP 位址
這個範例的架構如下所示:
在您按照本教學課程的步驟操作時,請將 REGION_A 和 REGION_B 替換為您要用於此範例的各個地區。
建立虛擬私有雲網路和子網路
建立名為
hub-vpc的虛擬私有雲網路。gcloud compute networks create hub-vpc --subnet-mode custom
在 REGION_A 的
hub-vpc中建立子網路。gcloud compute networks subnets create hub-subnet-a \ --network hub-vpc \ --range 10.0.0.0/24 \ --region REGION_A在 region B 的
hub-vpc中建立子網路。gcloud compute networks subnets create hub-subnet-b \ --network hub-vpc \ --range 10.0.1.0/24 \ --region REGION_B建立名為
spoke1-vpc的虛擬私有雲網路。gcloud compute networks create spoke1-vpc --subnet-mode custom
在
spoke1-vpc中建立子網路。gcloud compute networks subnets create spoke1-subnet1 \ --network spoke1-vpc \ --range 192.168.0.0/24 \ --region REGION_A建立名為
spoke2-vpc的虛擬私有雲網路。gcloud compute networks create spoke2-vpc --subnet-mode custom
在
spoke2-vpc中建立子網路。gcloud compute networks subnets create spoke2-subnet1 \ --network spoke2-vpc \ --range 192.168.1.0/24 \ --region REGION_A
設定防火牆規則
設定下列防火牆規則,允許 TCP、UDP 和 ICMP 流量從指定來源範圍傳送至執行個體。
gcloud compute firewall-rules create hub-vpc-web-ping-dns \ --network hub-vpc \ --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,10.0.1.0/24,192.168.0.0/24,192.168.1.0/24gcloud compute firewall-rules create spoke1-vpc-web-ping-dns \ --network spoke1-vpc \ --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,10.0.1.0/24,192.168.0.0/24,192.168.1.0/24gcloud compute firewall-rules create spoke2-vpc-web-ping-dns \ --network spoke2-vpc \ --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,10.0.1.0/24,192.168.0.0/24,192.168.1.0/24建立防火牆規則,允許健康狀態檢查探測器存取
hub-vpc上的執行個體。gcloud compute firewall-rules create hub-vpc-health-checks \ --network hub-vpc \ --allow tcp:80 \ --target-tags natgw \ --source-ranges 130.211.0.0/22,35.191.0.0/16建立防火牆規則,允許所有子網路上的執行個體使用 SSH 存取。如果您想使用 Identity-Aware Proxy 進行 TCP 轉送 (建議做法),請按照這些步驟啟用 SSH。
gcloud compute firewall-rules create hub-vpc-allow-ssh \ --network hub-vpc \ --allow tcp:22gcloud compute firewall-rules create spoke1-vpc-allow-ssh \ --network spoke1-vpc \ --allow tcp:22gcloud compute firewall-rules create spoke2-vpc-allow-ssh \ --network spoke2-vpc \ --allow tcp:22
設定虛擬私有雲網路對等互連
建立從
hub-vpc到spoke1-vpc的對等連線。gcloud compute networks peerings create hub-to-spoke1 \ --network hub-vpc \ --peer-network spoke1-vpc \ --peer-project PROJECT_ID \ --export-custom-routes建立從
spoke1-vpc到hub-vpc的對等連線。gcloud compute networks peerings create spoke1-to-hub \ --network spoke1-vpc \ --peer-network hub-vpc \ --peer-project PROJECT_ID \ --import-custom-routes建立從
hub-vpc到spoke2-vpc的對等連線。gcloud compute networks peerings create hub-to-spoke2 \ --network hub-vpc \ --peer-network spoke2-vpc \ --peer-project PROJECT_ID \ --export-custom-routes建立從
spoke2-vpc到hub-vpc的對等連線。gcloud compute networks peerings create spoke2-to-hub \ --network spoke2-vpc \ --peer-network hub-vpc \ --peer-project PROJECT_ID \ --import-custom-routes
在區域 A 中建立網路位址轉譯 (NAT) 閘道 VM 和負載平衡資源
在 REGION_A 中建立代管執行個體群組後端。接著建立負載平衡資源和下一個躍點路徑。
建立代管執行個體群組
建立執行個體範本,以便在 region A 中部署 NAT 閘道。
gcloud compute instance-templates create hub-natgw-region-a-template \ --network hub-vpc \ --subnet hub-subnet-a \ --region REGION_A \ --machine-type n1-standard-2 \ --can-ip-forward \ --tags natgw \ --metadata startup-script='#! /bin/bash # Enable IP forwarding: echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/20-iptables.conf # iptables configuration iptables -t nat -F sudo iptables -t nat -A POSTROUTING ! -d 192.168.0.0/16 -j MASQUERADE iptables-save # Use a web server to pass the health check for this example. # You should use a more complete test in production. apt-get update apt-get install apache2 tcpdump -y a2ensite default-ssl a2enmod ssl echo "Example web page to pass health check" | \ tee /var/www/html/index.html \ systemctl restart apache2'在 REGION_A 中建立執行個體群組。
gcloud compute instance-groups managed create hub-natgw-region-a-mig \ --region REGION_A \ --size=2 \ --template=hub-natgw-region-a-template
建立負載平衡器
請按照下列步驟在 REGION_A 中建立負載平衡器。
建立健康狀態檢查。
gcloud compute health-checks create http natgw-ilbnhop-health-check \ --port=80建立後端服務。
gcloud compute backend-services create hub-natgw-region-a-be \ --load-balancing-scheme=internal \ --protocol tcp \ --region REGION_A\ --health-checks=natgw-ilbnhop-health-check將代管執行個體群組新增為後端。
gcloud compute backend-services add-backend hub-natgw-region-a-be \ --instance-group=hub-natgw-region-a-mig \ --instance-group-region=REGION_A建立轉寄規則。
gcloud compute forwarding-rules create hub-natgw-region-a \ --load-balancing-scheme=internal \ --network=hub-vpc \ --subnet=hub-subnet-a \ --address=10.0.0.10 \ --ip-protocol=TCP \ --ports=all \ --allow-global-access \ --backend-service=hub-natgw-region-a-be \ --backend-service-region=REGION_A
建立下一個躍點路徑
使用預先定義的網路標記 ilbanh-region-a,建立內部直通式網路負載平衡器做為下一個躍點路徑。
gcloud compute routes create spoke1-natgw-region-a \
--network=spoke1-vpc \
--destination-range=0.0.0.0/0 \
--next-hop-ilb=10.0.0.10 \
--tags=ilbanh-region-a \
--priority 800
gcloud compute routes create spoke2-natgw-region-a \
--network=spoke2-vpc \
--destination-range=0.0.0.0/0 \
--next-hop-ilb=10.0.0.10 \
--tags=ilbanh-region-a \
--priority 800
測試連線能力
建立用戶端執行個體來測試連線能力。
在
spoke1-vpc中建立測試用戶端例項。gcloud compute instances create spoke1-client \ --subnet=spoke1-subnet1 --no-address --zone ZONE_A \ --tags=ilbanh-region-a \ --metadata startup-script='#! /bin/bash apt-get update apt-get install tcpdump -y'在
spoke2-vpc中建立測試用戶端例項。gcloud compute instances create spoke2-client \ --subnet=spoke2-subnet1 --no-address --zone ZONE_A \ --tags=ilbanh-region-a \ --metadata startup-script='#! /bin/bash apt-get update apt-get install tcpdump -y'
驗證南北和東西的流量
確認 NAT 閘道 VM 是否正在執行,並記下已指派的外部 IP 位址:
gcloud compute instances list --filter="status:RUNNING AND name~natgw"
確認負載平衡器是否正常運作,且路由是否如預期建立:
gcloud compute backend-services get-health hub-natgw-region-a-be --region REGION_A
backend: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-central1/instanceGroups/hub-natgw-region-a-mig status: healthStatus: - forwardingRule: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-central1/forwardingRules/hub-natgw-region-a forwardingRuleIp: 10.0.0.10 healthState: HEALTHY instance: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/us-central1-b/instances/<INSTANCE_NAME> ipAddress: 10.0.0.5 port: 80 - forwardingRule: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-central1/forwardingRules/hub-natgw-region-a forwardingRuleIp: 10.0.0.10 healthState: HEALTHY instance: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/us-central1-f/instances/<INSTANCE_NAME> ipAddress: 10.0.0.6 port: 80 kind: compute#backendServiceGroupHealth請確認內部直通式網路負載平衡器做為下一個躍點路徑,已以預期的優先順序新增至輻射狀 VPC,並指定內部直通式網路負載平衡器的 IP 位址:
gcloud compute routes list --filter="name~natgw"
前往 Google Cloud 控制台,並在不同分頁中建立與 NAT 閘道 VM 的 SSH 連線。
使用下列指令,在每個 SSH 工作階段中啟動
tcpdump:sudo tcpdump -n net 192.168.0.0/16
前往 Google Cloud 主控台,並建立與
spoke1-clientVM 的新 SSH 連線。接著,使用下列指令來 pingspoke2-client內部 IP 位址。ping SPOKE2_CLIENT_INTERNAL_IP
切換至 NAT 閘道 SSH 視窗,並確認您可以看到 ICMP 封包,如下所示:
16:51:28.411260 IP 192.168.0.2 > 192.168.1.2: ICMP echo request, id 1684, seq 492, length 64 16:51:28.411676 IP 192.168.1.2 > 192.168.0.2: ICMP echo reply, id 1684, seq 492, length 64
您應該可以成功對用戶端 VM 執行 Ping,如下所示:
- 東西向流量是透過 NAT 閘道啟用。請注意,系統不支援在輻射狀 VPC 之間建立轉介對等連線。
- 對稱雜湊 已啟用且運作正常,因為用戶端可以使用來源 IP 位址進行通訊,而不需要 SNAT 轉譯。
- 內部直通式網路負載平衡器可做為下一個躍點,支援所有通訊協定。
停止 NAT 閘道 VM 上的 tcpdump 輸出,並查看
iptables統計資料:watch sudo iptables -t nat -nvL
切換回
spoke1-clientVM,並多次執行下列指令。輸出內容會顯示用於連線至網站的公開來源 IP 位址。curl ifconfig.io
您應該會看到兩個 NAT 閘道 VM 的 IP 位址顯示為來源 IP 位址。這表示內部直通式網路負載平衡器會根據預設親和性 (5 元組雜湊) 分配流量。
切換回 NAT 閘道 VM,確認封包計數器已增加。
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 105 11442 MASQUERADE all -- * * 0.0.0.0/0 !192.168.0.0/16 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
在區域 B 中建立網路位址轉譯 (NAT) 閘道 VM 和負載平衡資源
在 region B 中建立代管執行個體群組後端。接著建立負載平衡資源和下一個躍點路徑。
建立代管執行個體群組
建立執行個體範本,以便在 region B 中部署 NAT 閘道。
gcloud compute instance-templates create hub-natgw-region-b-template \ --network hub-vpc \ --subnet hub-subnet-b --region REGION_B \ --machine-type n1-standard-2 --can-ip-forward \ --tags natgw \ --metadata startup-script='#! /bin/bash # Enable IP forwarding: echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/20-iptables.conf # iptables configuration iptables -t nat -F sudo iptables -t nat -A POSTROUTING ! -d 192.168.0.0/16 -j MASQUERADE iptables-save # Use a web server to pass the health check for this example. # You should use a more complete test in production. apt-get update apt-get install apache2 tcpdump -y a2ensite default-ssl a2enmod ssl echo "Example web page to pass health check" | \ tee /var/www/html/index.html \ systemctl restart apache2'在 region B 中建立執行個體群組。
gcloud compute instance-groups managed create hub-natgw-region-b-mig \ --region REGION_B \ --size=2 \ --template=hub-natgw-region-b-template
建立負載平衡器
請按照下列步驟在區域 B 中建立負載平衡器。
建立後端服務。
gcloud compute backend-services create hub-natgw-region-b-be \ --load-balancing-scheme=internal \ --protocol tcp \ --region REGION_B\ --health-checks=natgw-ilbnhop-health-check將代管執行個體群組新增為後端。
gcloud compute backend-services add-backend hub-natgw-region-b-be \ --instance-group=hub-natgw-region-b-mig \ --instance-group-region=REGION_B建立轉寄規則。
gcloud compute forwarding-rules create hub-natgw-region-b \ --load-balancing-scheme=internal \ --network=hub-vpc \ --subnet=hub-subnet-b \ --address=10.0.1.10 \ --ip-protocol=TCP \ --ports=all \ --allow-global-access \ --backend-service=hub-natgw-region-b-be \ --backend-service-region=REGION_B
建立下一個躍點路徑
使用預先定義的網路標記 ilbanh-region-a,建立內部直通式網路負載平衡器做為下一個躍點路徑。
gcloud compute routes create spoke1-natgw-region-b \
--network=spoke1-vpc \
--destination-range=0.0.0.0/0 \
--next-hop-ilb=10.0.1.10 \
--tags=ilbanh-region-a \
--priority 900
gcloud compute routes create spoke2-natgw-region-b \
--network=spoke2-vpc \
--destination-range=0.0.0.0/0 \
--next-hop-ilb=10.0.1.10 \
--tags=ilbanh-region-a \
--priority 900
驗證區域容錯移轉
確認 NAT 閘道 VM 是否正在執行,並記下已指派的外部 IP:
gcloud compute instances list --filter="status:RUNNING AND name~natgw"
確認負載平衡器是否正常運作,以及路徑是否如預期建立:
gcloud compute backend-services get-health hub-natgw-region-b-be --region REGION_B
backend: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-west2/instanceGroups/hub-natgw-region-b-mig status: healthStatus: - forwardingRule: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-west2/forwardingRules/hub-natgw-region-b forwardingRuleIp: 10.0.1.10 healthState: HEALTHY instance: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/us-west2-a/instances/<INSTANCE_NAME> ipAddress: 10.0.1.3 port: 80 - forwardingRule: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/regions/us-west2/forwardingRules/hub-natgw-region-b forwardingRuleIp: 10.0.1.10 healthState: HEALTHY instance: https://www.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/us-west2-b/instances/<INSTANCE_NAME> ipAddress: 10.0.1.2 port: 80 kind: compute#backendServiceGroupHealth請確認內部直通式網路負載平衡器做為下一個躍點路徑,已以預期的優先順序新增至輻射狀 VPC,並指定內部直通式網路負載平衡器的 IP 位址:
gcloud compute routes list --filter="name~natgw"
您現在可以刪除高優先順序路徑,並記錄發生的情況,藉此驗證區域復原功能。切換至
spoke1-clientVM,然後執行下列指令,每秒傳送一個 curl 要求。這個指令也會回報正在使用的外部 IP 位址:while true; do echo -n `date` && echo -n ' - ' && curl ifconfig.io --connect-timeout 1; done
region A 是高優先順序的路徑,因此只會顯示指派給 NAT 閘道的外部 IP 位址。讓
curl指令繼續執行,然後切換至 Cloud Shell,在 region A 中刪除內部直通式網路負載平衡器的路徑,以便驗證結果:gcloud -q compute routes delete spoke1-natgw-region-a
在 region B 中,系統會顯示指派給 NAT 閘道 VM 的外部 IP 位址,且停機時間可能會縮短,這表示區域備援成功。
清除所用資源
移除內部直通式網路負載平衡器做為下一個躍點的路徑:
gcloud -q compute routes delete spoke1-natgw-region-b gcloud -q compute routes delete spoke2-natgw-region-a gcloud -q compute routes delete spoke2-natgw-region-b
移除內部直通式網路負載平衡器資源和後端:
gcloud -q compute forwarding-rules delete hub-natgw-region-a \ --region REGION_A gcloud -q compute backend-services delete hub-natgw-region-a-be \ --region REGION_A gcloud -q compute instance-groups managed delete hub-natgw-region-a-mig \ --region REGION_A gcloud -q compute instance-templates delete hub-natgw-region-a-template gcloud -q compute forwarding-rules delete hub-natgw-region-b \ --region REGION_B gcloud -q compute backend-services delete hub-natgw-region-b-be \ --region REGION_B gcloud -q compute instance-groups managed delete hub-natgw-region-b-mig \ --region REGION_B gcloud -q compute instance-templates delete hub-natgw-region-b-template gcloud -q compute health-checks delete natgw-ilbnhop-health-check
刪除用戶端 VM:
gcloud -q compute instances delete spoke1-client \ --zone=ZONE_A gcloud -q compute instances delete spoke2-client \ --zone=ZONE_A
刪除虛擬私有雲網路對等互連、防火牆規則、子網路和虛擬私有雲:
gcloud -q compute networks peerings delete spoke2-to-hub \ --network spoke2-vpc gcloud -q compute networks peerings delete spoke1-to-hub \ --network spoke1-vpc gcloud -q compute networks peerings delete hub-to-spoke1 \ --network hub-vpc gcloud -q compute networks peerings delete hub-to-spoke2 \ --network hub-vpc gcloud -q compute firewall-rules delete spoke2-vpc-web-ping-dns gcloud -q compute firewall-rules delete spoke1-vpc-web-ping-dns gcloud -q compute firewall-rules delete hub-vpc-web-ping-dns gcloud -q compute firewall-rules delete hub-vpc-health-checks gcloud -q compute firewall-rules delete hub-vpc-allow-ssh gcloud -q compute firewall-rules delete spoke1-vpc-allow-ssh gcloud -q compute firewall-rules delete spoke2-vpc-allow-ssh gcloud -q compute networks subnets delete spoke1-subnet1 \ --region REGION_A gcloud -q compute networks subnets delete spoke2-subnet1 \ --region REGION_A gcloud -q compute networks subnets delete hub-subnet-a \ --region REGION_A gcloud -q compute networks subnets delete hub-subnet-b \ --region REGION_B gcloud -q compute networks delete spoke1-vpc gcloud -q compute networks delete spoke2-vpc gcloud -q compute networks delete hub-vpc
後續步驟
- 如需容錯移轉的重要資訊,請參閱內部直通式網路負載平衡器的容錯移轉概念一文。
- 如要瞭解如何設定內部直通式網路負載平衡器的記錄和監控,請參閱「內部直通式網路負載平衡器記錄和監控」一文。
- 關於如何透過連線至虛擬私人雲端網路的對等互連網路存取內部直通式網路負載平衡器,請參閱「內部直通式網路負載平衡器和已連線網路」一文。
- 如要瞭解如何排解內部直通式網路負載平衡器的問題,請參閱「排解內部直通式網路負載平衡器問題」。