Kubernetes调度
kube-scheduler 是 Kubernetes 默认调度器,负责为 Pod 选择最佳节点。
调度流程
Pod 创建 → 调度队列 → 过滤(Filtering)→ 评分(Scoring)→ 分配(Selecting)
↓ ↓
筛选可行节点 选择最优节点节点选择器
使用 nodeSelector 将 Pod 调度到带有特定标签的节点。
节点标签管理
# 添加标签
kubectl label nodes node1 disktype=ssd
# 查看标签
kubectl get nodes --show-labels
# 删除标签
kubectl label nodes node1 disktype-Pod 使用节点选择器
spec:
nodeSelector:
disktype: ssd # 匹配标签为 disktype=ssd 的节点
containers:
- name: app
image: app:v1污点和容忍
Taint(污点)和 Toleration(容忍)配合使用,控制 Pod 调度到特定节点。
污点效果
| 效果 | 说明 | 对已有 Pod 的影响 |
|---|---|---|
| NoSchedule | 不调度新 Pod | 不影响 |
| PreferNoSchedule | 尽量不调度 | 不影响 |
| NoExecute | 不调度并驱逐 | 会驱逐 |
设置污点
# 添加污点
kubectl taint nodes node1 key=value:NoSchedule
# 添加 NoExecute 污点(会驱逐已有 Pod)
kubectl taint nodes node1 key=value:NoExecute
# 删除污点(末尾加 -)
kubectl taint nodes node1 key=value:NoSchedule-Pod 容忍配置
spec:
tolerations:
# 精确匹配
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
# 通配符匹配
- key: "node-type"
operator: "Exists"
effect: "NoExecute"容忍配置说明:
| 字段 | 说明 |
|---|---|
| key | 污点 key |
| operator | Equal(值匹配)或 Exists(存在即可) |
| value | 污点值(operator 为 Equal 时需要) |
| effect | NoSchedule / PreferNoSchedule / NoExecute |
| tolerationSeconds | NoExecute 效果的持续时间 |
常见场景
专用节点(只运行特定应用):
kubectl taint nodes node1 dedicated=app:NoScheduleMaster 节点保护:
kubectl taint nodes master node-role.kubernetes.io/master:NoSchedule有状态应用的节点亲和性:
kubectl taint nodes node1 dedicated=mysql:NoSchedule资源限制
为容器设置 CPU 和内存的请求(requests)和限制(limits)。
requests vs limits
| 字段 | 作用 | 说明 |
|---|---|---|
| requests | 调度依据 | K8s 据此选择有足够资源的节点 |
| limits | 运行上限 | 超过会被限制或 OOM Kill |
spec:
containers:
- name: app
image: app:v1
resources:
limits:
cpu: "500m" # 上限,可突破(短暂)
memory: "256Mi" # 上限,不可突破(OOM)
requests:
cpu: "100m" # 调度参考
memory: "64Mi" # 调度参考单位:
| 资源 | 单位 | 说明 |
|---|---|---|
| CPU | m(毫核)或核 | 100m = 0.1 核 |
| Memory | Ki / Mi / Gi | 1024 进制 |
建议
- requests 设置为实际使用量
- limits 设置为 requests 的 2-4 倍
- 避免 limits 过大导致资源浪费
ResourceQuota
命名空间级别的资源总量限制:
apiVersion: v1
kind: ResourceQuota
metadata:
name: quota
spec:
hard:
requests.cpu: "4"
requests.memory: "4Gi"
limits.cpu: "8"
limits.memory: "8Gi"LimitRange
容器默认资源限制(未指定时自动应用):
apiVersion: v1
kind: LimitRange
metadata:
name: limits
spec:
limits:
- type: Container
default:
cpu: "200m"
memory: "128Mi"
defaultRequest:
cpu: "100m"
memory: "64Mi"最佳实践
- 为所有容器设置 resource requests
- 使用 LimitRange 设置命名空间默认值
- 使用 ResourceQuota 限制命名空间总量
常用命令
# 查看节点资源使用
kubectl top nodes
# 查看 Pod 资源使用
kubectl top pods
# 查看调度相关事件
kubectl get events --field-selector involvedObject.name=pod-name
# 查看 Pod 调度决策
kubectl describe pod <pod-name> | grep -A 5 "Events:"Pod 调度由 ReplicaSet 和 Deployment 控制器管理副本数量。