Pod污点、容忍策略、精确匹配、模糊匹配、Pod优先级与抢占、容器安全
·
一、Pod 污点(Taints)与容忍(Tolerations)
🧨 污点(Taint):节点“排斥”某些 Pod
污点作用于 Node,格式为 key=value:effect。
三种 Effect 类型
| Effect | 行为 |
|---|---|
| NoSchedule | 新 Pod 不会调度到该节点(已运行的不受影响) |
| PreferNoSchedule | 尽量不调度(软性排斥) |
| NoExecute | 立即驱逐 不容忍的 Pod,并阻止新 Pod 调度 |
✅ 典型使用场景
- Master 节点保护:默认带
node-role.kubernetes.io/control-plane:NoSchedule - 专用硬件隔离:GPU/FPGA 节点仅供 AI 任务使用
- 故障节点隔离:自动添加污点,驱逐非关键 Pod
🔧 操作命令
# 给节点添加污点
kubectl taint nodes gpu-node dedicated=gpu:NoSchedule
# 查看节点污点
kubectl describe node gpu-node | grep Taints
# 移除污点
kubectl taint nodes gpu-node dedicated-
二、容忍(Toleration):Pod “接受”特定污点
容忍定义在 Pod spec 中,决定是否能容忍某污点。
📄 YAML 示例:容忍 GPU 污点
apiVersion: v1
kind: Pod
metadata:
name: ai-training-job
spec:
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
containers:
- name: trainer
image: tensorflow:2.13-gpu
🔍 精确匹配 vs 模糊匹配
| 匹配类型 | 配置方式 | 说明 |
|---|---|---|
| 精确匹配 | operator: Equal + 指定 key/value/effect | 必须完全一致 |
| 模糊匹配(Exists) | operator: Exists + 仅指定 key | 只要存在该 key 的污点即匹配(忽略 value 和 effect) |
示例 1:精确匹配(推荐)
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
示例 2:模糊匹配(通用容忍)
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 300 # 容忍 5 分钟后才驱逐
💡 最佳实践:
- 关键系统 Pod(如 CoreDNS、Calico)应容忍
node.kubernetes.io/not-ready和unreachable;- 使用
tolerationSeconds实现优雅降级(如网络抖动时不立即驱逐)。
三、Pod 优先级与抢占(Priority & Preemption)
当资源不足时,高优先级 Pod 可抢占低优先级 Pod 的资源。
步骤 1:定义 PriorityClass
# critical-priority.yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority
value: 1000000 # 数值越大优先级越高
globalDefault: false # 是否作为默认优先级
description: "High priority for critical services"
⚠️ 注意:
- 系统保留值:< 1000000(如
system-cluster-critical = 2000000000)- 用户自定义建议:100000 ~ 999999999
步骤 2:Pod 引用 PriorityClass
apiVersion: v1
kind: Pod
metadata:
name: payment-service
spec:
priorityClassName: high-priority # 关键!
containers:
- name: app
image: payment:v1
🔄 抢占流程
- 高优先级 Pod 创建,但无足够资源;
- 调度器查找可被抢占的低优先级 Pod(在同一节点上);
- 驱逐低优先级 Pod(发送 SIGTERM);
- 高优先级 Pod 调度成功。
✅ 适用场景:
- 支付、风控等核心业务;
- 故障恢复时的关键组件重建。
四、容器安全上下文(Security Context)
通过 SecurityContext 限制容器权限,遵循 最小权限原则。
1. Pod 级 vs 容器级安全上下文
spec.securityContext:作用于整个 Pod(共享设置)spec.containers[].securityContext:作用于单个容器
2. 关键安全配置项
| 配置项 | 作用 | 安全建议 |
|---|---|---|
runAsNonRoot: true | 禁止以 root 用户运行 | ✅ 强制开启 |
runAsUser: 1000 | 指定运行 UID | 避免使用 0 |
readOnlyRootFilesystem: true | 根文件系统只读 | ✅ 推荐(配合 emptyDir 写临时文件) |
privileged: false | 禁用特权模式 | ❌ 永远不要设为 true(除非特殊驱动) |
allowPrivilegeEscalation: false | 禁止提权 | ✅ 强制开启 |
capabilities.drop: ["ALL"] | 剥离 Linux Capabilities | ✅ 仅保留必要能力(如 NET_BIND_SERVICE) |
📄 安全 Pod YAML 示例
apiVersion: v1
kind: Pod
metadata:
name: secure-app
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
containers:
- name: app
image: myapp:secure
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"] # 如需绑定 80/443 端口
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir: {}
五、Pod 安全准入(Pod Security Admission, PSA)
Kubernetes v1.25+ 内置的 集群级安全策略,替代旧版 PodSecurityPolicy(PSP)。
三种安全级别(Modes)
| 级别 | 说明 | 限制示例 |
|---|---|---|
| privileged | 无限制(等同旧 PSP) | 允许特权容器、hostNetwork |
| baseline | 默认安全基线 | 禁止 hostPID、hostIPC、privileged |
| restricted | 最严格(符合 CIS 基准) | 要求 runAsNonRoot、seccomp、drop ALL caps |
🔧 启用 PSA(通过 Namespace Label)
# 设置 Namespace 为 restricted 模式
kubectl label ns production pod-security.kubernetes.io/enforce=restricted
# 审计模式(不阻断,只记录)
kubectl label ns staging pod-security.kubernetes.io/audit=restricted
✅ 生产建议:
- 所有业务 Namespace 使用
restricted;- 系统 Namespace(kube-system)可设为
baseline。
六、综合实战:构建安全高优 AI 训练任务
apiVersion: v1
kind: Pod
metadata:
name: ai-train-critical
namespace: ai-prod
spec:
priorityClassName: high-priority
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
securityContext:
runAsNonRoot: true
runAsUser: 1001
containers:
- name: trainer
image: pytorch:2.1-cuda12
resources:
limits:
nvidia.com/gpu: 2
memory: "32Gi"
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: training-data-pvc
同时,在 ai-prod Namespace 启用 PSA:
kubectl label ns ai-prod pod-security.kubernetes.io/enforce=restricted
更多推荐
所有评论(0)