Kubernetes Pod Security Admission 迁移策略与违规报错排查
分析 Kubernetes 废弃 PSP 后转向 Pod Security Admission (PSA) 时遇到的 baseline 与 restricted 级别违规报错,并提供准入标签配置与 PodSpec 合规改造指南。
在 Kubernetes 1.25+ 彻底移除 PodSecurityPolicy (PSP) 之后,官方内置的 Pod Security Admission (PSA) 成为标配准入控制器。当集群管理员为命名空间开启了 pod-security.kubernetes.io/enforce: restricted 或 baseline 策略后,许多历史 Deployment、StatefulSet 在更新或滚动升级时,会出现 Error creating: pods ... is forbidden: violates PodSecurity "restricted:latest" 的拦截错误。
典型报错日志
使用 kubectl apply 或执行 Deployment 升级时,控制台直接报出禁止创建 Pod 的准入拒绝:
Error from server (Forbidden): error when creating "deployment.yaml":
pods "payment-service-58d7c49b6b-q7w8x" is forbidden: violates PodSecurity "restricted:latest":
allowPrivilegeEscalation != false (container "payment-app" must set securityContext.allowPrivilegeEscalation=false),
unrestricted capabilities (container "payment-app" must set securityContext.capabilities.drop=["ALL"]),
runAsNonRoot != true (pod or container "payment-app" must set securityContext.runAsNonRoot=true),
runAsUser=0 (container "payment-app" must not set runAsUser=0)
此时 ReplicaSet 无法创建新的 Pod,工作负载陷入 0/1 副本暂停状态。
Pod 安全标准的三个级别对比
Kubernetes PSA 定义了三种等级:
- privileged:完全无限制,允许任何特权容器和主机级访问。
- baseline:对已知的特权提升进行最小限度防御,限制运行 HostPort、HostNetwork、HostPID 等,但仍允许容器以 root 用户启动。
- restricted:遵循深度防御最佳安全实践,严格限制必须以非 root 身份运行、禁止特权提升、显式丢弃所有 Linux Capabilities、限制可挂载的文件卷类型。
根因与排查流程
步骤一:查看命名空间当前的 PSA 策略标签
执行命令检查目标 Namespace 的准入模式:
kubectl get ns production --show-labels
输出通常包含以下三种维度的标签:
enforce:拦截违规创建。audit:记录违规审计日志但不拦截。warn:在命令返回中显示警告提示但不拦截。
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/audit: restricted
步骤二:使用 warn 和 audit 模式平滑过渡
直接将生产环境设置为 enforce: restricted 极易导致业务中断。建议优先配置 enforce: baseline,并将 warn 和 audit 设置为 restricted:
kubectl label --overwrite ns production pod-security.kubernetes.io/enforce=baseline pod-security.kubernetes.io/enforce-version=latest pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/warn-version=latest
这样业务部署不会被立即阻断,但研发人员在执行 CI/CD 或 kubectl apply 时能直观看到需要整改的安全规范警告信息。
步骤三:整改 PodSpec 安全上下文 (SecurityContext)
针对 restricted 策略的强制规范,在 Deployment 或 StatefulSet 的 YAML 中补全容器和 Pod 级别的安全配置:
apiVersion: apps/v1
kind: Deployment
metadata:
name: payment-service
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: payment-service
template:
metadata:
labels:
app: payment-service
spec:
# 1. Pod 级别安全限制:以非 root 用户组运行
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: payment-app
image: harbor.example.internal/apps/payment-app:v1.4.2
# 2. 容器级别限制:禁止特权提升,丢弃全量 Linux 权能,只读根文件系统
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
volumeMounts:
- name: tmp-dir
mountPath: /tmp
volumes:
- name: tmp-dir
emptyDir: {}
步骤四:处理无法满足 restricted 的第三方 DaemonSet
对于监控代理(如 Datadog Agent、Prometheus Node Exporter)或 CNI 插件(如 Calico、Cilium),它们必须访问主机命名空间和特权设备。对此类专属系统组件,切勿将整个业务 Namespace 降级,而是将这些组件部署在专属的 kube-system 或独立管理的运维命名空间中,保持业务命名空间的 restricted 强安全隔离。


