4.16. 管理安全策略


创建一个安全策略,根据您指定的安全标准、类别和控制,报告并验证您的集群合规性。

查看以下部分:

4.16.1. 创建安全策略

您可以从命令行或控制台创建安全策略。

需要的访问权限:集群管理员

重要 :您必须定义放置和放置绑定,才能将策略应用到特定集群。PlacementBinding 资源绑定放置。为 cluster Label selector 字段输入有效值,以定义 PlacementPlacementBinding 资源。* 要使用 放置资源ManagedClusterSet 资源必须绑定到带有 ManagedClusterSetBinding 资源的 Placement 资源的命名空间。如需了解更多详细信息 ,请参阅创建 ManagedClusterSetBinding 资源

4.16.1.1. 使用命令行界面创建安全策略

完成以下步骤以从命令行创建策略:

  1. 运行以下命令来创建策略:

    oc create -f policy.yaml -n <policy-namespace>
  2. 定义策略使用的模板。通过添加 policy-templates 字段来定义模板来编辑 YAML 文件。您的策略可能类似以下 YAML 文件:

    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: policy1
    spec:
      remediationAction: "enforce" # or inform
      disabled: false # or true
      namespaceSelector:
        include:
        - "default"
        - "my-namespace"
      policy-templates:
        - objectDefinition:
            apiVersion: policy.open-cluster-management.io/v1
            kind: ConfigurationPolicy
            metadata:
              name: operator
              # namespace: # will be supplied by the controller via the namespaceSelector
            spec:
              remediationAction: "inform"
              object-templates:
              - complianceType: "musthave" # at this level, it means the role must exist and must have the following rules
                apiVersion: rbac.authorization.k8s.io/v1
                kind: Role
                metadata:
                  name: example
                objectDefinition:
                  rules:
                    - complianceType: "musthave" # at this level, it means if the role exists the rule is a musthave
                      apiGroups: ["extensions", "apps"]
                      resources: ["deployments"]
                      verbs: ["get", "list", "watch", "create", "delete","patch"]
  3. 定义一个 PlacementBinding 资源,将您的策略绑定到 放置资源。您的 PlacementBinding 资源可能类似以下 YAML 示例:

    apiVersion: policy.open-cluster-management.io/v1
    kind: PlacementBinding
    metadata:
      name: binding1
    placementRef:
      name: placement1
      apiGroup: cluster.open-cluster-management.io
      kind: Placement
    subjects:
    - name: policy1
      apiGroup: policy.open-cluster-management.io
      kind: Policy
4.16.1.1.1. 从命令行查看您的安全策略

完成以下步骤,通过 CLI 查看您的安全策略:

  1. 运行以下命令,查看具体安全策略的详情:

    oc get policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace> -o yaml
  2. 运行以下命令,查看您的安全策略的描述:

    oc describe policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace>

4.16.1.2. 从控制台创建集群安全策略

登录到 Red Hat Advanced Cluster Management 后,进入 Governance 页面并点 Create policy。从控制台创建新策略时,也会在 YAML 编辑器中创建 YAML 文件。要查看 YAML 编辑器,请在 Create policy 表单的开头选择切换来启用它。

  1. 完成 Create policy 表单,然后选择 提交按钮。您的 YAML 文件可能类似以下策略:

    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: policy-pod
      annotations:
        policy.open-cluster-management.io/categories: 'SystemAndCommunicationsProtections,SystemAndInformationIntegrity'
        policy.open-cluster-management.io/controls: 'control example'
        policy.open-cluster-management.io/standards: 'NIST,HIPAA'
        policy.open-cluster-management.io/description:
    spec:
      complianceType: musthave
      namespaces:
        exclude: ["kube*"]
        include: ["default"]
        pruneObjectBehavior: None
      object-templates:
      - complianceType: musthave
        objectDefinition:
          apiVersion: v1
          kind: Pod
          metadata:
            name: pod1
          spec:
            containers:
            - name: pod-name
              image: 'pod-image'
              ports:
              - containerPort: 80
      remediationAction: enforce
      disabled: false

    请参阅以下 PlacementBinding 示例:

    apiVersion: policy.open-cluster-management.io/v1
    kind: PlacementBinding
    metadata:
      name: binding-pod
    placementRef:
      name: placement-pod
      kind: Placement
      apiGroup: cluster.open-cluster-management.io
    subjects:
    - name: policy-pod
      kind: Policy
      apiGroup: policy.open-cluster-management.io

    请参阅以下 放置 示例:

    apiVersion: cluster.open-cluster-management.io/v1beta1
     kind: Placement
     metadata:
       name: placement-pod
    spec:
      predicates:
      - requiredClusterSelector:
          labelSelector:
            matchLabels:
              cloud: "IBM"
        tolerations:
            - key: cluster.open-cluster-management.io/unavailable
              operator: Exists
            - key: cluster.open-cluster-management.io/unreachable
              operator: Exists
  2. 可选: 为您的策略添加描述。
  3. 点击 Create Policy。从控制台创建了安全策略。
4.16.1.2.1. 从控制台查看您的安全策略

在控制台中查看任何安全策略及其状态。

  1. 进入 Governance 页面,以查看您的策略的表列表。注: 您可以选择 Policies 标签页或 Cluster violations 选项卡来过滤策略列表。
  2. 选择一个策略来查看更多详情。此时会显示 DetailsClustersTemplates 标签页。当无法决定集群或策略状态时,会显示以下信息: No status
  3. 或者,选择 Policies 选项卡来查看策略列表。展开一个策略行,以查看 Description,Standards,Controls, 和 Categories 详情。

4.16.1.3. 从命令行创建策略集

默认情况下,策略集是在没有策略或放置的情况下创建的。您必须为策略集合创建放置,并至少有一个策略存在于集群中。在创建策略集时,您可以添加多个策略。

运行以下命令从命令行创建策略集:

oc apply -f <policyset-filename>

4.16.1.4. 从控制台创建策略集

  1. 在导航菜单中选择 Governance
  2. 选择 Policy set 选项卡。
  3. 选择 Create policy set 按钮并完成表单。
  4. 添加您的策略集的详细信息,然后选择 Submit 按钮。

您的策略从 policy 表中列出。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

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

让开源更具包容性

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

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部