Debian系统上Kubernetes资源调度策略
一 调度总览与基础
二 常用调度策略与适用场景
| 策略 | 作用 | 关键字段 | 典型场景 |
|---|---|---|---|
| nodeSelector | 按节点标签强制选择 | spec.nodeSelector | 将负载调度到具备特定标签(如 disktype=ssd)的节点 |
| nodeAffinity | 节点级亲和/偏好,支持硬/软约束 | spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution / preferredDuringSchedulingIgnoredDuringExecution | 将 GPU/高性能节点优先分配给计算型负载 |
| podAffinity / podAntiAffinity | 基于已运行 Pod 的标签进行亲和/反亲和 | spec.affinity.podAffinity / podAntiAffinity | 将前端与后端靠近(降低时延),或将同类副本分散(提升高可用) |
| Taints / Tolerations | 节点“排斥/容忍”,实现专用/隔离 | spec.taints / spec.tolerations | 将 GPU 节点打污点仅允许带容忍的 Pod 调度;节点维护时驱逐 |
| 拓扑分散与均衡 | 跨节点/拓扑域分散或装箱 | topologyKey(如 kubernetes.io/hostname)、NodeResourcesBalancedAllocation 等 | 提升容灾与资源利用均衡 |
三 关键配置示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: ml-job
spec:
replicas: 3
selector: { matchLabels: { app: ml-job } }
template:
metadata:
labels: { app: ml-job }
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/gpu
operator: In
values: ["nvidia-gpu"]
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values: ["ml-job"]
topologyKey: kubernetes.io/hostname
containers:
- name: ml-container
image: my-ml-image:latest
resources:
requests: { cpu: "2", memory: "8Gi" }
limits: { cpu: "4", memory: "16Gi" }
spec:
tolerations:
- key: "gpu"
operator: "Equal"
value: "true"
effect: "NoSchedule"
containers:
- name: gpu-worker
image: nvidia/cuda:12.2-base
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector: { matchLabels: { app: web } }
template:
metadata:
labels: { app: web }
spec:
containers:
- name: app
image: nginx:1.25
resources:
requests: { cpu: "500m", memory: "512Mi" }
limits: { cpu: "1", memory: "1Gi" }
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: default-scheduler
plugins:
score:
enabled:
- name: NodeResourcesBalancedAllocation
weight: 2
四 在 Debian 上的实施与优化建议