温馨提示×

Ubuntu Kubernetes资源调度怎么做

小樊
39
2025-12-18 15:19:30
栏目: 智能运维

Ubuntu 上 Kubernetes 资源调度的落地做法

一 基础与容量规划

  • 在 Ubuntu 上部署集群后,调度以 kube-scheduler 为核心,依据 Pod 的资源请求 requests 与上限 limits 进行节点选择与打分;调度流程分为 过滤(Predicate)评分(Priority) 两阶段,常见过滤包括资源是否充足、节点污点容忍、亲和/反亲和规则,评分常见策略包含 LeastAllocated(默认)/MostAllocated/RequestedToCapacityRatio。节点侧需理解 Capacity(容量)Allocatable(可分配) 的关系:可分配资源要扣除系统守护进程与 kube 组件预留以及驱逐阈值,公式可概括为:Capacity = Allocatable + System-Reserved + Kube-Reserved + Eviction Thresholds。默认驱逐阈值常见为 内存 100Mi临时存储 10%。建议通过 kubectl describe node 观察各节点资源与可分配量,确保为系统预留与驱逐留出安全边界。

二 Pod 资源配置与请求

  • 为工作负载设置合理的 requests/limits,requests 用于调度阶段匹配节点资源,limits 用于运行时限制容器资源使用;对 GPU 等特殊资源,直接在 resources 中声明(如 nvidia.com/gpu)。示例:
apiVersion: v1
kind: Pod
metadata:
  name: demo
spec:
  containers:
  - name: app
    image: nginx:1.25
    resources:
      requests:
        cpu: "500m"
        memory: "1Gi"
      limits:
        cpu: "1"
        memory: "2Gi"
  • 健康检查(Liveness/Readiness/Startup)能提升调度有效性与稳定性,避免因应用未就绪或异常导致反复调度或流量误投。

三 常用调度策略与示例

  • 节点选择器 NodeSelector:按节点标签定向调度
spec:
  nodeSelector:
    disktype: ssd
  • 节点亲和性 Node Affinity:硬性要求 + 软性偏好
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: beta.kubernetes.io/arch
            operator: In
            values: [amd64]
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: disk-type
            operator: In
            values: [ssd]
  • Pod 反亲和性 Pod Anti-Affinity:副本分散,提升高可用
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values: [web]
        topologyKey: kubernetes.io/hostname
  • 污点与容忍 Taints/Tolerations:节点隔离与专用资源
# 给节点打污点
kubectl taint nodes gpu-node-1 hardware=gpu:NoSchedule
# Pod 容忍
spec:
  tolerations:
  - key: "hardware"
    operator: "Equal"
    value: "gpu"
    effect: "NoSchedule"
  • 拓扑分布约束 Topology Spread:跨域均匀分布
spec:
  topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: topology.kubernetes.io/zone
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        app: my-app
  • 优先级与抢占 Priority/Preemption:关键业务优先获得资源
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000
globalDefault: false
---
spec:
  priorityClassName: high-priority
  • 节点维护:临时屏蔽节点调度
kubectl cordon <node-name>   # 不可调度
kubectl uncordon <node-name> # 恢复调度
  • 直接指定节点 NodeName(绕过调度器,仅测试/排障用)
spec:
  nodeName: node-frontend-1

以上策略可组合使用,实现“定向—隔离—分布—保序”的完整调度闭环。

四 调度器配置与扩展

  • 调度器采用可插拔的 调度框架(Scheduling Framework),可按需启用/禁用插件并定制评分策略。例如将 NodeResourcesFit 的评分策略由 LeastAllocated 调整为 MostAllocated(更偏向装箱、减少节点数),或在多配置文件(profiles)中为不同工作负载启用不同插件组合。
  • 多调度器与调度命名:为特定负载指定 schedulerName 使用自定义或备用调度器;也可通过 KubeSchedulerConfiguration 配置多 profile、队列排序、抢占、绑定超时等参数,满足复杂场景的差异化调度需求。

五 运维与排障要点

  • 节点资源与可分配核对
kubectl describe node <node> | egrep 'Capacity|Allocatable'
  • 定位 Pending 原因
kubectl describe pod <pod> | grep -A 10 Events
kubectl get events --sort-by=.metadata.creationTimestamp
  • 快速隔离异常节点
kubectl cordon <node>    # 先屏蔽,避免新 Pod 进入
# 排查/维护完成后
kubectl uncordon <node>
  • 专用资源与维护窗口:对 GPU/高性能盘 等节点使用 污点容忍 做硬隔离;节点维护前使用 NoExecute 污点实现“排空”后再操作,避免业务受损。

0