Home / Best Practices / Configuration assigns more reasonable CPU management policies

Configuration assigns more reasonable CPU management policies

In Kubernetes, the method of assigning CPU to container groups is called CPU management policy. By adjusting the CPU management policy, CPU can be allocated more reasonably to container groups scheduled on nodes.

Type of policy

The kubelet controls the CPU management policy through the --cpu-manager-policy parameter. The current policy in use is recorded in the /var/lib/kubelet/cpu_manager_state file.

Policy Meaning Applicable scenarios
none Default policy, using CPU affinity based on CFS quotas. General scenarios, can achieve more effective CPU resource utilization.
static Static policy, allowing specific container groups to monopolize CPU relative to other container groups. If there are CPU-sensitive container groups, it can reduce performance loss caused by throttling scheduling.

Static policy usage restrictions

Configuration policy

The CPU management policy can be configured by modifying the /var/lib/kubelet/config.yaml file. The following operations are only for nodes within the same cluster.

(Optional) Password-free login settings

If there are too many nodes to be set in the cluster, to avoid entering the password for each node, you can enable password-free login between nodes to complete the operation quickly.

Caution: After the CPU allocation policy is successfully configured, be sure to disable password-free login to ensure environment security.

Enable password-free login

  1. Generate an SSH key pair on the control node. After entering the command, read the prompts and press Enter to confirm in order.

    ssh-keygen 
  2. Copy the key to all target nodes.

    ssh-copy-id root@{Target Node IP}

    Note: If the command cannot be found and cannot be installed, you can manually add the contents of the .ssh/id_rsa.pub file to the .ssh/authorized_keys file of the node to be configured.

Turn off password-free login

Caution: This operation will delete all remembered public keys. Please make sure it will not affect existing businesses.

for i in $(kubectl  get nodes  | grep -vi name | awk '{print $1}')
do
 
ssh root@$i "rm /root/.ssh/authorized_keys"
 
done

Enable static policies

Perform the following steps on the control node.

KUBECONFIG="/var/lib/kubelet/config.yaml"
BACKUPCONFIG="/root/kubelet-config-before-set-static.yaml"
for i in $(kubectl  get nodes  | grep -vi name | awk '{print $1}')
do
 
ssh root@$i "cp ${KUBECONFIG} ${BACKUPCONFIG}; sed -i '/^cpuManagerPolicy.*/d' ${KUBECONFIG}; sed -i '/cpuManagerReconcilePeriod*/acpuManagerPolicy: static' ${KUBECONFIG}; sed -i 's/  cpu: 100m/  cpu: 500m/g' ${KUBECONFIG}; rm -f /var/lib/kubelet/cpu_manager_state; systemctl restart kubelet "
 
done

Restore Default Policy

Perform the following steps on the control node.

KUBECONFIG="/var/lib/kubelet/config.yaml"
BACKUPCONFIG="/root/kubelet-config-before-set-none.yaml"
for i in $(kubectl  get nodes  | grep -vi name | awk '{print $1}')
do
 
ssh root@$i "cp ${KUBECONFIG} ${BACKUPCONFIG};  sed -i '/^cpuManagerPolicy.*/d' ${KUBECONFIG}; sed -i '/cpuManagerReconcilePeriod*/acpuManagerPolicy: none' ${KUBECONFIG} ; rm /var/lib/kubelet/cpu_manager_state ; systemctl restart kubelet"
 
done

Validation

Wait for about 30 seconds to regenerate the /var/lib/kubelet/cpu_manager_state file, then perform the operation on the control node.

for i in $(kubectl  get nodes  | grep -vi name | awk '{print $1}')
do
  
ssh root@$i "cat /var/lib/kubelet/cpu_manager_state"
 
done