使用自定义限制条件管理 Dataproc 资源
Google Cloud 组织政策可让您以编程方式集中控制组织的资源。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的 Google Cloud 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。
组织政策为各种 Google Cloud 服务提供预定义限制条件。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义限制条件并在自定义组织政策中使用这些自定义限制条件。
优势
您可以使用自定义组织政策来允许或拒绝 Dataproc 集群上的具体操作。例如,如果创建或更新集群的请求未能满足组织政策设置的自定义限制条件验证,该请求将失败,并向调用方返回错误。
政策继承
如果您对资源强制执行政策,默认情况下,该资源的后代会继承组织政策。例如,如果您对某个文件夹强制执行一项政策,Google Cloud 会对该文件夹中的所有项目强制执行该政策。如需详细了解此行为及其更改方式,请参阅层次结构评估规则。
价格
组织政策服务(包括预定义组织政策和自定义组织政策)可免费使用。
准备工作
- 设置项目
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
启用 Dataproc API。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
启用 Dataproc API。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
- 确保您知道您的组织 ID。
所需的角色
如需获取管理组织政策所需的权限,请让管理员授予您以下 IAM 角色:
-
组织资源的 Organization Policy Administrator (
roles/orgpolicy.policyAdmin
) -
如需创建或更新 Dataproc 集群,请执行以下操作:
针对项目资源的 Dataproc Admin 或 Dataproc Editor(
roles/dataproc.admin
或roles/dataproc.editor
)
如需详细了解如何授予角色,请参阅管理访问权限。
这些预定义角色包含管理组织政策所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
如需管理组织政策,您必须拥有以下权限:
-
orgpolicy.constraints.list
-
orgpolicy.policies.create
-
orgpolicy.policies.delete
-
orgpolicy.policies.list
-
orgpolicy.policies.update
-
orgpolicy.policy.get
-
orgpolicy.policy.set
创建自定义限制条件
自定义限制条件在 YAML 文件中由应用该限制条件的资源、方法、条件和操作定义。Dataproc 支持应用于 CLUSTER
资源的 CREATE
和 UPDATE
方法的自定义限制条件(请参阅 Dataproc 资源和操作限制条件)。
如需为 Dataproc 自定义限制条件创建 YAML 文件,请执行以下操作:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/Cluster
methodTypes:
- METHOD
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
替换以下内容:
ORGANIZATION_ID
:您的组织 ID,例如123456789
。CONSTRAINT_NAME
:新的自定义限制条件的名称。 自定义限制条件必须以custom.
开头,并且只能包含大写字母、小写字母或数字,例如 custom.dataprocEnableComponentGateway。此字段的最大长度为 70 个字符,不包括前缀,例如organizations/123456789/customConstraints/custom
。
METHOD
:创建集群创建限制条件时,指定CREATE
。创建集群UPDATE
限制条件时,请按如下方式指定这两者:methodTypes: - CREATE - UPDATE
CONDITION
:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的最大长度为 1,000 个字符。如需详细了解可用于写入条件的资源,请参阅支持的资源。 例如"resource.config.endpointConfig.enableHttpPortAccess==true"
。ACTION
:满足condition
时要执行的操作。可以是ALLOW
或DENY
。DISPLAY_NAME
:限制条件的易记名称,例如“强制启用 Dataproc 组件网关”。此字段的最大长度为 200 个字符。DESCRIPTION
:有关限制条件的直观说明,在违反政策时显示为错误消息,例如“仅允许在已启用组件网关的情况下创建 Dataproc 集群”。此字段的最大长度为 2000 个字符。
如需详细了解如何创建自定义限制条件,请参阅定义自定义限制条件。
设置自定义限制条件
使用 Google Cloud CLI 创建新的自定义限制条件后,您必须对其进行设置,以使其可用于组织中的组织政策。如需设置自定义限制条件,请使用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 资源。控制台
如需强制执行布尔值限制条件,请执行以下操作:
- 在 Google Cloud 控制台中,转到组织政策页面。
- 选择页面顶部的项目选择器。
- 从项目选择器中,选择要为其设置组织政策的项目。
- 从组织政策页面的列表中选择限制条件。 此时应显示该限制条件的政策详情页面。
- 如需为该资源配置组织政策,请点击管理政策。
- 在修改政策页面,选择覆盖父级政策。
- 点击添加规则。
- 在强制执行下,选择开启还是关闭此组织政策的强制执行。
- (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅使用标记设置组织政策。
- 如果是自定义限制条件,您可以点击测试更改来模拟此组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改。
- 若要完成并应用组织政策,请点击设置政策。该政策将在 15 分钟内生效。
gcloud
如需创建强制执行布尔值限制条件的组织政策,请创建引用该限制条件的 YAML 政策文件:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
请替换以下内容:
-
PROJECT_ID
:要对其实施限制条件的项目。 -
CONSTRAINT_NAME
:您为自定义限制条件定义的名称。例如,custom.dataprocEnableComponentGateway
。
如需强制执行包含限制条件的组织政策,请运行以下命令:
gcloud org-policies set-policy POLICY_PATH
将 POLICY_PATH
替换为组织政策 YAML 文件的完整路径。该政策将在 15 分钟内生效。
测试自定义限制条件
以下集群创建示例假定已创建自定义组织政策,并在创建集群时强制执行该政策,以要求启用组件网关 (resource.config.endpointConfig.enableHttpPortAccess==true
)。
gcloud dataproc clusters create example-cluster \
--project=PROJECT_ID \
--zone=COMPUTE_ZONE
示例输出(默认情况下,创建 Dataproc 集群时未启用组件网关):
Operation denied by custom org policies: ["customConstraints/custom.dataprocEnableComponentGateway": "Only allow Dataproc cluster creation if the Component Gateway is enabled"]
Dataproc 对资源和操作的限制
创建或更新 Dataproc 集群时,可使用以下 Dataproc 自定义限制条件字段。请注意,更新集群时,仅支持与可修改的集群参数相关的限制条件(请参阅更新集群)。
- Compute Engine 网络配置(networkUri、internalIpOnly、serviceAccount 和 metadata)
resource.config.gceClusterConfig.networkUri
resource.config.gceClusterConfig.internalIpOnly
resource.config.gceClusterConfig.serviceAccount
resource.config.gceClusterConfig.metadata
- Compute Engine 实例组配置(imageUri 和 machineTypeUri)
resource.config.masterConfig.imageUri
resource.config.masterConfig.machineTypeUri
resource.config.workerConfig.imageUri
resource.config.workerConfig.machineTypeUri
resource.config.secondaryWorkerConfig.imageUri
resource.config.secondaryWorkerConfig.machineTypeUri
- 初始化操作 (executableFile)
resource.config.initializationActions.executableFile
- 软件配置(imageVersion、属性和可选属性)
resource.config.softwareConfig.imageVersion
resource.config.softwareConfig.properties
resource.config.softwareConfig.optionalComponents
- Kerberos 配置(启用 Kerberos 和 crossRealmTrustKdc)
resource.config.securityConfig.kerberosConfig.enableKerberos
resource.config.securityConfig.kerberosConfig.crossRealmTrustKdc
- 组件网关 (enableHttpPortAccess)
resource.config.endpointConfig.enableHttpPortAccess
- Metastore 配置 (dataprocMetastoreService)
resource.config.metastoreConfig.dataprocMetastoreService
- 永久性磁盘 CMEK (gcePdKmsKeyName)
resource.config.encryptionConfig.gcePdKmsKeyName
- 集群标签
resource.labels
- 集群大小
resource.config.masterConfig.numInstances
resource.config.workerConfig.numInstances
resource.config.secondaryWorkerConfig.numInstances
- 自动扩缩
resource.config.autoscalingConfig.policyUri
常见用例的自定义限制条件示例
下表提供了自定义限制条件的示例:
说明 | 限制条件语法 |
---|---|
创建集群或更新时,将 Dataproc 工作器实例的数量限制为不超过 10 个。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocNoMoreThan10Workers resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE - UPDATE condition: "resource.config.workerConfig.numInstances + resource.config.secondaryWorkerConfig.numInstances > 10" actionType: DENY displayName: Total number of worker instances cannot be larger than 10 description: Cluster cannot have more than 10 workers, including primary and secondary workers. |
阻止应用主实例在 Dataproc 集群抢占式工作器上运行。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocAmPrimaryOnlyEnforced resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "('dataproc:am.primary_only' in resource.config.softwareConfig.properties) && (resource.config.softwareConfig.properties['dataproc:am.primary_only']==true)" actionType: ALLOW displayName: Application master cannot run on preemptible workers description: Property "dataproc:am.primary_only" must be "true". |
禁止 Dataproc 集群上的自定义 Hive 属性。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocNoCustomHiveProperties resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.config.softwareConfig.properties.all(p, !p.startsWith('hive:'))" actionType: ALLOW displayName: Cluster cannot have custom Hive properties description: Only allow Dataproc cluster creation if no property starts with Hive prefix "hive:". |
禁止将 n1-standard-2 机器类型用于 Dataproc 主实例。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocMasterMachineType resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.config.masterConfig.machineTypeUri.contains('n1-standard-2')" actionType: DENY displayName: Master cannot use the n1-standard-2 machine type description: Prevent Dataproc cluster creation if the master machine type is n1-standard-2. |
强制使用指定的初始化操作脚本。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocInitActionScript resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.config.initializationActions.exists(action, action.executableFile=='gs://some/init-action.sh')" actionType: ALLOW displayName: Initialization action script "gs://some/init-action.sh" must be used description: Only allow Dataproc cluster creation if the "gs://some/init-action.sh". initialization action script is used. |
强制使用指定的永久性磁盘加密密钥。 |
name: organizations/ORGANIZATION_ID/custom.dataprocPdCmek resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.config.encryptionConfig.gcePdKmsKeyName == 'projects/project-id/locations/global/keyRings/key-ring-name/cryptoKeys/key-name'" actionType: ALLOW displayName: Cluster PD must be encrypted with "key-name" from "key-ring-name" key-ring description: Only allow Dataproc cluster creation if the PD is encrypted with "key-name" from "key-ring-name" key-ring. |
强制执行集群标签限制。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocEnvLabel resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE - UPDATE condition: "('env' in resource.labels) && (resource.labels.env=='test')" actionType: DENY displayName: Cluster cannot have the "env=test" label description: Deny Dataproc cluster creation or update if the cluster will be labeled "env=test". |
强制使用非默认网络。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.dataprocNoDefaultNetwork resourceTypes: - dataproc.googleapis.com/Cluster methodTypes: - CREATE condition: "resource.config.gceClusterConfig.networkUri.contains('networks/default')" actionType: DENY displayName: Cluster cannot be created in the default network description: Deny Dataproc cluster creation if the cluster will be created in the default network. |