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

发布于:2025-09-01 ⋅ 阅读:(22) ⋅ 点赞:(0)

🛡️ 在 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

网站公告

今日签到

点亮在社区的每一天
去签到