禁止建立使用容器中繼資料的 VM

為確保機構中的資源不會使用已淘汰的容器啟動代理程式和相關gce-container-declaration中繼資料,Google 建議您強制執行機構政策。如果強制執行受管理限制 compute.managed.disableVmsWithContainerStartupAgent,系統會禁止建立使用已淘汰中繼資料的資源。

本文說明如何執行下列操作:

  • 強制執行機構政策,禁止建立使用容器啟動代理程式的 Compute Engine 執行個體。
  • 在模擬測試模式下強制執行政策,監控組織政策的影響。
  • 使用 Logs Explorer,找出嘗試使用已淘汰代理程式的專案。

強制執行機構政策,禁止建立使用容器中繼資料的 VM

為避免建立使用已淘汰容器啟動代理程式的資源,Google 建議您強制執行機構政策constraints/compute.managed.disableVmsWithContainerStartupAgent 限制會禁止使用 gce-container-declaration 中繼資料鍵建立新資源。這項限制不會影響現有執行個體或執行個體範本。

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Compute Engine API 強制執行這項限制。

控制台

如要透過主控台設定機構政策,請完成下列步驟:

  1. 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。

    前往「Organization policies」(組織政策)

  2. 在專案選擇工具中,選取要編輯組織政策的專案、資料夾或機構。

    「Organization policies」(機構政策) 頁面會顯示可用的機構政策限制清單。

  3. 從限制清單中選取「Disable creation of Compute Engine instances that use the deprecated container startup agent (konlet)」限制。「Policy details」(政策詳情) 頁面會描述限制條件,並說明限制條件的套用方式。

  4. 如要更新這項資源的機構政策,請按一下「管理政策」

  5. 在「Edit policy」(編輯政策) 頁面,按一下「Override parent's policy」(覆寫上層政策)

  6. 選取「新增規則」

  7. 在「Enforcement」(強制執行) 下方,選取「On」 (開啟)。

  8. 如要預覽機構政策變更的影響,請在政策生效前按一下「測試變更」。如要進一步瞭解如何測試機構政策變更,請參閱「使用 Policy Simulator 測試機構政策變更」。

  9. 如要在模擬測試模式下強制執行組織政策,請按一下「設定模擬測試政策」。詳情請參閱「從有效政策以模擬測試模式建立組織政策」。

  10. 確認機構政策在試營運模式下運作正常後,請按一下「設定政策」,設定正式政策。

gcloud

  1. 建立 YAML 檔案來定義組織政策。

    name: RESOURCE_TYPE/RESOURCE_ID/policies/compute.managed.disableVmsWithContainerStartupAgent
    spec:
      rules:
      - enforce: true
    dryRunSpec:
      rules:
      - enforce: true
    

    更改下列內容:

    • RESOURCE_TYPE,搭配 organizationsfoldersprojects

    • RESOURCE_ID,視 RESOURCE_TYPE 中指定的資源類型而定,可以是機構 ID、資料夾 ID、專案 ID 或專案編號。

    以下範例顯示的 YAML 檔案會為 ID 為 123456 的專案強制執行機構政策:

    name: projects/123456/policies/compute.managed.disableVmsWithContainerStartupAgent
    spec:
      rules:
      - enforce: true
    dryRunSpec:
      rules:
      - enforce: true
    

    (選用) 如要根據標記設定組織政策的條件,請在 rules 中新增 condition 區塊。如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「使用標記設定組織政策」。

  2. 執行 org-policies set-policy 指令並搭配 dryRunSpec 旗標,以模擬執行模式設定組織政策:

     gcloud org-policies set-policy POLICY_PATH \
       --update-mask=dryRunSpec
    

    請將 POLICY_PATH 替換為機構政策 YAML 檔案的完整路徑。

    如要進一步瞭解模擬測試組織政策,請參閱「以模擬測試模式建立組織政策」。

  3. 使用 policy-intelligence simulate orgpolicy 指令,在強制執行機構政策變更前,預先瞭解影響:

    gcloud policy-intelligence simulate orgpolicy \
      --organization=ORGANIZATION_ID \
      --policies=POLICY_PATH
    

    更改下列內容:

    • ORGANIZATION_ID 替換成機構 ID,例如 1234567890123。系統不支援模擬多個機構的變更。

    • POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。

    如要進一步瞭解如何測試組織政策變更,請參閱「使用 Policy Simulator 測試組織政策變更」。

  4. 確認模擬測試模式中的機構政策運作正常後,請使用 org-policies set-policy 指令和 spec 旗標設定正式政策:

    gcloud org-policies set-policy POLICY_PATH \
      --update-mask=spec
    

    請將 POLICY_PATH 替換為機構政策 YAML 檔案的完整路徑。

REST

如要設定機構政策,請使用 organizations.policies.create 方法。

POST https://orgpolicy.googleapis.com/v2/{parent=organizations/ORGANIZATION_ID}/policies

要求 JSON 主體包含組織政策的定義。如果這項限制不支援參數,請省略 rules 下方的 parameters 區塊。

{
  "name": "RESOURCE_TYPE/RESOURCE_ID/policies/compute.managed.disableVmsWithContainerStartupAgent",
  "spec": {
    "rules": [
      {
        "enforce": true,
      }
    ]
  },
  "dryRunSpec": {
    "rules": [
      {
        "enforce": true,
      }
    ]
  }
}

更改下列內容:

  • RESOURCE_TYPE,搭配 organizationsfoldersprojects

  • RESOURCE_ID,視 RESOURCE_TYPE 中指定的資源類型而定,可以是機構 ID、資料夾 ID、專案 ID 或專案編號。

以下範例顯示要求主體,該主體會對 ID 為 123456 的專案強制執行機構政策:

  {
    "name": "projects/123456/policies/compute.managed.disableVmsWithContainerStartupAgent",
    "spec": {
      "rules": [
        {
          "enforce": true,
        }
      ]
    },
    "dryRunSpec": {
      "rules": [
        {
          "enforce": true,
        }
      ]
    }
  }
  ```

Optionally, to make the organization policy conditional on a tag, add a
`condition` block to the `rules`. If you add a conditional rule to an
organization policy, you must add at least one unconditional rule or the
policy cannot be saved. For more details, see
[Setting an organization policy with tags](/resource-manager/docs/organization-policy/tags-organization-policy).

For more information about dry-run organization policies, see
[Create an organization policy in dry-run mode](/resource-manager/docs/organization-policy/dry-run-policy).

在模擬測試模式下強制執行政策,監控已淘汰中繼資料的使用情況

您可以採用模擬執行模式套用政策,而非直接強制執行政策,以免系統禁止建立使用容器宣告中繼資料的例項。這項設定可讓您監控及記錄政策可封鎖的任何動作,但不會實際干擾作業。詳情請參閱「以模擬測試模式建立組織政策」。

當動作觸發模擬測試政策時 (例如嘗試使用 gce-container-declaration 中繼資料鍵建立執行個體),Cloud 稽核記錄會產生記錄項目。

如要找出嘗試使用已淘汰代理程式的專案,請完成下列步驟:

  1. 前往 Google Cloud 控制台的「Logs Explorer」頁面。

    前往「Logs explorer」(記錄檔探索工具)

  2. 在「Query」(查詢) 窗格中,輸入下列查詢:

    protoPayload.metadata.dryRun="true"
    protoPayload.methodName="CheckOrgPolicy"
    protoPayload.resourceName =~ "/compute.managed.disableVmsWithContainerStartupAgent"
    
  3. 點選「執行查詢」

  4. 查看記錄檔項目,找出嘗試使用已淘汰代理程式的專案。 違規模擬記錄具有下列特性:

    • 與「orgpolicy.googleapis.com」相關。
    • protoPayload.metadata.dryRun 欄位設為 true
    • 違規詳細資料中會列出 constraints/compute.managed.disableVmsWithContainerStartupAgent 限制。
  5. 查看稽核記錄中的資訊,瞭解仍在使用已淘汰代理程式的位置和原因。這項資訊可做為參考,將這些工作負載遷移至支援的替代方案。

  6. 確認模擬測試模式下的機構政策運作正常後,請將強制執行狀態從模擬測試模式變更為運作中,強制執行政策

如要進一步瞭解如何使用 Logs Explorer,請參閱「使用 Logs Explorer 查看記錄檔」。

後續步驟