“高级负载均衡优化”Codelab

1. 简介

欢迎学习高级负载均衡优化 Codelab!

在此 Codelab 中,您将学习如何为全球外部应用负载平衡器配置高级负载均衡选项。在开始之前,建议您先查看有关 Cloud Load Balancing 的文档 ( https://cloud.google.com/load-balancing/docs/load-balancing-overview)

c3fb1d3f027e8640.png

图 1. 使用全球外部应用负载平衡器选择目标终点的工作流程。

Codelab 拓扑和用例

2f7368df335d3de9.png

图 2. HTTP 负载平衡器路由拓扑

在本代码实验中,您将设置两个托管式实例组。您将创建一个全球外部 HTTPS 负载平衡器。负载平衡器将利用基于 Envoy 的负载平衡器支持的多种高级功能。部署完成后,您将生成一些模拟负载,并验证您设置的配置是否正常运行。

学习内容

  • 如何配置 ServiceLbPolicy 以微调负载平衡器。

所需条件

2. 准备工作

在 Cloud Shell 中,确保项目 ID 已设置

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
prodproject=YOUR-PROJECT-NAME
echo $prodproject

启用 API

启用所有必要的服务

gcloud services enable compute.googleapis.com
gcloud services enable logging.googleapis.com
gcloud services enable monitoring.googleapis.com
gcloud services enable networkservices.googleapis.com

3. 创建 VPC 网络

创建 VPC 网络

从 Cloud Shell

gcloud compute networks create httplbs --subnet-mode=auto

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/httplbs].
NAME     SUBNET_MODE  BGP_ROUTING_MODE  IPV4_RANGE  GATEWAY_IPV4
httplbs  AUTO         REGIONAL

创建 VPC 防火墙规则

创建 VPC 后,您现在将创建防火墙规则。防火墙规则将用于允许所有 IP 通过端口 80 访问测试应用网站的外部 IP,以实现 HTTP 流量。

从 Cloud Shell

gcloud compute firewall-rules create httplb-allow-http-rule \
--allow tcp:80 \
--network httplbs \
--source-ranges 0.0.0.0/0 \
--priority 700

输出

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/httplb-allow-http-rule].
Creating firewall...done.
NAME                    NETWORK  DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
httplb-allow-http-rule  httplbs  INGRESS    700       tcp:80        False

在此 Codelab 中,我们将调整虚拟机的健康状况。因此,我们还将创建防火墙规则以允许 SSH。

从 Cloud Shell

gcloud compute firewall-rules create fw-allow-ssh \
    --network=httplbs \
    --action=allow \
    --direction=ingress \
    --target-tags=allow-ssh \
    --rules=tcp:22

输出

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/fw-allow-ssh].
Creating firewall...done.
NAME          NETWORK  DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
fw-allow-ssh  httplbs  INGRESS    1000      tcp:22        False

4. 设置代管式实例组

您需要设置代管式实例组,其中包含 HTTP 负载平衡器使用的后端资源的模式。首先,我们将创建实例模板,用于定义为每个区域创建的虚拟机的配置。接下来,我们将为每个区域中的后端创建一个引用实例模板的托管式实例组。

代管式实例组的范围可以是可用区级,也可以是区域级。在本实验练习中,我们将创建地区托管式实例组。

在本部分中,您将看到一个预先创建的启动脚本,该脚本将在创建实例时被引用。此启动脚本会安装并启用 Web 服务器功能,我们将使用这些功能来模拟 Web 应用。您可以随时查看此脚本。

创建实例模板

第一步是创建实例模板。

从 Cloud Shell

gcloud compute instance-templates create test-template \
   --network=httplbs \
   --tags=allow-ssh,http-server \
   --image-family=debian-9 \
   --image-project=debian-cloud \
   --metadata=startup-script='#! /bin/bash
     apt-get update
     apt-get install apache2 -y
     a2ensite default-ssl
     a2enmod ssl
     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
     systemctl restart apache2'

输出

NAME           MACHINE_TYPE   PREEMPTIBLE  CREATION_TIMESTAMP
test-template  n1-standard-1               2021-11-09T09:24:35.275-08:00

现在,您可以使用以下 gcloud 命令验证实例模板是否已成功创建:

从 Cloud Shell

gcloud compute instance-templates list

输出

NAME                  MACHINE_TYPE   PREEMPTIBLE  CREATION_TIMESTAMP
test-template         n1-standard-1         2021-11-09T09:24:35.275-08:00

创建实例组

现在,我们必须根据之前创建的实例模板创建托管式实例组。

从 Cloud Shell

gcloud compute instance-groups managed create us-east1-a-mig \
--size=1 \
--template=test-template \
--zone=us-east1-a

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-a/instanceGroupManagers/us-east1-a-mig].
NAME            LOCATION    SCOPE  BASE_INSTANCE_NAME   SIZE  TARGET_SIZE  INSTANCE_TEMPLATE  AUTOSCALED
us-east1-a-mig  us-east1-a  zone   us-east1-a-mig       0     1            test-template      no

从 Cloud Shell

gcloud compute instance-groups managed create us-east1-b-mig \
--size=5 \
--template=test-template \
--zone=us-east1-b

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-b/instanceGroupManagers/us-east1-b-mig].
NAME            LOCATION    SCOPE  BASE_INSTANCE_NAME   SIZE  TARGET_SIZE  INSTANCE_TEMPLATE  AUTOSCALED
us-east1-b-mig  us-east1-b  zone   us-east1-b-mig       0     5            test-template      no

我们可以使用以下 gcloud 命令验证实例组是否已成功创建:

从 Cloud Shell

gcloud compute instance-groups list

输出

NAME                  LOCATION      SCOPE   NETWORK         MANAGED INSTANCES
us-east1-a-mig        us-east1-a    zone    httplbs          Yes      1
us-east1-b-mig        us-east1-b    zone    httplbs          Yes      5

验证 Web 服务器功能

每个实例都配置为运行一个 Apache Web 服务器,其中包含一个简单的 PHP 脚本,用于呈现如下内容:

网页提供自:us-east1-a-mig-ww2h

为确保 Web 服务器正常运行,请依次前往“Compute Engine”>“虚拟机实例”。确保已根据新实例组定义创建新实例(例如 us-east1-a-mig-xxx)。

现在,在浏览器中向其发出 Web 请求,以确保 Web 服务器正在运行(这可能需要一分钟才能启动)。在 Compute Engine 下的“虚拟机实例”页面上,选择由实例组创建的实例,然后点击其外部(公共)IP。

或者,在浏览器中前往 http://<IP_Address>

5. 设置负载平衡器

创建健康检查

首先,我们必须创建基本健康检查,以确保服务正常运行。我们将创建一个基本健康检查,但还有许多更高级的自定义设置可供使用。

从 Cloud Shell

gcloud compute health-checks create http http-basic-check \
    --port 80

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/healthChecks/http-basic-check].
NAME              PROTOCOL
http-basic-check  HTTP

预留外部 IP 地址

在此步骤中,您需要预留一个可供全球使用的静态 IP 地址,该地址稍后将附加到负载平衡器。

从 Cloud Shell

gcloud compute addresses create lb-ipv4-2 \
    --ip-version=IPV4 \
    --global

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/addresses/lb-ipv4-2].

请务必记下预留的 IP 地址。

gcloud compute addresses describe lb-ipv4-2 \
    --format="get(address)" \
    --global

创建后端服务

现在,我们必须为之前创建的代管式实例组创建后端服务。

从 Cloud Shell

gcloud compute backend-services create east-backend-service \
    --load-balancing-scheme=EXTERNAL_MANAGED \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=http-basic-check \
    --global

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/east-backend-service].
NAME                  BACKENDS  PROTOCOL
east-backend-service            HTTP

将 MIG 添加到后端服务

现在我们已经创建了后端服务,接下来必须将之前创建的托管式实例组添加到每个后端服务。

从 Cloud Shell

gcloud compute backend-services add-backend east-backend-service --instance-group us-east1-a-mig --instance-group-zone us-east1-a --global

从 Cloud Shell

gcloud compute backend-services add-backend east-backend-service --instance-group us-east1-b-mig --instance-group-zone us-east1-b --global

您可以通过运行以下命令来验证后端是否已添加。

从 Cloud Shell

gcloud compute backend-services list

输出

NAME                  BACKENDS                                                                                               PROTOCOL
east-backend-service  us-east1-a/instanceGroups/us-east1-a-mig,us-east1-b/instanceGroups/us-east1-b-mig  HTTP

创建网址映射

现在,我们将创建一个网址映射。

gcloud compute url-maps create web-map-http \
    --default-service=east-backend-service \
    --global

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/web-map-http].
NAME          DEFAULT_SERVICE
web-map-http  backendServices/east-backend-service

创建 HTTP 前端

创建负载平衡器的最后一步是创建前端。这会将您之前预留的 IP 地址映射到您创建的负载平衡器网址映射。

从 Cloud Shell

gcloud compute target-http-proxies create http-lb-proxy-adv \
    --url-map=web-map-http

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetHttpProxies/http-lb-proxy-adv].
NAME               URL_MAP
http-lb-proxy-adv  web-map-http

接下来,您需要创建一条全局转发规则,将之前预留的 IP 地址映射到 HTTP 代理。

从 Cloud Shell

gcloud compute forwarding-rules create http-content-rule \
    --load-balancing-scheme EXTERNAL_MANAGED \
    --address=lb-ipv4-2 \
    --global \
    --target-http-proxy=http-lb-proxy-adv \
    --ports=80

此时,您可以使用之前记下的 IP 地址确认负载平衡器是否正常运行。

6. 验证负载平衡器是否正常运行

为了验证负载均衡功能是否正常运行,您需要生成一些负载。为此,我们将创建一个新的虚拟机来模拟负载。

创建 Siege-vm

现在,您将创建 siege-vm,并使用它来生成负载

从 Cloud Shell

gcloud compute instances create siege-vm \
    --network=httplbs \
    --zone=us-east1-a \
    --machine-type=e2-medium \
    --tags=allow-ssh,http-server \
    --metadata=startup-script='sudo apt-get -y install siege'

输出

Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/us-east1-a/instances/siege-vm].
NAME      ZONE             MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
siege-vm  us-central1-ir1  e2-medium                  10.132.0.15  34.143.20.68  RUNNING

接下来,您可以通过 SSH 连接到您创建的虚拟机。创建完成后,点击“SSH”以启动终端并进行连接。

连接后,运行以下命令以生成负载。使用您之前为外部 HTTP 负载平衡器预留的 IP 地址。

从 Cloud Shell

siege -c 20 http://$lb-ipv4-2

输出

New configuration template added to /home/cloudcurriculumdeveloper/.siege
Run siege -C to view the current settings in that file

检查负载分布

现在 Siege 正在运行,接下来需要检查流量是否平均分配到两个托管式实例组。

停止围攻

现在,您已证明高级流量拆分功能正常运行,接下来可以停止 Siege 了。为此,请返回 siege-vm 的 SSH 终端,然后按 Ctrl+C 停止正在运行的 siege。

7. 配置服务负载平衡器政策

创建服务负载平衡器政策

基本设置完成后,我们将创建服务负载平衡器政策并试用高级功能。下面以一个示例为例,说明如何配置服务以使用一些高级负载均衡设置。在此示例中,我们将创建一个政策来测试自动容量排空功能。不过,您可以随意试用其他功能。

从 Cloud Shell

gcloud beta network-services service-lb-policies create http-policy \
    --auto-capacity-drain --location=global

我们可以使用以下 gcloud 命令验证政策是否已成功创建:

从 Cloud Shell

gcloud beta network-services service-lb-policies list --location=global

输出

NAME
http-policy

将服务负载平衡器政策附加到后端服务

现在,我们将新政策附加到上述现有后端服务。

从 Cloud Shell

gcloud beta compute backend-services update east-backend-service \
    --service-lb-policy=http-policy --global

8. 调整后端健康状况

此时,新的服务 LB 政策已应用于您的后端服务。因此,从技术上讲,您可以直接跳到清理步骤。不过,在此 Codelab 中,我们还会进行一些额外的正式版调整,以向您展示新政策的运作方式。

当健康后端的总数降至某个阈值(25%)以下时,自动容量排空功能会自动从负载平衡器中移除后端 MIG。为了测试此功能,我们将通过 SSH 连接到 us-east1-b-mig 中的虚拟机,并使其处于不健康状态。如果阈值为 25%,您需要通过 SSH 连接到 4 个虚拟机并关闭 Apache 服务器。

为此,请选择 4 个虚拟机,然后点击“SSH”以启动终端并进行连接,从而通过 SSH 连接到这些虚拟机。然后运行以下命令。

sudo apachectl stop

此时,系统将触发自动容量耗尽功能,并且 us-east1-b-mig 将不再接收新请求。

9. 验证自动容量耗尽功能是否正常运行

重新启动 Siege

为了验证新功能,我们将再次使用 siege 虚拟机。我们来通过 SSH 连接到您在上一步中创建的虚拟机。创建完成后,点击“SSH”以启动终端并进行连接。

连接后,运行以下命令以生成负载。使用您之前为外部 HTTP 负载平衡器预留的 IP 地址。

从 Cloud Shell

siege -c 20 http://$lb-ipv4-2

输出

New configuration template added to /home/cloudcurriculumdeveloper/.siege
Run siege -C to view the current settings in that file

此时,您会发现所有请求都发送到了 us-east1-a-mig。

停止围攻

现在,您已证明高级流量拆分功能正常运行,接下来可以停止 Siege 了。为此,请返回 siege-vm 的 SSH 终端,然后按 Ctrl+C 停止正在运行的 siege。

10. 清理步骤

现在,我们已完成实验环境的设置,接下来需要将其拆除。请运行以下命令以删除测试环境。

从 Cloud Shell

gcloud compute instances delete siege-vm --zone=us-east1-a

gcloud compute forwarding-rules delete http-content-rule --global
gcloud compute target-http-proxies delete http-lb-proxy-adv

gcloud compute url-maps delete web-map-http

gcloud compute backend-services delete east-backend-service --global

gcloud compute addresses delete lb-ipv4-2 --global
gcloud compute health-checks delete http-basic-check 

gcloud beta network-services service-lb-policies delete http-policy --location=global

gcloud compute instance-groups managed delete us-east1-a-mig --zone=us-east1-a
gcloud compute instance-groups managed delete us-east1-b-mig --zone=us-east1-b

gcloud compute instance-templates delete test-template 

gcloud compute firewall-rules delete httplb-allow-http-rule
gcloud compute firewall-rules delete fw-allow-ssh

gcloud compute networks delete httplbs 

11. 恭喜!

恭喜您完成此 Codelab。

所学内容

  • 创建具有服务负载均衡政策的外部应用负载平衡器。
  • 为后端服务配置自动容量排空功能。

后续步骤

  • 试用服务 LB 政策提供的其他功能。