使用 Manager 策略优化 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 node 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. Cordon 和 Drain

    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: staticfull-pcpus-only: "true"
  • 行为:仅适用于 Guaranteed Pod;不影响 Burstable/BestEffort

Memory ManagerPolicy

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

Topology ManagerPolicy

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

术语

  • NUMA node:Non-Uniform Memory Access 域
  • CPU pinning:将容器绑定到专用 CPU
  • NUMA affinity:优先使用与 CPU 位于同一 NUMA node 的内存
  • Topology alignment:将 CPU、内存和设备共置于同一个 NUMA node
  • Guaranteed Pod:requests = limits;CPU 以完整核心数指定