使用管理器策略优化 Pod 性能

Kubernetes 集群管理员可以启用并验证 CPU ManagerPolicy、Memory ManagerPolicy 和 Topology ManagerPolicy,以协调延迟敏感型工作负载的 CPU 绑定、NUMA 亲和性和拓扑放置。

范围和先决条件

角色和权限

  • 需要维护时段访问权限、kubectl 管理员权限以及节点的 SSH 访问权限。

工作负载要求

  • 要实现专用 CPU 和 NUMA 亲和性,Pod 必须在 Guaranteed QoS 类别中运行:requests = limits,且 CPU 必须以完整核心数指定(例如 2、4)。

不包括的内容

  • HugePages 不在本指南范围内。如果需要 HugePages 支持,请联系支持团队。

快速开始:示例 Kubelet 配置

以下 kubelet 配置可启用 NUMA 感知调度。请根据您的环境调整值。具体应用方式取决于节点操作系统,请参阅应用配置。

# —— CPU ManagerPolicy ——
cpuManagerPolicy: "static"              # Options: none | static
cpuManagerPolicyOptions:
  full-pcpus-only: "true"               # Recommended: allocate only full cores
cpuManagerReconcilePeriod: "5s"
reservedSystemCPUs: ""                  # e.g. "0-1" if reserving specific CPUs for the system

# —— Memory ManagerPolicy ——
memoryManagerPolicy: "Static"           # Options: none | Static 
reservedMemory:
  - numaNode: 0
    limits:
      memory: "2048Mi"
  - numaNode: 1
    limits:
      memory: "2048Mi"

# —— Topology ManagerPolicy ——
topologyManagerPolicy: "single-numa-node"     # Options: none | best-effort | restricted | single-numa-node
topologyManagerScope: "pod"                   # Options: container | pod

注意:

  • full-pcpus-only: "true" 可提高延迟一致性。
  • topologyManagerScope: pod 可确保同一 Pod 中的容器与公共 NUMA 拓扑对齐。
  • reservedMemory 必须根据 kubelet 配置和驱逐阈值计算(请参阅下一节)。

如何计算 reservedMemory

公式:

R_total = kubeReserved(memory) + systemReserved(memory) + evictionHard(memory.available)

所有 NUMA 节点上的 reservedMemory 之和必须等于 R_total。

步骤(对于 N 个 NUMA 节点):

  1. 计算 R_total(Mi)。

  2. 计算商和余数:

    • base = floor(R_total / N)
    • rem = R_total − base × N
  3. 分配值:

    • NUMA 节点 0 = base + rem
    • 其余 NUMA 节点 = base

示例(2 个 NUMA 节点):

  • kubeReserved=512Mi,systemReserved=512Mi,evictionHard=100Mi → R_total = 1124Mi
  • base = 562,rem = 0
    reservedMemory:
    - numaNode: 0
      limits:
        memory: "562Mi"
    - numaNode: 1
      limits:
        memory: "562Mi"

应用配置

这些 kubelet 设置的应用方式取决于节点操作系统。

Alauda OS

对于运行 Alauda OS 的节点,CPU 和 Memory Manager 策略由 Alauda 技术支持协助应用。请联系 Alauda 支持代表,在这些节点上启用这些策略。

非 Alauda OS

对于运行非 Alauda OS 的节点,请在每个节点上应用配置:

  1. 隔离并清空节点

    kubectl cordon <node>
    kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
  2. 停止 Kubelet 并清除状态

    sudo systemctl stop kubelet
    sudo rm -f /var/lib/kubelet/cpu_manager_state
    sudo rm -f /var/lib/kubelet/memory_manager_state
  3. 重启 Kubelet

    sudo systemctl daemon-reload
    sudo systemctl start kubelet
  4. 重新调度 Pod

    kubectl uncordon <node>
  • 对于 DaemonSet 和系统 Pod,请显式重启或删除 Pod。
  1. 验证恢复

    kubectl get nodes
    kubectl get pods -A -o wide | grep <node>

验证

CPU ManagerPolicy 状态

sudo cat /var/lib/kubelet/cpu_manager_state | jq .

检查:

  • .policyName = "static"
  • .defaultCpuSet 列出非独占 CPU
  • .entries 显示容器到 CPU 的分配

Memory ManagerPolicy 状态

sudo cat /var/lib/kubelet/memory_manager_state | jq .

检查:

  • .policyName = "Static"
  • 预留内存总量与 R_total 匹配
  • Guaranteed Pod 根据 single-numa-node 策略分配到 NUMA 节点

关键策略和行为

CPU ManagerPolicy

  • 用途:为 Guaranteed Pod 分配独占的物理 CPU
  • 配置:cpuManagerPolicy: static、full-pcpus-only: "true"
  • 行为:仅适用于 Guaranteed Pod;Burstable/BestEffort 不受影响

Memory ManagerPolicy

  • 用途:在 NUMA 节点级别预留并对齐内存
  • 配置:memoryManagerPolicy: "Static"、reservedMemory
  • 行为:与 Topology ManagerPolicy 配合使用可实现最佳对齐效果

Topology ManagerPolicy

  • 用途:将 CPU、内存和设备分配对齐到同一 NUMA 节点
  • 配置:topologyManagerPolicy: single-numa-node、topologyManagerScope: pod
  • 模式:best-effort、restricted、single-numa-node(严格)

术语

  • NUMA 节点:非一致性内存访问域
  • CPU 绑定:将容器绑定到独占 CPU
  • NUMA 亲和性:优先使用与 CPU 位于同一 NUMA 节点的内存
  • 拓扑对齐:将 CPU、内存和设备集中放置在一个 NUMA 节点上
  • Guaranteed Pod:requests = limits;CPU 指定为完整核心