更新时间:2025-12-10 GMT+08:00
分享

容器集群防护策略模板说明

在使用容器集群防护功能时,您需要配置防护策略,以确保集群资源符合您的安全与合规要求。

企业主机安全提供了安全类、合规类以及镜像阻断策略模板供您使用,您在配置策略时,只需选择策略模板并配置对应的参数,即可快速完成策略配置。

本文介绍策略模板的类型、生效资源、用途以及参数等内容,并提供策略实例进行详细说明。

策略模板说明

策略类型

策略名称

生效资源类型

推荐级别

策略用途

策略参数

安全

K8sPSPPrivilegedcontainer

Pod

限制Pod启动特权容器。

exemptImages

安全

K8sPSPHostFilesystem

Pod

限制Pod允许挂载的主机host目录范围。

allowedHostPaths

安全

K8sPSPForbiddenSysctls

Pod

限制Pod禁止的Sysctl范围。

  • forbiddenSysctls
  • allowedSysctls

安全

K8sPSPCapabilities

Pod

限制Pod配置Linux Capabilities能力。

  • requiredDropCapabilities
  • exemptImages
  • allowedCapabilities

安全

K8sPSPFSGroup

Pod

限制Pod fsGroup。

  • ranges
  • rule

安全

K8sPSPHostNetworkingPorts

Pod

限制Pod使用主机网络指定端口。

  • min
  • max
  • exemptImages

安全

K8sPSPFlexVolumes

Pod

限制Pod FlexVolume驱动配置。

allowedFlexVolumes

安全

K8sPSPProcMount

Pod

限制Pod允许挂载的Proc类型。

  • procMount
  • exemptImages

安全

K8sPSPAllowedUsers

Pod

限制Pod中启动时user、group、supplementalGroups以及fsGroup配置。

  • runAsUser
  • fsGroup
  • supplementalGroups
  • exemptImages
  • runAsGroup

安全

K8sPSPSeccomp

Pod

限制Pod使用指定的Seccomp配置文件。

  • allowedProfiles
  • allowedLocalhostFiles
  • exemptImages

安全

K8sPSPReadOnlyRootFilesystem

Pod

限制Pod只能使用只读根文件系统。

exemptImages

安全

K8sPSPHostNamespace

Pod

限制Pod是否允许共享主机的host命名空间。

安全

K8sPSPSELinuxV2

Pod

限制Pod必须使用AllowedSELinuxOptions参数中规定的Selinux配置。

  • exemptImages
  • allowedSELinuxOptions

安全

K8sPSPVolumeTypes

Pod

限制Pod使用指定的Volume挂载类型。

volumes

安全

K8sPSPAppArmor

Pod

限制Pod配置AppArmor。

  • allowedProfiles
  • exemptImages

安全

K8sPSPAllowPrivilegeEscalationContainer

Pod

限制Pod配置allowPrivilegeEscalation参数。

exemptImages

合规

K8sContainerRequests

Pod

限制CPU和内存的request必须设置且小于配置的最大值。

  • memory
  • cpu
  • exemptImages

合规

K8sContainerLimits

Pod

限制容器必须设置CPU和内存limit,并且小于设定的最大值。

  • memory
  • cpu
  • exemptImages

合规

K8sRequiredLabels

Pod

要求资源包含指定的标签,其值与提供的正则表达式匹配。

  • message
  • labels

合规

K8sReplicaLimits

Deployment,ReplicaSet,CronJob

要求具有replicas字段的对象在定义的范围内。

ranges

合规

K8sBlockNodePort

Service

不允许service为NodePort类型。

镜像阻断

K8sMaliciousImagePrevent

Deployment, Pod

阻断恶意镜像。

  • 告警策略
  • 风险等级
  • 项目

K8sPSPPrivilegedcontainer

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

通过禁止PodSecurityPolicy中的“privileged”字段为true,限制Pod启动特权容器。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
  name: psp-privileged-container
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
    excludedNamespaces: ["kube-system"]

符合策略实例的资源定义:以下示例中privileged设置为false,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-privileged-allowed
  labels:
    app: nginx-privileged
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      privileged: false

不符合策略实例的资源定义:以下示例中privileged设置为true,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-privileged-allowed
  labels:
    app: nginx-privileged
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      privileged: true

K8sPSPHostFilesystem

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

allowedHostPaths:object类型参数,用于指定允许挂载的主机路径白名单。包含以下取值:
  • readOnly:boolean类型参数,用于定义主机路径是否只读。
  • pathPrefix:string类型参数,用于指定主机路径匹配的前缀。

策略用途

限制Pod允许挂载的主机host目录范围。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中的allowedHostPaths指定了允许以只读方式访问主机上以“/foo”为前缀的路径。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPHostFilesystem
metadata:
  name: psp-host-filesystem
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    allowedHostPaths:
    - readOnly: true   # 指定挂载方式为只读,防止写入主机文件系统
      pathPrefix: "/foo"   # 仅允许挂载前缀为/foo的主机路径

符合策略实例的资源定义:以下示例中hostPath中pathPrefix以/foo开头,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-filesystem
  labels:
    app: nginx-host-filesystem-disallowed
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - mountPath: /cache
          name: cache-volume
          readOnly: true
  volumes:
    - name: cache-volume
      hostPath:
        path: /foo/bar

不符合策略实例的资源定义:以下示例中hostPath中pathPrefix以/tmp开头,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-filesystem
  labels:
    app: nginx-host-filesystem-disallowed
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
      readOnly: true
  volumes:
  - name: cache-volume
    hostPath:
      path: /tmp # directory location on host

K8sPSPForbiddenSysctls

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • allowedSysctls:array类型参数,用于指定sysctls中允许使用的内核参数名称。
  • forbiddenSysctls:array类型参数,用于指定sysctls中禁止使用的内核参数名称。

策略用途

限制Pod禁止的Sysctl范围。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。parameters中的forbiddenSysctls定义了sysctls中不允许使用以kernel.开头的参数。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPForbiddenSysctls
metadata:
  name: psp-forbidden-sysctls
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    forbiddenSysctls:
    # - "*" # * 可以用来禁止所有的sysctls
    - kernel.*  # 禁止使用所有以kernel.开头的Sysctls参数

符合策略实例的资源定义:以下示例中sysctls的name符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-forbidden-sysctls-disallowed
  labels:
    app: nginx-forbidden-sysctls
spec:
  containers:
    - name: nginx
      image: nginx
  securityContext:
    sysctls:
      - name: net.core.somaxconn
        value: "1024"

不符合策略实例的资源定义:以下示例中sysctls的name(kernel.msgmax)不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-forbidden-sysctls-disallowed
  labels:
    app: nginx-forbidden-sysctls
spec:
  containers:
    - name: nginx
      image: nginx
  securityContext:
    sysctls:
      - name: kernel.msgmax
        value: "65536"
      - name: net.core.somaxconn
        value: "1024"

K8sPSPCapabilities

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • allowedCapabilities:array类型参数,用于指定允许的capabilities白名单。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。
  • requiredDropCapabilities:array类型参数,用于指定需要强制Drop的capabilities。

策略用途

限制Pod配置Linux Capabilities能力。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中定义了allowedCapabilities和requiredDropCapabilities的列表。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPCapabilities
metadata:
  name: capabilities-demo
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
    namespaces:
      - "default"
  parameters:
    allowedCapabilities: ["CHOWN"]  # 允许Pod启用的内核能力列表,未列出的默认禁止
    requiredDropCapabilities: ["NET_ADMIN"]  # 必须被Pod丢弃的内核能力列表,无论是否声明

符合策略实例的资源定义:以下示例中capabilities中的各项参数符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-allowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      securityContext:
        capabilities:
          add: ["CHOWN"]
          drop: ["NET_ADMIN", "SYS_ADMIN"]
      resources:
        limits:
          cpu: "100m"
          memory: "30Mi"

不符合策略实例的资源定义:以下示例中capabilities中的参数不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-disallowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      securityContext:
        capabilities:
          add: ["NET_RAW"]
      resources:
        limits:
          cpu: "100m"
          memory: "30Mi"

K8sPSPFSGroup

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • ranges:object类型参数,用于指定允许Pod内容器访问挂载卷时可使用的fsGroup ID,包含以下取值:
    • min:Integer类型参数,fsGroup ID的最小值。
    • max:Integer类型参数,fsGroup ID的最大值。
  • rule:string类型参数,支持MayRunAs、MustRunAs和RunAsAny三种类型。

策略用途

限制Pod fsGroup。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,允许使用的fsGroup范围为1-1000。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFSGroup
metadata:
  name: psp-fsgroup
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    rule: "MayRunAs" #"MustRunAs" #"MayRunAs", "RunAsAny"
    ranges:
    - min: 1
      max: 1000

符合策略实例的资源定义:以下示例中fsGroup设为了500,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: fsgroup-disallowed
spec:
  securityContext:
    fsGroup: 500 # 目录将拥有ID为500的fsGroup
  volumes:
    - name: fsgroup-demo-vol
      emptyDir: {}
  containers:
    - name: fsgroup-demo
      image: busybox
      command: ["sh", "-c", "sleep 1h"]
      volumeMounts:
        - name: fsgroup-demo-vol
          mountPath: /data/demo

不符合策略实例的资源定义:以下示例中fsGroup设为了2000,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: fsgroup-disallowed
spec:
  securityContext:
    fsGroup: 2000   # 目录将拥有ID为2000的fsGroup
  volumes:
  - name: fsgroup-demo-vol
    emptyDir: {}
  containers:
  - name: fsgroup-demo
    image: busybox
    command: [ "sh", "-c", "sleep 1h" ]
    volumeMounts:
    - name: fsgroup-demo-vol
      mountPath: /data/demo

K8sPSPHostNetworkingPorts

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。
  • hostNetwork:boolean类型参数,用于指定允许Pod使用的主机网络端口。包含以下取值:
    • min:Integer类型参数,hostPort最小值。
    • max:Integer类型参数,hostPort最大值。

策略用途

限制Pod使用主机网络指定端口。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中hostNetwork为true时,使用的端口必须在指定的端口范围(80-9000)内。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPHostNetworkingPorts
metadata:
  name: psp-host-network-ports
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    hostNetwork: bool
    min: 80
    max: 9000

符合策略实例的资源定义:以下示例中hostNetwork设置成了false,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-networking-ports-allowed
  labels:
    app: nginx-host-networking-ports
spec:
  hostNetwork: false
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 9000
      hostPort: 80

不符合策略实例的资源定义:以下示例中hostNetwork设置成了true,但是端口9001未在指定范围内,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-networking-ports-disallowed
  labels:
    app: nginx-host-networking-ports
spec:
  hostNetwork: true
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 9001
      hostPort: 9001

K8sPSPFlexVolumes

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

allowedFlexVolumes:array类型参数,用于指定允许配置的FlexVolume驱动列表。

策略用途

限制Pod FlexVolume驱动配置

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中的allowedFlexVolumes字段定义了允许的driver类型列表。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPFlexVolumes
metadata:
  name: psp-flexvolume-drivers
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    allowedFlexVolumes: #[]
    - driver: "example/lvm" # 指定允许使用的FlexVolume驱动类型,此处为LVM示例
    - driver: "example/cifs" # 指定允许使用的FlexVolume驱动类型,此处为CIFS示例

符合策略实例的资源定义:以下示例中flexVolume中的类型在上述定义的允许范围内,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-flexvolume-driver-allowed
  labels:
    app: nginx-flexvolume-driver
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /test
      name: test-volume
      readOnly: true
  volumes:
  - name: test-volume
    flexVolume:
      driver: "example/lvm"

不符合策略实例的资源定义:以下示例中flexVolume中的类型不在上述定义的允许范围内,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-flexvolume-driver-disallowed
  labels:
    app: nginx-flexvolume-driver
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /test
      name: test-volume
      readOnly: true
  volumes:
  - name: test-volume
    flexVolume:
      driver: "example/testdriver" #"example/lvm"

K8sPSPProcMount

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。
  • procMount:string类型参数,用于限制Pod允许挂载的Proc类型。允许配置如下类型:
    • Default:默认屏蔽挂载/proc目录。
    • Unmasked:不屏蔽挂载/proc。

策略用途

限制Pod允许挂载的Proc类型。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中指定了procMount的值为Default,默认屏蔽挂载/proc目录。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPProcMount
metadata:
  name: psp-proc-mount
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    procMount: Default

符合策略实例的资源定义:以下示例中securityContext字段中的procMount为Default,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-proc-mount-disallowed
  labels:
    app: nginx-proc-mount
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      procMount: Default

不符合策略实例的资源定义:以下示例中securityContext字段中的procMount为Unmasked,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-proc-mount-disallowed
  labels:
    app: nginx-proc-mount
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      procMount: Unmasked

K8sPSPAllowedUsers

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • runAsUser:object类型参数,用于指定容器以什么用户身份运行。
    • ranges:object类型参数,用于指定允许使用的用户ID(UID)范围,包含以下取值:
      • min:Integer类型参数,用户ID的最小值。
      • max:Integer类型参数,用户ID的最大值。
    • rule:string类型参数,支持MayRunAs、MustRunAs和RunAsAny三种类型。
  • runAsGroup:object类型参数,用于指定容器以什么用户组身份运行。
    • ranges:object类型参数,用于指定允许使用的用户组ID(GID)范围,包含以下取值:
      • min:Integer类型参数,用户组ID的最小值。
      • max:Integer类型参数,用户组ID的最大值。
    • rule:string类型参数,支持MayRunAs、MustRunAs和RunAsAny三种类型。
  • fsGroup:object类型参数,
    • ranges:object类型参数,用于指定允许容器访问挂载卷时可使用的fsGroup ID,包含以下取值:
      • min:Integer类型参数,fsGroup ID的最小值。
      • max:Integer类型参数,fsGroup ID的最大值。
    • rule:string类型参数,支持MayRunAs、MustRunAs和RunAsAny三种类型。
  • supplementalGroups:object类型参数,用于指定为容器额外添加的用户组身份。
    • ranges:object类型参数,用于指定额外添加的用户组ID(GID)范围,包含以下取值:
      • min:Integer类型参数,用户组ID的最小值。
      • max:Integer类型参数,用户组ID的最大值。
    • rule:string类型参数,支持MayRunAs、MustRunAs和RunAsAny三种类型。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod中启动时user、group、supplementalGroups以及fsGroup配置。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中定义了对runAsUser、runAsGroup、supplementalGroups和fsGroup等字段的约束。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowedUsers
metadata:
  name: psp-pods-allowed-user-ranges
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    runAsUser:  # 控制容器必须使用ID为100-200的UID运行
      rule: MustRunAs # MustRunAsNonRoot # RunAsAny 
      ranges:
        - min: 100
          max: 200
    runAsGroup:  # 控制容器必须使用ID为100-200的GID运行
      rule: MustRunAs # MayRunAs # RunAsAny 
      ranges:
        - min: 100
          max: 200
    supplementalGroups:  # 控制容器必须使用ID为100-200的补充组
      rule: MustRunAs # MayRunAs # RunAsAny 
      ranges:
        - min: 100
          max: 200
    fsGroup:  # 控制容器必须使用ID为100-200的fsGroup
      rule: MustRunAs # MayRunAs # RunAsAny 
      ranges:
        - min: 100
          max: 200

符合策略实例的资源定义:以下示例中runAsUser等参数均在范围内,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-users-allowed
  labels:
    app: nginx-users
spec:
  securityContext:
    supplementalGroups:
      - 199
    fsGroup: 199
  containers:
    - name: nginx
      image: nginx
      securityContext:
        runAsUser: 199
        runAsGroup: 199

不符合策略实例的资源定义:以下示例中runAsUser等参数不在范围内,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-users-disallowed
  labels:
    app: nginx-users
spec:
  securityContext:
    supplementalGroups:
      - 250
    fsGroup: 250
  containers:
    - name: nginx
      image: nginx
      securityContext:
        runAsUser: 250
        runAsGroup: 250

K8sPSPSeccomp

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • allowedLocalhostFiles:array类型参数,用于指定允许使用的本地主机文件
  • allowedProfiles:array类型参数,用于指定允许使用的Seccomp配置文件。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod使用指定的Seccomp配置文件。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中allowedProfiles明确注解了可以使用的Seccomp配置文件范围。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPSeccomp
metadata:
  name: psp-seccomp
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    allowedProfiles:
    - runtime/default # 允许使用运行时默认的Seccomp配置文件,通常是容器运行时(如 containerd 或 CRI-O)提供的默认配置
    - docker/default  # 允许使用 Docker 默认的Seccomp配置文件,该配置文件限制了某些系统调用,以增强容器的安全性

符合策略实例的资源定义:以下示例中container.seccomp.security.alpha.kubernetes.io/nginx注解的value在设定的值列表中,符合策略定义。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-seccomp-allowed
  annotations:
    container.seccomp.security.alpha.kubernetes.io/nginx: runtime/default
  labels:
    app: nginx-seccomp
spec:
  containers:
  - name: nginx
    image: nginx

不符合策略实例的资源定义:以下示例中container.seccomp.security.alpha.kubernetes.io/nginx注解的value没在设定的值列表中,不符合策略定义。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-seccomp-disallowed
  annotations:
    container.seccomp.security.alpha.kubernetes.io/nginx: unconfined
  labels:
    app: nginx-seccomp
spec:
  containers:
  - name: nginx
    image: nginx

K8sPSPReadOnlyRootFilesystem

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod只能使用只读根文件系统

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPReadOnlyRootFilesystem
metadata:
  name: psp-readonlyrootfilesystem
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]

符合策略实例的资源定义:以下示例中readOnlyRootFilesystem字段为true,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-readonlyrootfilesystem-allowed
  labels:
    app: nginx-readonlyrootfilesystem
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      readOnlyRootFilesystem: true

不符合策略实例的资源定义:以下示例中readOnlyRootFilesystem字段为false,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-readonlyrootfilesystem-disallowed
  labels:
    app: nginx-readonlyrootfilesystem
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      readOnlyRootFilesystem: false

K8sPSPHostNamespace

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

策略用途

限制Pod共享主机的host命名空间。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPHostNamespace
metadata:
  name: psp-host-namespace
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]

符合策略实例的资源定义:以下示例中hostPID和hostIPC均为false,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-namespace-allowed
  labels:
    app: nginx-host-namespace
spec:
  hostPID: false
  hostIPC: false
  containers:
  - name: nginx
    image: nginx

不符合策略实例的资源定义:以下示例中hostPID和hostIPC均为true,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host-namespace-disallowed
  labels:
    app: nginx-host-namespace
spec:
  hostPID: true
  hostIPC: true
  containers:
  - name: nginx
    image: nginx

K8sPSPSELinuxV2

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • allowedSELinuxOptions:object类型参数,用于指定SELinux配置白名单,包含level、role、type、user四个字符串取值对象。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod必须使用AllowedSELinuxOptions参数中规定的Selinux配置。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中的allowedSELinuxOptions定义了SELinux配置白名单。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPSELinuxV2
metadata:
  name: psp-selinux-v2
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    allowedSELinuxOptions:
      - level: s0:c123,c456  # 定义允许敏感等级为s0:c123,c456 
        role: object_r  # 定义允许使用object_r角色
        type: svirt_sandbox_file_t  # 定义允许类型为svirt_sandbox_file_t 
        user: system_u  # 定义允许用户为ystem_u

符合策略实例的资源定义:以下示例中seLinuxoptions参数均在白名单中,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
    name: nginx-selinux-allowed
    labels:
        app: nginx-selinux
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      seLinuxOptions:
        level: s0:c123,c456
        role: object_r
        type: svirt_sandbox_file_t
        user: system_u

不符合策略实例的资源定义:以下示例中seLinuxOptions参数不在白名单中,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
    name: nginx-selinux-disallowed
    labels:
        app: nginx-selinux
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      seLinuxOptions:
        level: s1:c234,c567
        user: sysadm_u
        role: sysadm_r
        type: svirt_lxc_net_t

K8sPSPVolumeTypes

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

volumes:array类型参数,用于指定允许使用的volume挂载类型列表。

策略用途

限制Pod使用指定的Volume挂载类型。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters的volumes字段定义了允许的类型列表。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPVolumeTypes
metadata:
  name: psp-volume-types
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    volumes:
    # - "*" # * may be used to allow all volume types
    - configMap    # 允许使用ConfigMap类型的卷
    - emptyDir     # 允许使用emptyDir类型的卷
    - projected    # 允许使用projected类型的卷
    - secret       # 允许使用Secret 类型的卷
    - downwardAPI  # 允许使用DownwardAPI类型的卷
    - persistentVolumeClaim   # 允许使用PersistentVolumeClaim类型的卷
    #- hostPath #required for allowedHostPaths
    - flexVolume #required for allowedFlexVolumes  # 允许使用flexVolume

符合策略实例的资源定义:以下示例中volumes中的类型均在上述定义的允许范围内,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-volume-types-allowed
  labels:
    app: nginx-volume-types
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - name: nginx2
    image: nginx
    volumeMounts:
    - mountPath: /cache2
      name: demo-vol
  volumes:
  - name: cache-volume
    emptyDir: {}
  - name: demo-vol
    emptyDir: {}

不符合策略实例的资源定义:以下示例中volumes中的类型(hostPath)不在上述定义的允许范围内,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-volume-types-disallowed
  labels:
    app: nginx-volume-types
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - name: nginx2
    image: nginx
    volumeMounts:
    - mountPath: /cache2
      name: demo-vol
  volumes:
  - name: cache-volume
    hostPath:
      path: /tmp # directory location on host
  - name: demo-vol
    emptyDir: {}

K8sPSPAppArmor

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

  • allowedProfiles:array类型参数,用于指定允许使用的AppArmor配置文件。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod配置AppArmor。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters的allowedProfiles字段定义了允许的值列表。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAppArmor
metadata:
  name: psp-apparmor
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    allowedProfiles:
    - runtime/default  # 允许使用AppArmor的默认配置文件

符合策略实例的资源定义:以下示例中apparmor的值在上述定义的允许范围内,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-apparmor-allowed
  annotations:
    # apparmor.security.beta.kubernetes.io/pod: unconfined # runtime/default
    container.apparmor.security.beta.kubernetes.io/nginx: runtime/default
  labels:
    app: nginx-apparmor
spec:
  containers:
  - name: nginx
    image: nginx

不符合策略实例的资源定义:以下示例中privileged设置为true,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-apparmor-disallowed
  annotations:
    # apparmor.security.beta.kubernetes.io/pod: unconfined # runtime/default
    container.apparmor.security.beta.kubernetes.io/nginx: unconfined
  labels:
    app: nginx-apparmor
spec:
  containers:
  - name: nginx
    image: nginx

K8sPSPAllowPrivilegeEscalationContainer

基本信息

策略类型

安全

推荐级别

生效资源类型

Pod

策略参数

exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制Pod配置allowPrivilegeEscalation参数。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowPrivilegeEscalationContainer
metadata:
  name: psp-allow-privilege-escalation-container
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]

符合策略实例的资源定义:以下示例中allowPrivilegeEscalation的值为false,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-privilege-escalation-allowed
  labels:
    app: nginx-privilege-escalation
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      allowPrivilegeEscalation: false

不符合策略实例的资源定义:以下示例中allowPrivilegeEscalation的值不为true,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: nginx-privilege-escalation-disallowed
  labels:
    app: nginx-privilege-escalation
spec:
  containers:
  - name: nginx
    image: nginx
    securityContext:
      allowPrivilegeEscalation: true

K8sContainerRequests

基本信息

策略类型

合规

推荐级别

生效资源类型

Pod

策略参数

  • cpu:string类型参数,用于指定容器CPU请求的最大值,最大值以下符合要求,不含最大值。
  • memory:string类型参数,用于指定容器内存请求的最大值,最大值以下符合要求,不含最大值。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制CPU和内存的request必须设置且小于配置的最大值

示例

策略实例:以下策略实例配置了CPU和内存的最大Request限制,其中CPU最大Request为200m,内存最大Request为1Gi。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sContainerRequests
metadata:
  name: container-must-have-requests
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    cpu: "200m"
    memory: "1Gi"

符合策略实例的资源定义:以下示例中CPU和内存的Request小于配置的最大值,符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-allowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      resources:
        requests:
          cpu: "100m"
          memory: "1Gi"

不符合策略实例的资源定义:以下示例中内存的Request大于约束的最大值,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-disallowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      resources:
        requests:
          cpu: "100m"
          memory: "2Gi"

K8sContainerLimits

基本信息

策略类型

合规

推荐级别

生效资源类型

Pod

策略参数

  • cpu:string类型参数,用于指定容器CPU最大值,最大值以下符合要求,不含最大值。
  • memory:string类型参数,用于指定容器内存最大值,最大值以下符合要求,不含最大值。
  • exemptImages:array of strings类型参数,用于排除无需管控的镜像列表。

策略用途

限制容器必须设置CPU和内存limit,并且小于设定的最大值

示例

策略实例:以下策略实例展示了匹配的对象的CPU最大为200m,内存最大为1G。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sContainerLimits
metadata:
  name: container-must-have-limits
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Pod"]
  parameters:
    cpu: "200m"
    memory: "1Gi"

符合策略实例的资源定义:以下示例中CPU和内存的Limit符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-allowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      resources:
        limits:
          cpu: "100m"
          memory: "1Gi"

不符合策略实例的资源定义:以下示例中内存Limit超过最大值,不符合策略实例。

apiVersion: v1
kind: Pod
metadata:
  name: opa-allowed
  labels:
    owner: me.agilebank.demo
spec:
  containers:
    - name: opa
      image: openpolicyagent/opa:0.9.2
      args:
        - "run"
        - "--server"
        - "--addr=localhost:8080"
      resources:
        limits:
          cpu: "300m"
          memory: "1Gi"

K8sRequiredLabels

基本信息

策略类型

合规

推荐级别

生效资源类型

Pod

策略参数

  • message:string类型参数,当资源创建或更新时,如果不满足本策略的标签限制,给出的提示信息。
  • labels:array类型参数,用于指定标签。
    • allowedRegex:string类型参数,Label白名单的正则表达式。
    • key:string类型参数,需提供匹配allowedRegex的值。

策略用途

要求资源包含指定的标签,其值与提供的正则表达式匹配。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters中指定了提示信息message以及label的约束定义。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
  name: all-must-have-owner
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Namespace"]
  parameters:
    message: "All namespaces must have an `owner` label that points to your company username"
    labels:
      - key: owner  # 定义必须存在的label键为owner
        allowedRegex: "^[a-zA-Z]+.agilebank.demo$"  # 限制label值的格式,必须符合该正则表达式

符合策略实例的资源定义:以下示例中包含策略实例中定义的label,符合策略实例。

apiVersion: v1
kind: Namespace
metadata:
  name: allowed-namespace
  labels:
    owner: user.agilebank.demo

不符合策略实例的资源定义:以下示例中不包含策略实例中定义的label,不符合策略实例。

apiVersion: v1
kind: Namespace
metadata:
  name: disallowed-namespace

K8sReplicaLimits

基本信息

策略类型

合规

推荐级别

生效资源类型

Deployment,ReplicaSet,CronJob

策略参数

ranges:object类型参数,用于定义replicas字段的对象范围。包含以下取值:

  • min_replicas:integer类型参数,replicas最小值。
  • max_replicas:integer类型参数,replicas最大值。

策略用途

要求具有replicas字段的对象在定义的范围内。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型,parameters定义replicas范围为3到50。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sReplicaLimits
metadata:
  name: replica-limits
spec:
  match:
    kinds:
      - apiGroups: ["apps"]
        kinds: ["Deployment"]
  parameters:
    ranges:
    - min_replicas: 3  # 指定副本数的最小值为3
      max_replicas: 50  # 指定副本数的最大值为50

符合策略实例的资源定义:以下示例中Replicas设为了3,符合策略实例。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: allowed-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

不符合策略实例的资源定义:以下示例中Replicas设为了100,不符合策略实例。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: disallowed-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 100
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

K8sBlockNodePort

基本信息

策略类型

合规

推荐级别

生效资源类型

Service

策略参数

策略用途

不允许service为NodePort类型。

示例

策略实例:以下策略实例展示了策略定义生效的资源类型。

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sBlockNodePort
metadata:
  name: block-node-port
spec:
  match:
    kinds:
      - apiGroups: [""]
        kinds: ["Service"]

符合策略实例的资源定义:以下示例中Servie类型非Nodeport,符合策略实例。

apiVersion: v1
kind: Service
metadata:
  name: my-service-disallowed
spec:
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30007

不符合策略实例的资源定义:以下示例中privileged设置为true,不符合策略实例。

apiVersion: v1
kind: Service
metadata:
  name: my-service-disallowed
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30007

K8sMaliciousImagePrevent

基本信息

策略类型

镜像阻断

推荐级别

生效资源类型

Deployment, Pod

策略参数

  • 是否拦截未扫描镜像:开启后,将自动拦截未经过安全扫描的镜像。镜像安全扫描请参见容器镜像安全概述
  • 告警策略:选择告警的风险类型,可选基线、漏洞、恶意脚本。
  • 风险等级:选择告警的风险等级,可选高危、中危、低危。
  • 项目:当告警策略选择基线或漏洞时,可选项目。漏洞项目为漏洞编号,支持多选。基线项目为基线检查规则,支持多选。

策略用途

阻断恶意镜像。

示例

无,配置了镜像阻断策略后,企业主机安全将依据容器镜像安全扫描结果,对您指定的集群/镜像名称/标签所对应的镜像实施告警/阻断/放行的防护动作。

相关文档