在 Compute Engine 实例中停用正常关停


本文档介绍了如何在现有 Compute Engine 实例中停用正常关停。如需详细了解正常关停,包括如何跳过针对单个停止或删除操作的正常关停,请参阅正常关停概览

如果您已在实例中启用正常关停,则可以停用该功能来执行以下操作:

  • 加快停止或删除操作的速度,以避免产生不必要的费用。

  • 更新需要重启的实例属性。

准备工作

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

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

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    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 时进行身份验证

所需的角色

如需获得在计算实例中停用正常关停所需的权限,请让管理员为您授予项目的 Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色包含在计算实例中停用正常关停所需的 compute.instances.update on the instance 权限。

您也可以使用自定义角色或其他预定义角色来获取此权限。

在实例中停用正常关停

您可以在计算实例中停用正常关停,而无需重启实例。不过,在实例正常关停 (PENDING_STOP) 的过程中,您无法停用正常关停。

如需在实例中停用正常关停,请选择以下选项之一:

控制台

  1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

    进入“虚拟机实例”

  2. 名称列中,点击实例名称以查看其详情。

    系统会打开实例的详情页面,并且详情标签页处于选中状态。

  3. 点击 修改

  4. 管理部分中,清除正常关停虚拟机复选框。

  5. 点击保存

gcloud

如需在实例中停用正常关停,请使用带有 --no-graceful-shutdown 标志的 gcloud beta compute instances update 命令

gcloud beta compute instances update INSTANCE_NAME \
    --no-graceful-shutdown \
    --zone=ZONE

替换以下内容:

  • INSTANCE_NAME:实例的名称。

  • ZONE:实例所在的区域。

REST

  1. 创建一个空的 JSON 文件。

  2. 如需查看现有实例的属性,请向 Beta 版 instances.get 方法发出 GET 请求:

    GET https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
    

    替换以下内容:

    • PROJECT_ID:实例所在项目的 ID。

    • ZONE:实例所在的区域。

    • INSTANCE_NAME:现有实例的名称。

  3. 在您在上一步中创建的空 JSON 文件中,执行以下操作:

    1. 输入 GET 请求输出中的实例属性。

    2. 找到 gracefulShutdown.enabled 字段,然后将其值更改为 false

      {
        ...
        "scheduling": {
          ...
          "gracefulShutdown": {
            "enabled": false
          }
        },
        ...
      }
      
  4. 如需更新实例,请向 Beta 版 instances.update 方法发出 PUT 请求。包含以下内容:

    • 在请求网址中,添加 mostDisruptiveAllowedAction 查询参数。

    • 在请求正文中,添加您在上述步骤中创建和更新的 JSON 文件中的实例属性。

    用于更新实例的 PUT 请求类似于以下内容:

    PUT https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME?mostDisruptiveAllowedAction=ALLOWED_ACTION
    
    {
      ...
      "scheduling": {
        ...
        "gracefulShutdown": {
          "enabled": false
        }
      },
      ...
    }
    

    ALLOWED_ACTION 替换为以下某个值:

    • NO_EFFECT:请求会检查更新请求是否有效以及资源是否可用,但不会更新实例。

    • REFRESH:如果修改后的实例属性不需要实例重启,则 Compute Engine 会更新实例。

如需详细了解如何更新实例的属性,请参阅更新实例属性

后续步骤