在集群级别应用 Pod 安全标准

🛡️ 在 Kubernetes 集群中统一应用 Pod 安全标准

一、🎯 什么是 Pod 安全标准?

在 Kubernetes 中,Pod 是运行容器的基本单元,默认情况下,Pod 几乎可以做很多"危险"的事情,比如:

  • 以 root 用户运行(runAsUser: 0
  • 挂载主机敏感路径(如 /etc, /var
  • 使用特权模式(privileged: true
  • 启用主机网络/进程/IPC(hostNetwork: true, hostPID: true, hostIPC: true
  • 拥有高危 Linux Capabilities(如 SYS_ADMIN
  • 未配置 Seccomp、AppArmor 等安全模块

这些行为如果被恶意利用,可能会导致:

🚨 容器逃逸、权限提升、主机被入侵等严重安全问题。


二、🧪 创建一个没有安全策略的集群

复制代码
kind create cluster --name my-cluster
kubectl cluster-info --context kind-my-cluster
kubectl get ns
kind load docker-image nginx:latest --name my-cluster

✅ 集群未启用任何安全策略,所有 Pod(包括特权容器)默认允许运行,用于对比。


三、🧪 模拟不同安全级别

复制代码
kubectl label --dry-run=server --overwrite ns --all pod-security.kubernetes.io/enforce=privileged
kubectl label --dry-run=server --overwrite ns --all pod-security.kubernetes.io/enforce=baseline
kubectl label --dry-run=server --overwrite ns --all pod-security.kubernetes.io/enforce=restricted

✅ 这些命令仅用于模拟,帮助您理解不同 enforce 策略可能产生的影响,但不会实际生效,因为没有启用 Pod 安全准入控制器。

四、🛠️ 定义 Pod 安全准入策略配置文件

📄 文件名: /tmp/pss/cluster-level-pss-policy.yaml

🔒 ​​用途:​​ 定义 Pod 安全准入控制器的核心策略:enforce / audit / warn / exemptions

复制代码
mkdir -p /tmp/pss
cat <<EOF > /tmp/pss/cluster-level-pss-policy.yaml
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
- name: PodSecurity
  configuration:
    apiVersion: pod-security.admission.config.k8s.io/v1
    kind: PodSecurityConfiguration
    defaults:
      enforce: "baseline"
      enforce-version: "latest"
      audit: "restricted"
      audit-version: "latest"
      warn: "restricted"
      warn-version: "latest"
    exemptions:
      namespaces: ["kube-system"]  # 不对系统命名空间生效
EOF

✅ 请确保该文件路径为:/tmp/pss/cluster-level-pss-policy.yaml

  • enforce: baseline → 违反则拒绝 Pod
  • auditwarn: 使用 restricted,仅记录/警告
  • exemptions: 保护系统组件

五、🛠️ 拥有安全策略的配置文件(加载上面的策略配置)

📄 文件名: /tmp/pss/kind-cluster-with-pss-config.yaml

🔧 ​​用途:​​ 定义 Kind 集群的 API Server 挂载与启动参数,以加载上一步的策略文件

复制代码
cat <<EOF > /tmp/pss/kind-cluster-with-pss-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: ClusterConfiguration
    apiServer:
      extraArgs:
        admission-control-config-file: /etc/config/cluster-level-pss-policy.yaml
      extraVolumes:
        - name: pss-config
          hostPath: /etc/config
          mountPath: /etc/config
          readOnly: true
          pathType: DirectoryOrCreate
  extraMounts:
  - hostPath: /tmp/pss
    containerPath: /etc/config
    readOnly: true
EOF

✅ 说明:

  • 我们将主机上的 /tmp/pss/cluster-level-pss-policy.yaml 挂载到容器中的 /etc/config/cluster-level-pss-policy.yaml
  • API Server 启动时会读取该文件,从而启用 Pod 安全准入控制并应用您定义的 enforce/audit/warn 策略

六、🚀 创建启用安全策略的新集群

复制代码
kind create cluster \
  --name pss-test-cluster \
  --config /tmp/pss/kind-cluster-with-pss-config.yaml  #上一步的配置文件

# 加载本地镜像
kind load docker-image nginx:latest --name pss-test-cluster

✅ 集群名称:pss-test-cluster(请记住此名称,后面所有 kubectl 操作都要用它)

验证集群:

复制代码
kubectl cluster-info --context kind-pss-test-cluster

七、🧪 测试 Pod 是否按策略被允许 / 拒绝 / 警告

✅ 测试 1:部署符合 Baseline 的普通 Pod

复制代码
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: baseline-pod
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: Never
EOF

✅ 预期:Pod 创建成功,但可能收到来自 Restricted 策略的警告(比如应使用非 root)


❌ 测试 2:部署一个 Privileged Pod

复制代码
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: privileged-pod
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: Never
    securityContext:
      privileged: true   # 特权容器
EOF

🔴 预期结果:Pod 被拒绝,报错类似:

复制代码
Error from server (Forbidden): error when creating "STDIN": pods "privileged-pod" is forbidden: violates PodSecurity "baseline:latest": privileged (container "nginx" must not set securit
yContext.privileged=true)              

✅ 这是期望的安全行为!


八、🧹 清理

复制代码
kind delete cluster --name pss-test-cluster
kind delete cluster --name my-cluster
相关推荐
dalerkd14 小时前
忙里偷闲叙-谈谈最近两年
网络·安全·web安全
牛三金14 小时前
匿踪查询沿革-Private Information Retrieval(PIR)
算法·安全
xixixi7777715 小时前
量子通信是当前信息安全和通信领域最前沿、最具变革性的技术之一
安全·信息安全·量子计算·通信·量子通信·密钥·传输
WLJT12312312315 小时前
守护自然与滋养民生的绿色之路
大数据·安全
C++ 老炮儿的技术栈16 小时前
什么是通信规约
开发语言·数据结构·c++·windows·算法·安全·链表
五仁火烧16 小时前
生产环境中配置了接口3000后,不能启动,改成8080后就可以
linux·网络·安全·vue
专业开发者16 小时前
借助安全返场方案提升智慧建筑能效的新机遇
物联网·安全
菩提小狗17 小时前
Sqlmap双击运行脚本,双击直接打开。
前端·笔记·安全·web安全
●VON18 小时前
跨模态暗流:多模态安全攻防全景解析
人工智能·学习·安全·von
廋到被风吹走19 小时前
【Spring】Spring Boot 配置管理深度指南:Profile、类型安全与加密
spring boot·安全·spring