使用自訂限制條件管理 Compute Engine 資源


Google Cloud 機構政策可讓您透過程式以集中方式控管機構的資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於Google Cloud 資源和Google Cloud 資源階層中這些資源的子系。您可以在機構、資料夾或專案層級強制執行機構政策。

機構政策提供各種Google Cloud 服務的預先定義限制。不過,如要進一步自訂組織政策中受限制的特定欄位,也可以建立自訂限制,並在自訂組織政策中使用這些限制。

優點

  • 成本管理:使用自訂機構政策,限制貴機構可用的 VM 執行個體、磁碟大小和類型。您也可以限制 VM 執行個體使用的機器系列
  • 安全性、法規遵循和管理:您可以透過自訂機構政策,強制執行下列政策:
    • 如要強制執行安全性規定,您可以在 VM 上要求特定防火牆通訊埠規則。
    • 為支援硬體隔離或授權法規遵循,您可以要求特定專案或資料夾中的所有 VM 都必須在單一用戶群節點上執行。
    • 如要控管自動化指令碼,可以使用自訂機構政策,確認標籤符合指定運算式。

政策繼承

根據預設,機構政策會由您強制執行政策的資源子系繼承。舉例來說,如果您對資料夾強制執行政策, Google Cloud 系統會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為及如何變更,請參閱「階層評估規則」。

定價

機構政策服務 (包括預先定義和自訂機構政策) 免費提供。

事前準備

  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 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. 安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:

      gcloud init

      如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    2. Set a default region and zone.

必要的角色

如要取得管理 Compute Engine 資源組織政策所需的權限,請要求管理員授予下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這些預先定義的角色具備管理 Compute Engine 資源組織政策所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

如要管理 Compute Engine 資源的組織政策,必須具備下列權限:

  • orgpolicy.constraints.list
  • orgpolicy.policies.create
  • orgpolicy.policies.delete
  • orgpolicy.policies.list
  • orgpolicy.policies.update
  • orgpolicy.policy.get
  • orgpolicy.policy.set
  • 如要測試限制:
    • 專案的 compute.instances.create 權限
    • 如要使用自訂映像檔建立 VM,請按一下映像檔上的 compute.images.useReadOnly
    • 如要使用快照建立 VM,請按一下快照上的 compute.snapshots.useReadOnly
    • 如要使用執行個體範本建立 VM,請按一下執行個體範本上的 compute.instanceTemplates.useReadOnly
    • 如要將舊版網路指派給 VM:專案的 compute.networks.use
    • 如要為 VM 指定靜態 IP 位址,請在專案中擁有 compute.addresses.use 權限
    • 使用舊版網路時,如要將外部 IP 位址指派給 VM,請在專案中設定 compute.networks.useExternalIp
    • 為 VM 指定子網路:專案或所選子網路的 compute.subnetworks.use
    • 使用虛擬私有雲網路時,如要將外部 IP 位址指派給 VM,請在專案或所選子網路上擁有 compute.subnetworks.useExternalIp 權限
    • 如要為專案中的 VM 設定 VM 執行個體中繼資料,請執行下列指令:compute.instances.setMetadata
    • 如要為 VM 設定標記,請在 VM 上按一下 compute.instances.setTags
    • 如要為 VM 設定標籤,請在 VM 上執行下列步驟:compute.instances.setLabels
    • 如要設定 VM 使用的服務帳戶,請在 VM 上執行 compute.instances.setServiceAccount
    • 如要為 VM 建立新磁碟,請取得專案的 compute.disks.create 權限
    • 如要以唯讀或讀寫模式連接現有磁碟,請對磁碟擁有 compute.disks.use 權限
    • 如要以唯讀模式連接現有磁碟,請對磁碟擁有 compute.disks.useReadOnly 權限

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

Compute Engine 支援的資源

針對 Compute Engine,您可以對下列資源和欄位設定自訂限制。

  • Persistent Diskcompute.googleapis.com/Disk
    • 永久磁碟類型:resource.type
    • 永久磁碟大小:resource.sizeGb
    • 永久磁碟授權:resource.licenses
    • 永久磁碟授權代碼:resource.licenseCodes
    • Persistent Disk 機密運算:resource.enableConfidentialCompute
    • 永久磁碟來源映像檔:resource.sourceImage
  • 圖片compute.googleapis.com/Image
    • 原始磁碟來源:resource.rawDisk.source
  • VM 執行個體compute.googleapis.com/Instance
    • 進階機器功能:
      • resource.advancedMachineFeatures.enableNestedVirtualization
      • resource.advancedMachineFeatures.threadsPerCore
      • resource.advancedMachineFeatures.performanceMonitoringUnit
    • 機密 VM 執行個體設定:
      • resource.confidentialInstanceConfig.enableConfidentialCompute
      • resource.confidentialInstanceConfig.confidentialInstanceType
    • 防刪除功能:resource.deletionProtection
    • IP 轉送:resource.canIpForward
    • Private Google Access (IPv6):resource.privateIpv6GoogleAccess
    • 標籤數:resource.labels
    • 加速器:
      • resource.guestAccelerators.acceleratorType
      • resource.guestAccelerators.acceleratorCount
    • 機器類型:resource.machineType
    • 最低 CPU 平台:resource.minCpuPlatform
    • 名稱:resource.name
    • 網路介面:
      • resource.networkInterfaces.network
      • resource.networkInterfaces.subnetwork
      • resource.networkInterfaces.networkAttachment
    • 節點相依性:
      • resource.scheduling.nodeAffinities.key
      • resource.scheduling.nodeAffinities.operator
      • resource.scheduling.nodeAffinities.values
    • 受防護的執行個體設定:
      • resource.shieldedInstanceConfig.enableSecureBoot
      • resource.shieldedInstanceConfig.enableVtpm
      • resource.shieldedInstanceConfig.enableIntegrityMonitoring
    • 可用區:resource.zone
  • 其他支援的運算資源:
    • 如要進一步瞭解 Cloud Load Balancing 使用的 Compute Engine 資源,例如後端服務、後端 bucket、轉送規則、健康檢查、SSL 原則、目標 Proxy 和網址對應,請參閱「使用自訂限制管理 Cloud Load Balancing 資源」頁面。

設定自訂限制

自訂限制是由資源、方法、條件和動作所定義,這些項目受到您要強制執行機構政策的服務支援。自訂限制的條件是使用一般運算語言 (CEL) 定義。如要進一步瞭解如何使用 CEL 在自訂限制中建立條件,請參閱「建立及管理自訂機構組織政策」的 CEL 一節。

您可以使用 Google Cloud 控制台或 gcloud CLI 建立自訂限制,並設定在機構政策中使用。

控制台

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

    前往「機構政策」

  2. 選取頁面頂端的「專案挑選器」

  3. 專案選擇工具中,選取要設定機構政策的資源。

  4. 按一下「自訂限制」

  5. 在「顯示名稱」方塊中,輸入容易理解的限制名稱。這個欄位的長度上限為 200 個字元。 請勿在限制名稱中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。

  6. 在「Constraint ID」(限制條件 ID) 方塊中,輸入新自訂限制條件的名稱。自訂限制必須以 custom. 開頭,且只能包含大寫英文字母、小寫英文字母或數字,例如 custom.createOnlyN2DVMs。這個欄位的長度上限為 70 個字元,不含前置字元,例如 organizations/123456789/customConstraints/custom.

  7. 在「Description」方塊中,輸入限制的易讀說明,違反政策時會以錯誤訊息形式顯示。 這個欄位的長度上限為 2000 個字元。

  8. 在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱。例如:compute.googleapis.com/Instance

  9. 在「強制執行方式」下方,選取是否要對 REST CREATE 方法強制執行限制。

  10. 如要定義條件,請按一下「編輯條件」

    1. 在「新增條件」面板中,建立參照支援服務資源的 CEL 條件,例如 resource.machineType.contains('/machineTypes/n2d')。這個欄位的長度上限為 1000 個字元。

    2. 按一下 [儲存]

  11. 在「動作」下方,選取是否要在符合先前條件時允許或拒絕評估方法。

  12. 按一下「建立限制」

在每個欄位中輸入值後,右側會顯示這個自訂限制的對等 YAML 設定。

gcloud

如要使用 gcloud CLI 建立自訂限制,請建立自訂限制的 YAML 檔案:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resource_types: compute.googleapis.com/RESOURCE_NAME
method_types: CREATE
condition: CONDITION
action_type: ACTION
display_name: DISPLAY_NAME
description: DESCRIPTION

更改下列內容:

  • ORGANIZATION_ID:您的機構 ID,例如 123456789

  • CONSTRAINT_NAME:新自訂限制的名稱。自訂限制條件必須以 custom. 開頭,且只能包含大寫英文字母、小寫英文字母或數字。例如,custom.createOnlyN2DVMs。這個欄位的長度上限為 70 個字元,不含前置字元 (例如 organizations/123456789/customConstraints/custom.)。

  • RESOURCE_NAME:包含要限制物件和欄位的 Compute Engine API REST 資源名稱 (而非 URI)。例如:Instance

  • CONDITION:針對支援服務資源的代表項目編寫的 CEL 條件。這個欄位的長度上限為 1000 個字元。如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。例如:"resource.machineType.contains('/machineTypes/n2d')"

  • ACTION:如果符合 condition,則要採取的動作。可以是 ALLOWDENY

  • DISPLAY_NAME:限制條件的易記名稱。這個欄位的長度上限為 200 個字元。

  • DESCRIPTION:違反政策時,要以錯誤訊息形式顯示的限制說明。這個欄位的長度上限為 2000 個字元。

如要進一步瞭解如何建立自訂限制,請參閱「建立及管理自訂機構政策」。

為新的自訂限制建立 YAML 檔案後,您必須進行設定,才能在貴機構的機構政策中使用該檔案。如要設定自訂限制,請使用 gcloud org-policies set-custom-constraint 指令:
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
CONSTRAINT_PATH 替換為自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml。 完成後,自訂限制就會顯示在 Google Cloud 機構政策清單中,做為機構政策使用。如要確認自訂限制存在,請使用 gcloud org-policies list-custom-constraints 指令:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
ORGANIZATION_ID 替換為機構資源的 ID。 詳情請參閱「查看組織政策」。

強制執行自訂限制

如要強制執行限制,請建立參照該限制的機構政策,然後將該政策套用至 Google Cloud 資源。

控制台

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

    前往「機構政策」

  2. 在專案選擇工具中,選取要設定機構政策的專案。
  3. 在「Organization policies」(機構政策) 頁面上的清單中選取限制條件,即可查看該限制條件的「Policy details」(政策詳情) 頁面。
  4. 如要設定這項資源的機構政策,請按一下「管理政策」
  5. 在「編輯政策」頁面中,選取「覆寫上層政策」
  6. 按一下「新增規則」
  7. 在「Enforcement」(強制執行) 區段中,選取是否要強制執行這項機構政策。
  8. 選用:如要根據標記設定機構政策條件,請按一下「新增條件」。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「使用標記設定組織政策」。
  9. 按一下「測試變更」,模擬機構政策的影響。舊版受管理限制不支援政策模擬。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
  10. 如要完成並套用機構政策,請按一下「設定政策」。這項政策最多需要 15 分鐘才會生效。

gcloud

如要建立含有布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

取代下列項目:

  • PROJECT_ID:要強制執行限制的專案。
  • CONSTRAINT_NAME:您為自訂限制定義的名稱。例如:custom.createOnlyN2DVMs

如要強制執行包含限制的機構政策,請執行下列指令:

    gcloud org-policies set-policy POLICY_PATH
    

POLICY_PATH 替換為機構政策 YAML 檔案的完整路徑。這項政策最多需要 15 分鐘才會生效。

範例:建立限制 VM 使用 N2D 機器類型的限制

gcloud

  1. 建立包含下列資訊的 onlyN2DVMs.yaml 限制檔案:

    name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs
    resource_types: compute.googleapis.com/Instance
    condition: "resource.machineType.contains('/machineTypes/n2d')"
    action_type: ALLOW
    method_types: CREATE
    display_name: Only N2D VMs allowed
    description: Restrict all VMs created to only use N2D machine types.
  2. 設定自訂限制。

    gcloud org-policies set-custom-constraint onlyN2DVMs.yaml
    
  3. 建立包含下列資訊的 onlyN2DVMs-policy.yaml 政策檔案。 在本範例中,我們是在專案層級強制執行這項限制,但您也可以在機構或資料夾層級設定這項限制。將 PROJECT_ID 替換為您的專案 ID。

    name: projects/PROJECT_ID/policies/custom.createOnlyN2DVMs
    spec:
      rules:
    enforce: true
  4. 強制執行政策。

    gcloud org-policies set-policy onlyN2DVMs-policy.yaml
    
  5. 如要測試限制,請嘗試建立使用非 N2D 機器類型的 VM。

    gcloud compute instances create my-test-instance \
        --project=PROJECT_ID \
        --zone=us-central1-c \
        --machine-type=e2-medium
    

    輸出結果會與下列內容相似:

    ERROR: (gcloud.compute.instances.create) Could not fetch resource:
    – Operation denied by custom org policies: [customConstraints/custom.createOnlyN2DVMs]: Restrict all VMs created to only use N2D machine types.
    

常見用途的自訂限制範例

以下各節提供一些實用的自訂限制語法:

磁碟

用途 語法
永久磁碟類型必須為「極端永久磁碟 (pd-extreme)」
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksPDExtremeOnly
  resource_types: compute.googleapis.com/Disk
  condition: "resource.type.contains('pd-extreme')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Create pd-extreme disks only
  description: Only the extreme persistent disk type is allowed to be created.
磁碟大小不得超過 250 GB
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksLessThan250GB
  resource_types: compute.googleapis.com/Disk
  condition: "resource.sizeGb <= 250"
  action_type: ALLOW
  method_types: CREATE
  display_name: Disks size maximum is 250 GB
  description: Restrict the boot disk size to 250 GB or less for all VMs.

映像檔

用途 語法
來源圖片只能來自 Cloud Storage test_bucket
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksfromStoragebucket
  resource_types: compute.googleapis.com/Image
  condition: "resource.rawDisk.source.contains('storage.googleapis.com/test_bucket/')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Source image must be from Cloud Storage test_bucket only
  description: Source images used in this project must be imported from the
  Cloud Storage test_bucket.

VM 執行個體

用途 語法
VM 必須有標籤,且索引鍵設為 cost center
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createVMWithLabel
  resource_types: compute.googleapis.com/Instance
  condition: "'cost_center' in resource.labels"
  action_type: ALLOW
  method_types: CREATE
  display_name: 'cost_center' label required
  description: Requires that all VMs created must have the a 'cost_center' label
  that can be used for tracking and billing purposes.
VM 必須有標籤,且鍵設為 cost center,值設為 eCommerce
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createECommerceVMOnly
  resource_types: compute.googleapis.com/Instance
  condition: "'cost_center' in resource.labels and resource.labels['cost_center'] == 'eCommerce'"
  action_type: ALLOW
  method_types: CREATE
  display_name:  Label (cost_center/eCommerce) required
  description: Label required and Key/value must be cost_center/eCommerce.
VM 必須使用 N2D 機型
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs
  resource_types: compute.googleapis.com/Instance
  condition: "resource.machineType.contains('/machineTypes/n2d')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only N2D VMs allowed
  description: Restrict all VMs created to only use N2D machine types.
VM 必須使用機器類型 e2-highmem-8
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyE2highmem8
  resource_types: compute.googleapis.com/Instance
  condition: "resource.machineType.endsWith('-e2-highmem-8')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only "e2-highmem-8" VMs allowed
  description: Restrict all VMs created to only use the E2 high-memory
  machine types that have 8 vCPUs.
確保 VM 排定在「foo」節點群組上
  name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlySTVM
  resource_types: compute.googleapis.com/Instance
  condition: "resource.scheduling.nodeAffinities.exists(n, n.key == 'foo')"
  action_type: ALLOW
  method_types: CREATE
  display_name: Only VMs scheduled on node group "foo" allowed
  description: Restrict all VMs created to use the node group "foo".

後續步驟