模拟区域级 MIG 在一个可用区发生服务中断的情况


如需测试您的区域级代管式实例组 (MIG) 的超额预配是否足够,能否在发生可用区服务中断后继续运行,您可以使用以下示例来模拟一个可用区级故障。

准备工作

  • 如果您要使用本指南中的命令行示例,请安装 Google Cloud CLI
  • 如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:

    Select the tab for how you plan to use the samples on this page:

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.
    3. REST

      如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。

        After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

使用脚本模拟可用区服务中断

默认情况下,此脚本会停止并启动 Apache。如果这不适用于您的应用,请将停止和启动 Apache 的命令替换为您遇到的故障和恢复情况下的命令。

  1. 在实例组中的每个虚拟机中连续部署并运行此脚本。您可以通过以下方式完成此操作:将此脚本添加到实例模板,或将此脚本添加到自定义映像中,并在实例模板中使用该映像。

    #!/usr/bin/env bash
    
    # Copyright 2016 Google Inc. All Rights Reserved.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    set -o nounset
    set -o errexit
    set -o pipefail
    
    function GetMetadata() {
      curl -s "$1" -H "Metadata-Flavor: Google"
    }
    
    PROJECT_METADATA_URL="http://metadata.google.internal/computeMetadata/v1/project/attributes"
    INSTANCE_METADATA_URL="http://metadata.google.internal/computeMetadata/v1/instance"
    ZONE=$(GetMetadata "$INSTANCE_METADATA_URL/zone" | cut -d '/' -f 4)
    INSTANCE_NAME=$(hostname)
    
    # We keep track of the state to make sure failure and recovery is triggered only once.
    STATE="healthy"
    while true; do
      if [[ "$ZONE" = "$(GetMetadata $PROJECT_METADATA_URL/failed_zone)" ]] && \
         [[ "$INSTANCE_NAME" = *"$(GetMetadata $PROJECT_METADATA_URL/failed_instance_names)"* ]]; then
        if [[ "$STATE" = "healthy" ]]; then
          STATE="failure"
          # Do something to simulate failure here.
          echo "STARTING A FAILURE"
          /etc/init.d/apache2 stop
        fi
      else
        if [[ "$STATE" = "failure" ]] ; then
          STATE="healthy"
          # Do something to recover here.
          echo "RECOVERING FROM FAILURE"
          /etc/init.d/apache2 start
        fi
      fi
      sleep 5
    done
    
    
  2. 通过设置以下两个项目元数据字段来模拟在一个地区发生的故障:

    • failed_zone:设置您要在其中模拟服务中断情况的区域(假定只有一个区域发生了故障)。
    • failed_instance_names:按名称选择要离线的虚拟机(假定只有名称包含此字符串的虚拟机发生了故障)。

    您可以使用 gcloud CLI 设置此元数据。例如,以下命令设置 europe-west1-b 可用区发生服务中断的情况,并影响名称以 base-instance-name 开头的虚拟机:

    gcloud compute project-info add-metadata --metadata failed_zone='europe-west1-b',failed_instance_names='base-instance-name-'
  3. 模拟完服务中断情况后,通过移除元数据键来进行故障恢复:

    gcloud compute project-info remove-metadata --keys failed_zone,failed_instance_names

以下是一些可使用此脚本运行的故障场景:

  • 完全停止应用以查看 MIG 的响应方式。
  • 在进行负载均衡健康检查时,使您的虚拟机返回“健康状况不佳”。
  • 修改 iptables 以阻止一部分流量进出虚拟机。
  • 关停虚拟机。默认情况下,地区级 MIG 很快就会重新创建虚拟机,但只要设置了元数据值,新的虚拟机将在脚本运行后立即自行关停。这会导致崩溃循环。

后续步骤