Skip to content

CPU pinning with cpuset

CPU pinning with cpuset is a performance optimization technique in Kubernetes that binds a container to specific CPU cores, rather than allowing it to share CPU capacity across available cores.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]

This configuration differs from the default cpushare model where containers share CPU computing power.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md] By binding a process to exclusive cores, the frequency of CPU context switches is significantly reduced, leading to a substantial performance improvement for latency-sensitive applications.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]

Configuration Requirements

To enable CPU pinning via the cpuset subsystem, the Pod must meet specific criteria regarding its resource configuration:

  • QoS Class: The Pod must be of the Guaranteed QoS type.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
  • CPU Limits: The CPU requests and limits must be set to the same value.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
  • Integer Value: The specified CPU value must be a positive integer (e.g., "2"), representing the number of exclusive cores required.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]

Example Configuration

The following manifest demonstrates a valid configuration for pinning a container to 2 exclusive CPU cores:

spec:
  containers:
  - name: nginx
    image: nginx
    resources:
      limits:
        memory: "200Mi"
        cpu: "2"
      requests:
        memory: "200Mi"
        cpu: "2"

In this scenario, the kubelet is responsible for assigning the specific physical CPU cores to the Pod.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]

Use Cases and Best Practices

It is strongly recommended to configure DaemonSet Pods (or similar critical system agents) as Guaranteed QoS.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md] If these Pods are not Guaranteed, they face eviction during resource contention. Since DaemonSets would immediately be recreated on the same host, the eviction action would be futile, causing system instability.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]

  • [[Kubernetes QoS]]
  • [[CPU Resource Management]]

Sources

^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]