关于 implementing-kubernetes-pod-security-standards
这项 Kubernetes 安全技能帮助团队为工作负载选择和应用 Pod Security Standards,可评估策略级别、发现违规、准备合规 Pod 规范,并规划渐进式强制执行或 PodSecurityPolicy 迁移。
主要功能
- 01比较 Privileged、Baseline 和 Restricted
- 02评估现有工作负载违规
- 03创建符合要求的 Pod 规范
- 04规划渐进式策略迁移
- 05通过服务端试运行测试强制执行
适用场景
- 01为各命名空间选择安全配置级别
- 02找出强制 Restricted 后可能失败的工作负载
- 03调整 Pod 安全上下文以满足 Restricted
- 04准备 PodSecurityPolicy 迁移
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill implementing-kubernetes-pod-security-standards安装后在 Claude Code 中输入/implementing-kubernetes-pod-security-standards确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
实施 Kubernetes Pod 安全标准
概述
Pod 安全标准(PSS)定义了三个安全策略级别——Privileged、Baseline 和 Restricted,由 Kubernetes 1.25+ 内置的 Pod 安全准入(PSA)控制器强制执行。PSA 取代了已弃用的 PodSecurityPolicy,并提供命名空间级别的强制执行,支持三种模式:enforce、audit 和 warn。
适用场景
- 判断命名空间或工作负载应属于 Privileged、Baseline 还是 Restricted 级别
- 审计如果今天开始强制执行 Restricted,哪些现有工作负载会被拒绝
- 将“必须满足 Restricted”的要求转化为具体的
securityContext修改 - 规划 PodSecurityPolicy 迁移,并预判可能出现的问题
- 将 Pod 安全态势映射到合规控制项(NIST PR.PS-01、CIS Kubernetes)
本技能不涉及: 配置强制执行这些配置文件的控制器——命名空间标签、
AdmissionConfiguration、豁免项,或调试被 PSA 拒绝的 Pod。请使用
implementing-pod-security-admission-controller。
前提条件
- Kubernetes 集群 1.25+(PSA 已正式发布)
- 已配置 kubectl,且拥有 cluster-admin 访问权限
- 了解 Linux 功能和安全上下文
核心概念
三种安全配置文件
| 配置文件 | 用途 | 限制 |
|---|---|---|
| Privileged | 不受限制,适用于系统工作负载 | 无 |
| Baseline | 防止已知的权限提升 | 不允许使用 hostNetwork、hostPID、hostIPC、特权容器和危险功能 |
| Restricted | 强化的最佳实践 | 非 root、移除所有功能、必须配置 seccomp、建议使用只读根文件系统 |
三种强制执行模式
| 模式 | 行为 |
|---|---|
| enforce | 拒绝违反策略的 Pod |
| audit | 在审计日志中记录违规情况,但允许 Pod 创建 |
| warn | 向用户返回警告,但允许 Pod 创建 |
工作流
步骤 1:为命名空间添加 PSA 标签
# Restricted namespace - production workloads
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/audit: restricted
pod-security.kubernetes.io/audit-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: latest
# Baseline namespace - general workloads
apiVersion: v1
kind: Namespace
metadata:
name: staging
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/audit-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: latest
# Privileged namespace - system components only
apiVersion: v1
kind: Namespace
metadata:
name: kube-system
labels:
pod-security.kubernetes.io/enforce: privileged
pod-security.kubernetes.io/enforce-version: latest
步骤 2:为现有命名空间添加标签
# Apply restricted enforcement to production
kubectl label namespace production \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/audit=restricted \
pod-security.kubernetes.io/warn=restricted \
--overwrite
# Apply baseline to staging with restricted warnings
kubectl label namespace staging \
pod-security.kubernetes.io/enforce=baseline \
pod-security.kubernetes.io/audit=restricted \
pod-security.kubernetes.io/warn=restricted \
--overwrite
# Check labels on all namespaces
kubectl get namespaces -L pod-security.kubernetes.io/enforce
步骤 3:创建符合要求的 Pod 规范
# Restricted-compliant deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: secure-app
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: secure-app
template:
metadata:
labels:
app: secure-app
spec:
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 65534
runAsGroup: 65534
fsGroup: 65534
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: myregistry.com/myapp:v1.0.0@sha256:abc123
ports:
- containerPort: 8080
protocol: TCP
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
runAsNonRoot: true
runAsUser: 65534
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
volumeMounts:
- name: tmp
mountPath: /tmp
- name: cache
mountPath: /var/cache
volumes:
- name: tmp
emptyDir:
sizeLimit: 100Mi
- name: cache
emptyDir:
sizeLimit: 50Mi
步骤 4:渐进式迁移策略
# Phase 1: Audit mode - discover violations without blocking
kubectl label namespace my-namespace \
pod-security.kubernetes.io/audit=restricted \
pod-security.kubernetes.io/warn=restricted
# Check audit logs for violations
kubectl logs -n kube-system -l component=kube-apiserver | grep "pod-security"
# Phase 2: Enforce baseline, warn on restricted
kubectl label namespace my-namespace \
pod-security.kubernetes.io/enforce=baseline \
pod-security.kubernetes.io/warn=restricted \
--overwrite
# Phase 3: Full restricted enforcement
kubectl label namespace my-namespace \
pod-security.kubernetes.io/enforce=restricted \
--overwrite
步骤 5:干运行强制执行测试
# Test what would happen with restricted enforcement
kubectl label --dry-run=server --overwrite namespace my-namespace \
pod-security.kubernetes.io/enforce=restricted
# Example output:
# Warning: existing pods in namespace "my-namespace" violate the new
# PodSecurity enforce level "restricted:latest"
# Warning: nginx-xxx: allowPrivilegeEscalation != false,
# unrestricted capabilities, runAsNonRoot != true, seccompProfile
Baseline 配置文件限制
| 控制项 | Restricted | 要求 |
|---|---|---|
| HostProcess | 不得设置 | Pod 不能使用 Windows HostProcess |
| 主机命名空间 | 不得设置 | 不允许使用 hostNetwork、hostPID、hostIPC |
| 特权 | 不得设置 | 不允许设置 privileged: true |
| 功能 | 仅允许 Baseline 列表中的功能 | 仅允许 NET_BIND_SERVICE;Restricted 必须移除所有功能 |
| HostPath 卷 | 不得使用 | 不允许挂载 hostPath 卷 |
| 主机端口 | 不得使用 | 容器规范中不得设置 hostPort |
| AppArmor | 默认值/运行时 | 不得设置为 unconfined |
| SELinux | 限定类型 | 仅允许 container_t、container_init_t、container_kvm_t |
| /proc 挂载类型 | 仅允许默认值 | 必须使用默认 proc 挂载 |
| Seccomp | RuntimeDefault 或 Localhost | 必须指定 seccomp 配置文件(Restricted) |
| Sysctls | 仅允许安全集合 | 仅允许有限的安全 sysctls |
验证命令
# Verify namespace labels
kubectl get ns --show-labels | grep pod-security
# Test pod creation against policy
kubectl run test-pod --image=nginx --namespace=production --dry-run=server
# Check for violations in audit logs
kubectl get events --field-selector reason=FailedCreate -A
# Scan with Kubescape for PSS compliance
kubescape scan framework nsa --namespace production
参考资料
评论
支持 Markdown