设置内部直通网络负载均衡器作为下一个跃点(具有标记)

本教程使用一个示例来展示如何将内部直通网络负载均衡器部署为下一个跃点,数据包沿通往最终目的地的路径转发到下一个跃点。使用网络标记来配置要应用路由的特定客户端实例。

本指南假定您熟悉内部直通网络负载均衡器的工作原理、相关组件(如防火墙规则和健康检查),以及如何使用内部直通网络负载均衡器作为下一个跃点转发路由上的数据包。

通过内部直通网络负载均衡器作为下一个跃点功能,您可以以高度可用的横向扩容方式集成第三方设备。为此,您需要配置自定义静态路由并将下一个跃点设置为负载均衡器,从而将目标前缀的流量分配到健康检查的第三方虚拟机设备池。在选择下一个跃点以支持这些第三方设备的高可用性时,您有多种选择:

  • 将 IP 地址指定为下一个跃点:使用与作为下一个跃点的转发规则关联的内部 IP 地址。此负载均衡器的虚拟 IP 地址可以在对等方之间获知,而无需通过对等方导出自定义路由。
  • 使用网络标记:您可以指定网络标记,以使作为下一个跃点路由的内部直通网络负载均衡器仅适用于已配置该标记的客户端实例。这样,您就可以选择使用哪个已标记为下一个跃点路由的内部直通网络负载均衡器来填充哪个客户端实例,以及将流量路由到哪组设备。您无需将不同的客户端实例隔离到单独的 VPC 中,每个客户端实例均指向将一组设备作为前端的首选内部直通网络负载均衡器。带标记的路由不会通过 VPC 网络对等互连导出或导入。
  • 配置多个指向同一目的地前缀的路由:使用标记,您可以指定多个指向同一目的地的路由,并将不同内部负载均衡器作为下一个跃点。虽然不支持 ECMP(相同的目的地前缀、相同的标记、不同的下一个跃点),但您可以为相同的目的地路由使用不同的标记或不同的优先级。

设置概览

使用具有单个 NIC 的虚拟机的代管式实例组在不同区域进行定义,其中 Linux 实例配置为通过 SNAT 将所有出站流量发送到互联网(南北出站流量)。区域故障切换是手动触发的。本教程还演示了使用内部直通网络负载均衡器作为下一个跃点的对称哈希东西连接。

此部分中的步骤介绍了如何配置以下内容:

  1. 具有自定义子网的示例 VPC 网络
  2. 允许后端虚拟机的传入连接的防火墙规则
  3. 部署 NAT 网关的后端代管式实例组
  4. 用于测试连接的客户端虚拟机
  5. 以下内部直通网络负载均衡器组件:
    • 后端服务健康检查
    • 内部后端服务
    • 负载均衡器前端的内部转发规则和 IP 地址

此示例的架构如下所示:

内部直通网络负载均衡器作为下一个跃点配置
内部直通式网络负载均衡器作为下一个跃点配置(点击可放大)

按照本教程中的步骤执行操作时,请将 REGION_AREGION_B 替换为要用于此示例的相应区域。

创建 VPC 网络和子网

  1. 创建一个名为 hub-vpc 的 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 的 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 的 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
    

配置 VPC 网络对等互连

  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 网关虚拟机和负载均衡资源

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 网关虚拟机正在运行,并记下已分配的外部 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. 验证将内部直通网络负载均衡器作为下一个跃点路由添加到具有预期优先级的 spoke VPC,并定位内部直通网络负载均衡器的 IP 地址:

    gcloud compute routes list --filter="name~natgw"
    
  4. 转到 Google Cloud 控制台并在各标签页中建立与 NAT 网关虚拟机的 SSH 连接。

  5. 使用以下命令在其中的每个 SSH 会话中启动 tcpdump

    sudo tcpdump -n net 192.168.0.0/16
    
  6. 转到 Google Cloud 控制台并建立与 spoke1-client 虚拟机的新 SSH 连接。然后,使用以下命令对 spoke2-client 内部 IP 地址执行 ping 操作。

    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
    

    您应该能够成功对客户端虚拟机执行 ping 操作,这一过程演示了以下内容:

  8. 停止 NAT 网关虚拟机上的 tcpdump 输出并观察 iptables 统计信息:

    watch sudo iptables -t nat -nvL
    
  9. 切换回 spoke1-client 虚拟机并多次运行以下命令。输出会显示用于连接到网站的公共来源 IP 地址。

    curl ifconfig.io
    

    您应该会看到两个 NAT 网关虚拟机的 IP 地址显示为来源 IP 地址。这表明内部直通网络负载均衡器根据默认亲和性(5 元组哈希)分配流量。

  10. 切换回 NAT 网关虚拟机以确认数据包计数器已增加。

    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 网关虚拟机和负载均衡资源

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 网关虚拟机正在运行,并记下已分配的外部 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. 验证将内部直通网络负载均衡器作为下一个跃点路由添加到具有预期优先级的 spoke VPC,并定位内部直通网络负载均衡器的 IP 地址:

    gcloud compute routes list --filter="name~natgw"
    
  4. 现在,您可以通过删除高优先级路由并记下发生的情况来验证区域故障切换。切换到 spoke1-client 虚拟机并运行以下命令以每秒发送 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 网关虚拟机的外部 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. 删除客户端虚拟机:

    gcloud -q compute instances delete spoke1-client \
      --zone=ZONE_A
    
    gcloud -q compute instances delete spoke2-client \
      --zone=ZONE_A
    
  4. 删除 VPC 网络对等互连、防火墙规则、子网和 VPC:

    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
    

后续步骤