使用自訂限制條件管理 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. 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.
  • 請務必確認您知道自己的機構 ID

必要的角色

如要取得管理 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: compute.instances.setMetadata 設定 VM 執行個體中繼資料
    • 如何為 VM 設定標記:compute.instances.setTags
    • 如何為 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
    • 私人 Google 存取權 (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 資源 (例如後端服務、後端值區、轉送規則、健康檢查、SSL 政策、目標 Proxy 和網址對應),請參閱「使用自訂限制管理 Cloud Load Balancing 資源」頁面。

設定自訂限制

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

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

控制台

  1. 在 Google Cloud 控制台中,前往「Organization policies」(機構政策) 頁面。

    前往「機構政策」

  2. 選取頁面頂端的「Project picker」

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

  4. 按一下 「自訂限制」

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

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

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

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

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

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

    1. 在「Add condition」面板中,建立參照支援的服務資源 (例如 resource.machineType.contains('/machineTypes/n2d')) 的 CEL 條件。這個欄位的長度上限為 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. 在「Edit policy」頁面中,選取「Override parent's policy」
  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.
確保在「foo」節點群組中排定 VM
  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".

後續步驟