Kubernetes学习之旅-Pod 调度
·
Kubernetes Pod调度详解:掌控容器部署的艺术
什么是Pod调度?
Pod调度是Kubernetes将Pod分配到合适节点的过程。调度器(Scheduler)负责根据集群状态、资源需求、调度策略等因素,为每个新创建的Pod选择最佳运行节点。这是Kubernetes自动化运维的核心能力之一。
调度器的工作原理
调度流程
Pod创建请求 → API Server → 调度器监控 → 过滤节点 → 评分节点 → 绑定节点 → kubelet运行Pod
核心调度阶段
过滤(Filtering):排除不满足要求的节点
评分(Scoring):为合格节点打分排序
绑定(Binding):选择最高分节点并绑定Pod
基础调度策略
资源请求与限制
yaml
apiVersion: v1 kind: Pod metadata: name: resource-aware-pod spec: containers: - name: app image: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
作用:确保节点有足够资源运行Pod
节点选择器(nodeSelector)
yaml
apiVersion: v1 kind: Pod metadata: name: gpu-pod spec: nodeSelector: accelerator: gpu-nvidia disktype: ssd containers: - name: ai-app image: tensorflow:latest
适用场景:基于节点标签简单调度
高级调度机制
节点亲和性(nodeAffinity)
yaml
apiVersion: v1 kind: Pod metadata: name: affinity-pod spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: topology.kubernetes.io/zone operator: In values: - us-west-2a preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: environment operator: In values: - production containers: - name: web-app image: nginx
Pod亲和性与反亲和性(podAffinity/podAntiAffinity)
yaml
apiVersion: v1 kind: Pod metadata: name: anti-affinity-pod spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - redis topologyKey: kubernetes.io/hostname containers: - name: web-app image: nginx
污点和容忍度(Taints and Tolerations)
节点污点设置
bash
# 给节点添加污点 kubectl taint nodes node1 key=value:NoSchedule # 查看节点污点 kubectl describe node node1 | grep Taint
Pod容忍度配置
yaml
apiVersion: v1 kind: Pod metadata: name: toleration-pod spec: tolerations: - key: "key" operator: "Equal" value: "value" effect: "NoSchedule" - key: "disktype" operator: "Exists" effect: "NoExecute" tolerationSeconds: 3600 # 被驱逐前运行时间 containers: - name: special-app image: special-app:latest
优先级和抢占(Priority and Preemption)
定义优先级类
yaml
apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority value: 1000000 globalDefault: false description: "用于关键业务Pod"
在Pod中使用优先级
yaml
apiVersion: v1 kind: Pod metadata: name: high-priority-pod spec: priorityClassName: high-priority containers: - name: critical-app image: critical-app:latest
节点资源管理
资源配额(Resource Quotas)
yaml
apiVersion: v1 kind: ResourceQuota metadata: name: namespace-quota spec: hard: requests.cpu: "2" requests.memory: 4Gi limits.cpu: "4" limits.memory: 8Gi pods: "10"
限制范围(Limit Ranges)
yaml
apiVersion: v1 kind: LimitRange metadata: name: cpu-limit-range spec: limits: - default: cpu: "500m" defaultRequest: cpu: "250m" type: Container
自定义调度器
使用多调度器
yaml
apiVersion: v1 kind: Pod metadata: name: custom-scheduled-pod spec: schedulerName: my-custom-scheduler # 默认是default-scheduler containers: - name: app image: nginx
实战调度场景
场景1:GPU资源调度
yaml
apiVersion: v1 kind: Pod metadata: name: gpu-inference spec: nodeSelector: nvidia.com/gpu: "true" containers: - name: inference-app image: tensorflow-serving resources: limits: nvidia.com/gpu: 2 memory: 8Gi cpu: "4" requests: nvidia.com/gpu: 1 memory: 4Gi cpu: "2"
场景2:高可用部署
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: ha-web-app spec: replicas: 3 template: spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - ha-web-app topologyKey: topology.kubernetes.io/zone containers: - name: web-app image: nginx
场景3:成本优化调度
yaml
apiVersion: v1 kind: Pod metadata: name: cost-optimized-pod spec: affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 preference: matchExpressions: - key: cost-tier operator: In values: - spot - weight: 50 preference: matchExpressions: - key: cost-tier operator: In values: - on-demand tolerations: - key: "spot" operator: "Exists" effect: "NoSchedule" containers: - name: batch-job image: batch-processor
调度问题调试
常用诊断命令
bash
# 查看Pod调度事件 kubectl describe pod <pod-name> # 查看节点资源情况 kubectl describe nodes # 查看调度器日志 kubectl logs -n kube-system -l component=kube-scheduler # 检查节点标签 kubectl get nodes --show-labels # 查看Pending状态的Pod kubectl get pods --field-selector status.phase=Pending
常见调度问题
bash
# 资源不足 kubectl describe node | grep -A 10 -B 5 "Allocatable" # 节点选择器不匹配 kubectl get nodes -l disktype=ssd # 污点阻止调度 kubectl describe node | grep -i taint # 亲和性规则冲突 kubectl get pods -o wide
最佳实践
1. 合理的资源请求
yaml
resources: requests: cpu: "250m" memory: "512Mi" limits: cpu: "500m" memory: "1Gi"
2. 多维度亲和性
yaml
affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 80 preference: matchExpressions: - key: topology.kubernetes.io/zone operator: In values: [us-west-2a] - weight: 20 preference: matchExpressions: - key: instance-type operator: In values: [m5.large]
3. 优雅的故障处理
yaml
tolerations: - key: "node.kubernetes.io/unreachable" operator: "Exists" effect: "NoExecute" tolerationSeconds: 300 - key: "node.kubernetes.io/not-ready" operator: "Exists" effect: "NoExecute" tolerationSeconds: 300
总结
Kubernetes Pod调度提供了强大的部署控制能力:
核心价值
资源优化:合理分配集群资源
高可用性:确保应用跨节点/区域分布
性能保障:优化应用运行位置
成本控制:智能选择计算资源
关键机制
基础调度:资源请求、节点选择器
高级调度:亲和性、污点容忍度
优先级控制:抢占式调度
扩展能力:自定义调度器
实践建议
始终设置资源请求和限制
使用亲和性确保关键应用分布
利用污点管理专用节点
监控调度性能并及时调整策略
掌握Pod调度技术,能够显著提升集群的资源利用率、应用性能和业务连续性,是Kubernetes管理员的核心技能之一。
更多推荐


所有评论(0)