配置加权负载均衡

本指南介绍如何使用区域后端服务为每个虚拟机 (VM) 实例创建加权外部直通网络负载均衡器部署。

在本教程中,您将创建一个包含三个虚拟机实例的实例组,并为每个实例分配权重。您可以创建 HTTP 健康检查以报告后端实例权重。在位置负载均衡器政策为 WEIGHTED_MAGLEV 的后端服务上启用加权负载均衡。

准备工作

创建 VPC 网络、子网和防火墙规则

创建 VPC 网络、子网和入站允许防火墙规则,以允许连接到负载均衡器的后端虚拟机。

  1. 创建 VPC 网络和子网:

    a. 要创建 VPC 网络,请运行 gcloud compute networks create 命令

    gcloud compute networks create NETWORK_NAME --subnet-mode custom
    

    b. 在此示例中,子网的主要 IPv4 地址范围为 10.10.0.0/24。 如需创建子网,请运行 gcloud compute networks subnets create 命令

    gcloud compute networks subnets create SUBNET_NAME \
      --network=NETWORK_NAME \
      --range=10.10.0.0/24 \
      --region=us-central1
    

    请替换以下内容:

    • NETWORK_NAME:要创建的 VPC 网络的名称。
    • SUBNET_NAME:要创建的子网的名称。
  2. 创建入站流量允许防火墙规则,以允许发送到目标 TCP 端口 80 和 443 的数据包传送到后端虚拟机。在此示例中,防火墙规则允许来自任何来源 IP 地址的连接。防火墙规则适用于网络标记为 network-lb-tag 的虚拟机。

    如需创建防火墙规则,请运行 gcloud compute firewall-rules create 命令

    gcloud compute firewall-rules create FIREWALL_RULE_NAME \
       --direction=INGRESS \
       --priority=1000 \
       --network=NETWORK_NAME \
       --action=ALLOW \
       --rules=tcp:80,tcp:443 \
       --source-ranges=0.0.0.0/0 \
       --target-tags=network-lb-tag
    

    FIREWALL_RULE_NAME 替换为要创建的防火墙规则的名称。

创建虚拟机实例并分配权重

创建三个虚拟机实例并分配权重:

  1. 配置三个后端虚拟机实例,以通过 HTTP 响应返回 X-Load-Balancing-Endpoint-Weight 标头中的权重。在本教程中,您配置一个后端实例报告权重为零,配置另一个后端实例报告权重 100,配置第三个后端实例报告权重 900。

    如需创建实例,请运行 gcloud compute instances create 命令

    gcloud compute instances create instance-0 \
      --zone=us-central1-a \
      --tags=network-lb-tag \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --subnet=SUBNET_NAME \
      --metadata=load-balancing-weight=0,startup-script='#! /bin/bash
      apt-get update
      apt-get install apache2 -y
      ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
      vm_hostname="$(curl -H "Metadata-Flavor:Google" \
      http://169.254.169.254/computeMetadata/v1/instance/name)"
      echo "Page served from: $vm_hostname" | \
      tee /var/www/html/index.html
      lb_weight="$(curl -H "Metadata-Flavor:Google" \
      http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)"
      echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \
      tee /etc/apache2/conf-enabled/headers.conf
      systemctl restart apache2'
    
    gcloud compute instances create instance-100 \
      --zone=us-central1-a \
      --tags=network-lb-tag \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --subnet=SUBNET_NAME \
      --metadata=load-balancing-weight=100,startup-script='#! /bin/bash
      apt-get update
      apt-get install apache2 -y
      ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
      vm_hostname="$(curl -H "Metadata-Flavor:Google" \
      http://169.254.169.254/computeMetadata/v1/instance/name)"
      echo "Page served from: $vm_hostname" | \
      tee /var/www/html/index.html
      lb_weight="$(curl -H "Metadata-Flavor:Google" \
      http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)"
      echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \
      tee /etc/apache2/conf-enabled/headers.conf
      systemctl restart apache2'
    
    gcloud compute instances create instance-900 \
      --zone=us-central1-a \
      --tags=network-lb-tag \
      --image-family=debian-10 \
      --image-project=debian-cloud \
      --subnet=SUBNET_NAME \
      --metadata=load-balancing-weight=900,startup-script='#! /bin/bash
        apt-get update
        apt-get install apache2 -y
        ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
        vm_hostname="$(curl -H "Metadata-Flavor:Google" \
        http://169.254.169.254/computeMetadata/v1/instance/name)"
        echo "Page served from: $vm_hostname" | \
        tee /var/www/html/index.html
        lb_weight="$(curl -H "Metadata-Flavor:Google" \
        http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)"
        echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \
        tee /etc/apache2/conf-enabled/headers.conf
        systemctl restart apache2'
    

创建实例组

在本教程中,您将说明如何创建包含所有三个虚拟机实例(instance-0instance-100instance-900)的非代管式实例组。

  • 如需创建实例组,请运行 gcloud compute instance-groups unmanaged create 命令

    gcloud compute instance-groups unmanaged create INSTANCE_GROUP \
      --zone=us-central1-a
    
    gcloud compute instance-groups unmanaged add-instances INSTANCE_GROUP \
      --zone=us-central1-a \
      --instances=instance-0,instance-100,instance-900
    

    INSTANCE_GROUP 替换为要创建的实例组的名称。

创建 HTTP 健康检查

在本教程中,您将提供创建 HTTP 健康检查以读取包含后端虚拟机权重的 HTTP 响应的说明。

  • 如需创建 HTTP 健康检查,请运行 gcloud compute health-checks create 命令

    gcloud compute health-checks create http HTTP_HEALTH_CHECK_NAME \
      --region=us-central1
    

    HTTP_HEALTH_CHECK_NAME 替换为要创建的 HTTP 健康检查的名称。

创建后端服务

以下示例提供了有关创建配置为使用加权负载均衡的区域外部后端服务的说明。

  1. 使用 HTTP 健康检查创建后端服务,并将位置负载均衡器政策设置为 WEIGHTED_MAGLEV

    • 如需创建后端服务,请运行 gcloud compute backend-services create 命令

      gcloud compute backend-services create BACKEND_SERVICE_NAME \
        --load-balancing-scheme=external \
        --protocol=tcp \
        --region=us-central1 \
        --health-checks=HTTP_HEALTH_CHECK_NAME \
        --health-checks-region=us-central1 \
        --locality-lb-policy=WEIGHTED_MAGLEV
      

      BACKEND_SERVICE_NAME 替换为要创建的后端服务的名称。

  2. 将实例组添加到后端服务。

  3. 为负载均衡器预留区域外部 IP 地址:

    • 如需预留一个或多个 IP 地址,请运行 gcloud compute addresses create 命令

      gcloud compute addresses create ADDRESS_NAME \
       --region us-central1
      

      ADDRESS_NAME 替换为要创建的 IP 地址的名称。

      使用 compute addresses describe 命令可以查看结果。请记下预留的静态外部 IP 地址 (IP_ADDRESS)。

      gcloud compute addresses describe ADDRESS_NAME
      
  4. 使用预留的区域级外部 IP 地址 IP_ADDRESS 创建转发规则。将转发规则连接到后端服务。

    • 如需创建转发规则,请运行 gcloud compute forwarding-rules create 命令

      gcloud compute forwarding-rules create FORWARDING_RULE \
        --region=us-central1 \
        --ports=80 \
        --address=IP_ADDRESS \
        --backend-service=BACKEND_SERVICE_NAME
      

      请替换以下内容:

      FORWARDING_RULE:要创建的转发规则的名称。

      IP_ADDRESS:分配给实例的 IP 地址。使用预留的静态外部 IP 地址,而不是地址名称。

使用后端服务 API 验证后端权重

验证已将后端权重正确报告给 HTTP 健康检查。

输出如下所示:

backend: https://www.googleapis.com/compute/projects/project-name/{project}/zones/us-central1-a/instanceGroups/{instance-group-name}
status:
  healthStatus:
  - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name}
    forwardingRuleIp: 34.135.46.66
    healthState: HEALTHY
    instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-0
    ipAddress: 10.10.0.5
    port: 80
    weight: '0'
  - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name}
    forwardingRuleIp: 34.135.46.66
    healthState: HEALTHY
    instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-100
    ipAddress: 10.10.0.6
    port: 80
    weight: '100'
  - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name}
    forwardingRuleIp: 34.135.46.66
    healthState: HEALTHY
    instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-900
    ipAddress: 10.10.0.7
    port: 80
    weight: '900'
  kind: compute#backendServiceGroupHealth