搜索

第 3 章 Gatekeeper operator

download PDF

Gatekeeper 是一个验证 webhook,它带有审计功能,可以强制执行基于自定义资源定义的策略,该策略使用 Open Policy Agent (OPA) 运行。您可以使用 Gatekeeper operator 策略在集群中安装 Gatekeeper。Gatekeeper 约束可用于评估 Kubernetes 资源合规性。您可以使用 OPA 作为策略引擎,并使用 Rego 作为策略语言。

先决条件: 安装 Gatekeeper 并将 Gatekeeper 策略应用到集群时,需要 Red Hat Advanced Cluster Management for Kubernetes 或 Red Hat OpenShift Container Platform Plus 订阅。只有在最新版本的 Red Hat Advanced Cluster Management 支持的 OpenShift Container Platform 版本(版本 3.11 除外)上支持 gatekeeper。

继续阅读以了解更多有关使用 Gatekeeper operator 的信息:

集成 Gatekeeper 约束和约束模板管理 Gatekeeper operator 策略

3.1. 集成 Gatekeeper 约束和约束模板

Gatekeeper 策略通过使用约束模板(ConstraintTemplates)和约束编写。查看 Red Hat Advanced Cluster Management 策略中使用 Gatekeeper 约束的以下 YAML 示例:

  • ConstraintTemplates 和约束: 使用 Red Hat Advanced Cluster Management 策略在 hub 集群上进行多集群发布 Gatekeeper 约束和 Gatekeeper 审计结果聚合。以下示例定义了一个 Gatekeeper ConstraintTemplate 和 constraint (K8sRequiredLabels),以确保在所有命名空间中设置了 gatekeeper 标签:

    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: require-gatekeeper-labels-on-ns
    spec:
      remediationAction: inform 1
      disabled: false
      policy-templates:
        - objectDefinition:
            apiVersion: templates.gatekeeper.sh/v1beta1
            kind: ConstraintTemplate
            metadata:
              name: k8srequiredlabels
              annotations:
                policy.open-cluster-management.io/severity: low 2
            spec:
              crd:
                spec:
                  names:
                    kind: K8sRequiredLabels
                  validation:
                    openAPIV3Schema:
                      properties:
                        labels:
                          type: array
                          items: string
              targets:
                - target: admission.k8s.gatekeeper.sh
                  rego: |
                    package k8srequiredlabels
                    violation[{"msg": msg, "details": {"missing_labels": missing}}] {
                      provided := {label | input.review.object.metadata.labels[label]}
                      required := {label | label := input.parameters.labels[_]}
                      missing := required - provided
                      count(missing) > 0
                      msg := sprintf("you must provide labels: %v", [missing])
                    }
        - objectDefinition:
            apiVersion: constraints.gatekeeper.sh/v1beta1
            kind: K8sRequiredLabels
            metadata:
              name: ns-must-have-gk
              annotations:
                policy.open-cluster-management.io/severity: low 3
            spec:
              enforcementAction: dryrun
              match:
                kinds:
                  - apiGroups: [""]
                    kinds: ["Namespace"]
              parameters:
                labels: ["gatekeeper"]
    1
    因为 remediationAction 被设置为 inform,所以 Gatekeeper 约束的 enforcementAction 字段会被覆盖来 warn。这意味着 Gatekeeper 会检测并警告您创建或更新缺少 gatekeeper 标签的命名空间。如果策略 remediationAction 设置为 enforce,则 Gatekeeper 约束 enforcementAction 字段会被覆盖为 deny。在这种情况下,此配置可防止任何用户创建或更新缺少 gatekeeper 标签的命名空间。
    2 3
    可选:为每个 Gatekeeper 约束或约束模板设置 policy.open-cluster-management.io/severity 注解的严重性值。有效值与其他 Red Hat Advanced Cluster Management 策略类型相同:low, medium, high, 或 critical

    使用前面的策略,您可能会收到以下策略状态消息:warn - you must provide labels: {"gatekeeper"} (on Namespace default); warn - you must provide labels: {"gatekeeper"} (on Namespace gatekeeper-system)。当包含 Gatekeeper 约束或 ConstraintTemplates 被删除后,限制和 ConstraintTemplates 也会从受管集群中删除。

    要从控制台查看特定受管集群的 Gatekeeper 审计结果,请进入策略模板 结果 页面。如果启用了搜索,请查看失败的审计的 Kubernetes 对象的 YAML。

    备注:

    • 只有在 Gatekeeper 版本 3.9 或更高版本生成的审计结果时,相关资源 部分才可用。
    • Gatekeeper 审计功能默认每分钟运行一次。审计结果会发回到 hub 集群,以便在受管集群的 Red Hat Advanced Cluster Management 策略状态中查看。
  • policy-gatekeeper-admission :在 Red Hat Advanced Cluster Management 策略中使用 policy-gatekeeper-admission 配置策略来检查 gatekeeper admission webhook 拒绝的 Kubernetes API 请求:

    apiVersion: policy.open-cluster-management.io/v1
    kind: ConfigurationPolicy
    metadata:
      name: policy-gatekeeper-admission
    spec:
      remediationAction: inform # will be overridden by remediationAction in parent policy
      severity: low
      object-templates:
        - complianceType: mustnothave
          objectDefinition:
            apiVersion: v1
            kind: Event
            metadata:
              namespace: openshift-gatekeeper-system # set it to the actual namespace where gatekeeper is running if different
              annotations:
                constraint_action: deny
                constraint_kind: K8sRequiredLabels
                constraint_name: ns-must-have-gk
                event_type: violation

3.1.1. 其他资源

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.