Kubernetes provides two CPU policies: none and static.
Based on the Kubernetes static core binding policy, the enhanced CPU policy (enhanced-static) supports burstable pods (whose CPU requests and limits must be positive integers) and allows them to preferentially use certain CPUs to ensure application stability. Example:
... spec: containers: - name: nginx image: nginx resources: limits: memory: "300Mi" cpu: "2" requests: memory: "200Mi" cpu: "1"
This feature is built on the optimized CPU scheduling in the HCE OS 2.0 kernel. When the CPU usage preferentially used by a container exceeds 85%, the container is automatically allocated to other CPUs with low usage to ensure the response capability of applications.
To use this feature, the following conditions must be met:
Take a node with 8 vCPUs and 32 GB memory as an example. Deploy a workload whose CPU request is 1 and limit is 2 in the cluster in advance.
cat /var/lib/kubelet/cpu_manager_state
Command output:
{"policyName":"enhanced-static","defaultCpuSet":"0,2-7","entries":{"6739f6f2-ebe5-48ae-945a-986d5d8919b9":{"container-1":"0-7,10001"}},"checksum":1638128523}
cat /sys/fs/cgroup/cpuset/kubepods/burstable/pod {pod uid} / {Container ID} /cpuset.preferred_cpus
kubectl get po {pod name} -n {namespace} -ojsonpath='{.metadata.uid}{"\n"}'
In the preceding command, {pod name} and {namespace} indicate the pod name and the namespace to which the pod belongs.
docker ps --no-trunc | grep {pod name} | grep -v cce-pause | awk '{print $1}'
containerd node pool: In the command, {pod name} indicates the pod name, {pod id} indicates the pod ID, and {container name} indicates the container name.
# Obtain the pod ID. crictl pods | grep {pod name} | awk '{print $1}' # Obtain the complete container ID. crictl ps --no-trunc | grep {pod id} | grep {container name} | awk '{print $1}'
A complete example is as follows:
cat /sys/fs/cgroup/cpuset/kubepods/burstable/pod6739f6f2-ebe5-48ae-945a-986d5d8919b9/5ba5603434b95fd22d36fba6a5f1c44eba83c18c2e1de9b52ac9b52e93547a13/cpuset.preferred_cpus
If the following command output is displayed, CPU 1 is preferentially used.
1