86·安全高级

Pod 安全

kubernetessecuritypodsecurity-context

Pod 安全

学习目标

  1. 理解 Pod 安全的重要性
  2. 掌握 SecurityContext 的配置
  3. 了解 Pod Security Standards 和 Admission Controller

核心概念

一句话解释

Pod 安全通过 SecurityContext、Pod Security Standards 和准入控制,限制容器的权限和访问能力。

Pod 安全威胁模型

┌─────────────────────────────────────────────────────────────┐
│                    Pod 安全威胁                              │
├─────────────────────────────────────────────────────────────┤
│  容器逃逸                                                   │
│  ├── 特权容器访问宿主机                                     │
│  ├── 挂载宿主机文件系统                                     │
│  └── 使用宿主机网络                                         │
├─────────────────────────────────────────────────────────────┤
│  资源滥用                                                   │
│  ├── 挖矿程序                                               │
│  ├── DDoS 攻击                                              │
│  └── 资源耗尽                                               │
├─────────────────────────────────────────────────────────────┤
│  数据泄露                                                   │
│  ├── 访问其他 Pod 数据                                      │
│  ├── 读取 Secrets                                           │
│  └── 访问云元数据                                           │
└─────────────────────────────────────────────────────────────┘

SecurityContext 配置

apiVersion: v1
kind: Pod
metadata:
  name: secure-pod
spec:
  securityContext:
    # Pod 级别安全上下文
    runAsNonRoot: true
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: app
    image: my-app:latest
    securityContext:
      # 容器级别安全上下文
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      runAsNonRoot: true
      runAsUser: 1000
      capabilities:
        drop:
          - ALL
        add:
          - NET_BIND_SERVICE

SecurityContext 字段说明

字段说明默认值
runAsNonRoot必须以非 root 运行false
runAsUser运行用户 ID0 (root)
runAsGroup运行组 ID0
fsGroup文件系统组 ID-
readOnlyRootFilesystem只读根文件系统false
allowPrivilegeEscalation允许权限提升true
privileged特权模式false
capabilitiesLinux Capabilities-
seccompProfileSeccomp 配置-

Linux Capabilities

常见 Capabilities:
- NET_BIND_SERVICE: 绑定 1024 以下端口
- NET_RAW: 使用原始套接字
- SYS_ADMIN: 系统管理操作
- SYS_PTRACE: 跟踪进程

安全建议:
- drop ALL,然后只 add 必要的
- 避免 SYS_ADMIN、SYS_PTRACE

Pod Security Standards

┌─────────────────────────────────────────────────────────────┐
│                    Pod Security Standards                   │
├─────────────────────────────────────────────────────────────┤
│  Privileged (特权)                                          │
│  ├── 无限制                                                 │
│  └── 系统组件专用                                           │
├─────────────────────────────────────────────────────────────┤
│  Baseline (基线)                                            │
│  ├── 防止已知的权限提升                                     │
│  └── 适用于大多数工作负载                                   │
├─────────────────────────────────────────────────────────────┤
│  Restricted (受限)                                          │
│  ├── 最严格的安全限制                                       │
│  └── 适用于安全敏感工作负载                                 │
└─────────────────────────────────────────────────────────────┘

Pod Security Admission

# 命名空间级别配置
apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/warn: restricted
# 查看 Pod Security Admission 配置
kubectl get namespace my-namespace --show-labels

# 测试 Pod 是否符合安全标准
kubectl apply --dry-run=server -f pod.yaml

Pod Security Policy (已弃用)

# 注意:PodSecurityPolicy 已在 K8s 1.25 弃用
# 推荐使用 Pod Security Admission
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restricted
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  runAsUser:
    rule: MustRunAsNonRoot
  seccompProfile:
    type: RuntimeDefault

AppArmor 和 Seccomp

# AppArmor 配置
apiVersion: v1
kind: Pod
metadata:
  name: apparmor-pod
  annotations:
    container.apparmor.security.beta.kubernetes.io/app: localhost/my-profile
spec:
  containers:
  - name: app
    image: my-app:latest

---
# Seccomp 配置
apiVersion: v1
kind: Pod
metadata:
  name: seccomp-pod
spec:
  securityContext:
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: app
    image: my-app:latest

实践练习

练习 1:创建安全 Pod

# 创建符合 Restricted 标准的 Pod
apiVersion: v1
kind: Pod
metadata:
  name: secure-app
  namespace: default
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: app
    image: nginx:latest
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      runAsNonRoot: true
      runAsUser: 1000
      capabilities:
        drop:
          - ALL
    volumeMounts:
    - name: tmp
      mountPath: /tmp
  volumes:
  - name: tmp
    emptyDir: {}
# 应用 Pod
kubectl apply -f secure-app.yaml

# 验证安全配置
kubectl get pod secure-app -o jsonpath='{.spec.securityContext}'

练习 2:配置命名空间安全策略

# 配置命名空间安全标准
apiVersion: v1
kind: Namespace
metadata:
  name: secure-ns
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/warn: restricted
# 创建命名空间
kubectl apply -f secure-ns.yaml

# 测试不安全的 Pod
kubectl run test --image=nginx --restart=Never -n secure-ns
# 应该被拒绝

常见误解

1. 特权容器是必要的

事实:大多数应用不需要特权容器
误解:需要特权才能正常运行
解决:使用 capabilities 替代特权模式

2. 安全配置影响性能

事实:安全配置对性能影响很小
误解:安全配置会显著降低性能
解决:安全性和性能可以兼顾

小结

要点说明
SecurityContextPod 和容器级别的安全配置
Pod Security StandardsPrivileged、Baseline、Restricted
Pod Security Admission命名空间级别的安全策略
最佳实践非 root 运行、只读文件系统、drop capabilities
替代方案AppArmor、Seccomp

练习编辑器

bash
Loading...

继续学习

完成本课后,建议继续学习下一课「网络策略」 以巩固所学知识。