bigywater.tech
Kubernetes · 89

Pod Security 准入:三种级别与模式

Pod Security 准入怎么用?baseline 和 restricted 各拒绝什么?warn / audit / enforce 有什么区别?为什么 Deployment 创建成功但 Pod 起不来?给已有命名空间加标签会误杀吗?这篇在 5 个命名空间里把这些情形逐个跑出来。

4 条一起拒
restricted 对一个普通 Pod 同时指出的违规项数
Deployment 不被拦
检查在创建 Pod 时,不是创建 Deployment 时;看 ReplicaSet 的 FailedCreate 事件
旧 Pod 不动
加 enforce 标签只对之后的创建/更新生效,存量 Pod 继续运行

baseline:挡住已知的提权

实测 · 本机执行输出
  普通 Pod(什么都不设):退出码 0;['pod/ok created']
  privileged: true:退出码 1;['Error from server (Forbidden): error when creating "STDIN": pods "priv" is forbidden: violates PodSecurity "baseline:latest": privileged (container "c" must not set securityContext.privileged=true)']
  hostNetwork: true:退出码 1;['Error from server (Forbidden): error when creating "STDIN": pods "hostnet" is forbidden: violates PodSecurity "baseline:latest": host namespaces (hostNetwork=true)']
  hostPath 卷:退出码 1;['Error from server (Forbidden): error when creating "STDIN": pods "hostpath" is forbidden: violates PodSecurity "baseline:latest": hostPath volumes (volume "h")']
  add NET_ADMIN:退出码 1;['Error from server (Forbidden): error when creating "STDIN": pods "caps" is forbidden: violates PodSecurity "baseline:latest": non-default capabilities (container "c" must not include "NET_ADMIN" in securityContext.capabilities.add)']
  add NET_BIND_SERVICE(baseline 允许的少数能力之一):退出码 0;['pod/caps2 created']

命名空间 ps-base 带 pod-security.kubernetes.io/enforce=baseline:什么都不设的普通 Pod 通过;privileged: true 被拒(violates PodSecurity "baseline:latest": privileged (container "c" must not set securityContext.privileged=true));hostNetwork 被拒(host namespaces (hostNetwork=true));hostPath 卷被拒;add: [NET_ADMIN] 被拒(non-default capabilities);add: [NET_BIND_SERVICE] 却能通过——baseline 允许少数默认之外的能力。

restricted:一次列出全部违规

实测 · 本机执行输出
  普通 Pod:退出码 1
    Error from server (Forbidden): error when creating "STDIN": pods "plain" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "c" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "c" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "c" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "c" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
  补齐 allowPrivilegeEscalation=false / drop ALL / runAsNonRoot / seccomp RuntimeDefault:退出码 0;['pod/good created']
  去掉 runAsUser(镜像是 root,但 runAsNonRoot=true 声明了):退出码 0(准入只看字段,不看镜像;运行时 kubelet 才会拒绝)

enforce=restricted 下同一个普通 Pod 一次被指出 4 条:allowPrivilegeEscalation != false、unrestricted capabilities(要 drop ALL)、runAsNonRoot != true、seccompProfile(要 RuntimeDefault 或 Localhost)。补齐这 4 项后创建成功。去掉 runAsUser 但保留 runAsNonRoot: true(镜像其实是 root)也通过:准入只看字段,不看镜像,真正的拒绝要等 kubelet(CreateContainerConfigError,见上一篇)。

warn 与控制器

实测 · 本机执行输出
  warn=restricted + enforce=baseline,创建普通 Pod:退出码 0;
    pod/w1 created
    Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "c" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "c" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "c" must set securityContext.runAs
  enforce=restricted 的命名空间里创建不合规的 Deployment:退出码 0;['deployment.apps/dep created', 'Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "c" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "c" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "c" must set securityContext.runAs']
  Deployment 本身创建成功,但 readyReplicas=0;ReplicaSet 的 conditions:[('ReplicaFailure', 'FailedCreate', 'pods "dep-7798578f69-49jdw" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "c" must set security')]
  事件:['5s Warning FailedCreate replicaset/dep-7798578f69 Error creating: pods "dep-7798578f69-xq4rv" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "c" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "c" must set securityContext.capabil']

warn=restricted + enforce=baseline 的命名空间创建普通 Pod:退出码 0、pod/w1 created,同时返回 Warning: would violate PodSecurity "restricted:latest": …。在 enforce=restricted 的命名空间创建不合规的 Deployment:Deployment 本身创建成功(只带 Warning),但 readyReplicas=0,ReplicaSet 的 ReplicaFailure=FailedCreate,事件是 pods "dep-…" is forbidden: violates PodSecurity "restricted:latest"——拒绝发生在 ReplicaSet 创建 Pod 的那一刻,要去 ReplicaSet 的事件里找。

给已有命名空间加标签

实测 · 本机执行输出
  --dry-run=server 加 enforce=baseline:退出码 0;['namespace/ps-late labeled (server dry run)', 'Warning: existing pods in namespace "ps-late" violate the new PodSecurity enforce level "baseline:latest"', 'Warning: old: privileged']
  真正加上标签:退出码 0;['namespace/ps-late labeled', 'Warning: existing pods in namespace "ps-late" violate the new PodSecurity enforce level "baseline:latest"']
  已经在运行的 privileged Pod 老 old:phase=Running(不受影响,只对之后创建/更新 Pod 的请求检查)
  再创建一个 privileged 的 new:退出码 1;['Error from server (Forbidden): error when creating "STDIN": pods "new" is forbidden: violates PodSecurity "baseline:latest": privileged (container "c" must not set securityContext.privileged=true)']

先在 ps-late 里创建 privileged 的 Pod old,再加 enforce=baseline:--dry-run=server 会输出 Warning: existing pods in namespace "ps-late" violate the new PodSecurity enforce level "baseline:latest" 并列出 old: privileged;真正加上标签也只给 Warning,old 仍是 Running;之后再创建 privileged 的 new 才被拒。所以推广 Pod Security 的稳妥顺序是:先 dry-run 看存量违规,再 warn / audit,最后 enforce。

分步演示

Pod Security
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / kind(Docker 里的单节点或多节点真实集群,节点是 Docker Desktop 虚拟机里的容器)/ kubectl;用空的 kubeconfig 之外的独立 kubeconfig,不碰用户自己的集群;计时用 date +%s%N / kubectl 事件时间戳,数字每次运行会有小幅抖动。三节点 kind 集群 kx-main(Kubernetes v1.37.0,PodSecurity 准入默认开启);用 kubectl apply 创建 Pod / Deployment 并读返回信息(包括 Warning 头);级别版本都是 latest;每种情形各一次
    • 没有验证:audit 模式写入的审计注解(需要审计日志,见 k18);enforce-version 固定到旧版本的差别;AdmissionConfiguration 里的 exemptions(按用户名、命名空间、RuntimeClass 豁免);privileged 级别;Pod Security Standards 对 Windows Pod 的规则;ValidatingAdmissionPolicy 实现同类约束(下面一篇)
    京ICP备2026067502号-1京公网安备11010502063487号