在 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 驗證說明文件中的「Authenticate for using REST」。

必要的角色

如要取得在 Compute 執行個體中停用正常關機所需的權限,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (roles/compute.instanceAdmin.v1) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這個預先定義的角色具備 compute.instances.update on the instance 權限,這是停用運算執行個體正常關機程序時的必要權限。

您或許還可透過自訂角色或其他預先定義的角色取得這項權限。

在執行個體中停用安全關機

您可以在不重新啟動執行個體的情況下,停用運算執行個體中的安全關機功能。不過,執行個體正在安全關機 (PENDING_STOP) 時,您無法停用安全關機。

如要在執行個體中停用正常關機程序,請選取下列其中一個選項:

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 在「名稱」欄中,按一下執行個體名稱即可查看詳細資料。

    執行個體的詳細資料頁面隨即開啟,並選取「詳細資料」分頁標籤。

  3. 按一下「Edit」(編輯)

  4. 在「管理」部分,取消勾選「按適當流程關閉 VM」核取方塊。

  5. 按一下 [儲存]

gcloud

如要在執行個體中停用正常關機,請使用 gcloud beta compute instances update 指令搭配 --no-graceful-shutdown 旗標:

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 就會更新執行個體。

如要進一步瞭解如何更新執行個體的屬性,請參閱「更新執行個體屬性」。

後續步驟