使用 Manager Policies 优化 Pod 性能

Kubernetes 集群管理员可以启用并验证 CPU ManagerPolicyMemory ManagerPolicyTopology 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 设置取决于节点操作系统。

不可变基础设施

在不可变基础设施上,CPU 和 Memory Manager 策略会在 Alauda 技术支持的协助下应用。请联系你的 Alauda 支持代表,以在不可变节点上启用这些策略。

传统操作系统

在传统操作系统上,请在每个节点上应用配置:

  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>
  • 对于 DaemonSets 和 system 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"
  • reserved memory 的总和与 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 pinning:将容器绑定到专用 CPU
  • NUMA affinity:优先使用与 CPU 相同 NUMA 节点上的内存
  • Topology alignment:将 CPU、内存和设备共置于一个 NUMA 节点上
  • Guaranteed Pod:requests = limits;CPU 以完整核心指定