設定內部直通式網路負載平衡器做為下一個躍點 (含標記)

本教學課程會透過範例說明如何部署內部直通式網路負載平衡器做為下一個躍點,讓封包沿著路徑轉送至最終目的地。您可以使用網路標記,設定路徑適用的特定用戶端執行個體。

本指南假設您熟悉內部直通式網路負載平衡器的運作方式、相關元件 (例如防火牆規則和健康狀態檢查),以及內部直通式網路負載平衡器如何做為下一個躍點,在路線上轉送封包。

您可以使用內部直通式網路負載平衡器做為下一個躍點功能,以高可用性和橫向擴充的方式整合第三方設備。如要這麼做,您必須設定自訂靜態路徑,並將下一個躍點設為負載平衡器,這樣負載平衡器就會將目的地前置字串的流量分配給已通過健康狀態檢查的第三方 VM 設備集區。您可以選擇下列幾種選項,為這些第三方應用程式提供高可用性:

  • 指定 IP 位址做為下一個躍點:使用與轉送規則相關聯的內部 IP 位址做為下一個躍點。這個負載平衡器的虛擬 IP 位址可透過同儕學習,而不需要透過同儕匯出自訂路徑。
  • 使用網路標記:您可以指定網路標記,讓內部直通式網路負載平衡器做為下一個躍點路徑,只套用至已使用標記設定的用戶端執行個體。這樣一來,您就能選取要填入哪些用戶端執行個體,以及要將流量路由至哪些標記的內部直通式網路負載平衡器。您不需要將不同的用戶端執行個體分隔至不同的 VPC,每個執行個體都會指向其偏好的內部轉送網路負載平衡器,前端連線至一組設備。標記的路徑不會透過 VPC 網路對等互連匯出或匯入。
  • 將多個路徑設為相同目的地前置字串:您可以使用標記,將多個路徑設為相同目的地,並使用不同的內部負載平衡器做為後續躍點。雖然系統不支援 ECMP (相同的目的地前置字串、相同的標記和不同的後續躍點),但您可以為這些相同的目的地路徑使用不同的標記或不同的優先順序。

設定總覽

使用單一 NIC 的 VM 在不同地區定義的代管執行個體群組,Linux 執行個體會設為將所有傳出流量透過 SNAT 轉譯至網際網路 (北向傳出流量流)。區域容錯移轉會手動觸發。本教學課程也示範使用內部直通式網路負載平衡器做為下一個躍點,以對稱雜湊法建立東西向連線。

本節中的步驟說明如何設定以下項目:

  1. 包含自訂子網路的範例虛擬私有雲網路
  2. 允許連入連線到後端 VM 的防火牆規則
  3. 部署 NAT 閘道的後端代管執行個體群組
  4. 用來測試連線的用戶端 VM
  5. 以下內部直通式網路負載平衡器元件:
    • 一個用於後端服務的健康狀態檢查
    • 內部後端服務
    • 一個用於負載平衡器前端的內部轉送規則和 IP 位址

這個範例的架構如下所示:

以內部直通式網路負載平衡器做為下一個躍點設定
內部直通式網路負載平衡器做為下一跳設定 (按一下可放大)

在您按照本教學課程的步驟操作時,請將 REGION_AREGION_B 替換為您要用於此範例的各個地區。

建立虛擬私有雲網路和子網路

  1. 建立名為 hub-vpc 的虛擬私有雲網路。

    gcloud compute networks create hub-vpc --subnet-mode custom
    
  2. REGION_Ahub-vpc 中建立子網路。

    gcloud compute networks subnets create hub-subnet-a \
        --network hub-vpc \
        --range 10.0.0.0/24 \
        --region REGION_A
    
  3. region Bhub-vpc 中建立子網路。

    gcloud compute networks subnets create hub-subnet-b \
        --network hub-vpc \
        --range 10.0.1.0/24 \
        --region REGION_B
    
  4. 建立名為 spoke1-vpc 的虛擬私有雲網路。

    gcloud compute networks create spoke1-vpc --subnet-mode custom
    
  5. spoke1-vpc 中建立子網路。

    gcloud compute networks subnets create spoke1-subnet1 \
        --network spoke1-vpc \
        --range 192.168.0.0/24 \
        --region REGION_A
    
  6. 建立名為 spoke2-vpc 的虛擬私有雲網路。

    gcloud compute networks create spoke2-vpc --subnet-mode custom
    
  7. spoke2-vpc 中建立子網路。

    gcloud compute networks subnets create spoke2-subnet1 \
        --network spoke2-vpc \
        --range 192.168.1.0/24 \
        --region REGION_A
    

設定防火牆規則

  1. 設定下列防火牆規則,允許 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/24
    
    gcloud 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/24
    
    gcloud 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
    
  2. 建立防火牆規則,允許健康狀態檢查探測器存取 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
    
  3. 建立防火牆規則,允許所有子網路上的執行個體使用 SSH 存取。如果您想使用 Identity-Aware Proxy 進行 TCP 轉送 (建議做法),請按照這些步驟啟用 SSH

    gcloud compute firewall-rules create hub-vpc-allow-ssh \
        --network hub-vpc \
        --allow tcp:22
    
    gcloud compute firewall-rules create spoke1-vpc-allow-ssh \
        --network spoke1-vpc \
        --allow tcp:22
    
    gcloud compute firewall-rules create spoke2-vpc-allow-ssh \
        --network spoke2-vpc \
        --allow tcp:22
    

設定虛擬私有雲網路對等互連

  1. 建立從 hub-vpcspoke1-vpc 的對等連線。

    gcloud compute networks peerings create hub-to-spoke1 \
        --network hub-vpc \
        --peer-network spoke1-vpc \
        --peer-project PROJECT_ID \
        --export-custom-routes
        
  2. 建立從 spoke1-vpchub-vpc 的對等連線。

    gcloud compute networks peerings create spoke1-to-hub \
        --network spoke1-vpc \
        --peer-network hub-vpc \
        --peer-project PROJECT_ID \
        --import-custom-routes
    
  3. 建立從 hub-vpcspoke2-vpc 的對等連線。

    gcloud compute networks peerings create hub-to-spoke2 \
        --network hub-vpc \
        --peer-network spoke2-vpc \
        --peer-project PROJECT_ID \
        --export-custom-routes
    
  4. 建立從 spoke2-vpchub-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 中建立代管執行個體群組後端。接著建立負載平衡資源和下一個躍點路徑。

建立代管執行個體群組

  1. 建立執行個體範本,以便在 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'
    
  2. 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 中建立負載平衡器。

  1. 建立健康狀態檢查。

    gcloud compute health-checks create http natgw-ilbnhop-health-check \
        --port=80
    
  2. 建立後端服務。

    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
    
  3. 將代管執行個體群組新增為後端。

    gcloud compute backend-services add-backend hub-natgw-region-a-be \
        --instance-group=hub-natgw-region-a-mig \
        --instance-group-region=REGION_A
    
  4. 建立轉寄規則。

    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

測試連線能力

建立用戶端執行個體來測試連線能力。

  1. 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'
    
  2. 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'
    

驗證南北和東西的流量

  1. 確認 NAT 閘道 VM 是否正在執行,並記下已指派的外部 IP 位址:

    gcloud compute instances list --filter="status:RUNNING AND name~natgw"
    
  2. 確認負載平衡器是否正常運作,且路由是否如預期建立:

    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
    
  3. 請確認內部直通式網路負載平衡器做為下一個躍點路徑,已以預期的優先順序新增至輻射狀 VPC,並指定內部直通式網路負載平衡器的 IP 位址:

    gcloud compute routes list --filter="name~natgw"
    
  4. 前往 Google Cloud 控制台,並在不同分頁中建立與 NAT 閘道 VM 的 SSH 連線。

  5. 使用下列指令,在每個 SSH 工作階段中啟動 tcpdump

    sudo tcpdump -n net 192.168.0.0/16
    
  6. 前往 Google Cloud 主控台,並建立與 spoke1-client VM 的新 SSH 連線。接著,使用下列指令來 ping spoke2-client 內部 IP 位址

    ping SPOKE2_CLIENT_INTERNAL_IP
    
  7. 切換至 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,如下所示:

  8. 停止 NAT 閘道 VM 上的 tcpdump 輸出,並查看 iptables 統計資料:

    watch sudo iptables -t nat -nvL
    
  9. 切換回 spoke1-client VM,並多次執行下列指令。輸出內容會顯示用於連線至網站的公開來源 IP 位址。

    curl ifconfig.io
    

    您應該會看到兩個 NAT 閘道 VM 的 IP 位址顯示為來源 IP 位址。這表示內部直通式網路負載平衡器會根據預設親和性 (5 元組雜湊) 分配流量。

  10. 切換回 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 中建立代管執行個體群組後端。接著建立負載平衡資源和下一個躍點路徑。

建立代管執行個體群組

  1. 建立執行個體範本,以便在 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'
    
  2. 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 中建立負載平衡器。

  1. 建立後端服務。

    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
    
  2. 將代管執行個體群組新增為後端。

    gcloud compute backend-services add-backend hub-natgw-region-b-be \
        --instance-group=hub-natgw-region-b-mig \
        --instance-group-region=REGION_B
    
  3. 建立轉寄規則。

    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

驗證區域容錯移轉

  1. 確認 NAT 閘道 VM 是否正在執行,並記下已指派的外部 IP:

    gcloud compute instances list --filter="status:RUNNING AND name~natgw"
    
  2. 確認負載平衡器是否正常運作,以及路徑是否如預期建立:

    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
    
  3. 請確認內部直通式網路負載平衡器做為下一個躍點路徑,已以預期的優先順序新增至輻射狀 VPC,並指定內部直通式網路負載平衡器的 IP 位址:

    gcloud compute routes list --filter="name~natgw"
    
  4. 您現在可以刪除高優先順序路徑,並記錄發生的情況,藉此驗證區域復原功能。切換至 spoke1-client VM,然後執行下列指令,每秒傳送一個 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 位址,且停機時間可能會縮短,這表示區域備援成功。

清除所用資源

  1. 移除內部直通式網路負載平衡器做為下一個躍點的路徑:

    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
    
  2. 移除內部直通式網路負載平衡器資源和後端:

    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
    
  3. 刪除用戶端 VM:

    gcloud -q compute instances delete spoke1-client \
      --zone=ZONE_A
    
    gcloud -q compute instances delete spoke2-client \
      --zone=ZONE_A
    
  4. 刪除虛擬私有雲網路對等互連、防火牆規則、子網路和虛擬私有雲:

    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
    

後續步驟