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
-
The
limitandrequestvalues of the container group must be equal and integers. -
Different nodes can use different allocation policies, but it is not conducive to management. It is recommended to keep the same settings for the entire cluster.
-
After setting a static policy, if the number or specifications of CPUs on a node change, you need to delete
/var/lib/kubelet/cpu_manager_stateand restart kubelet for the static policy to continue to take effect.
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
-
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 -
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.pubfile to the.ssh/authorized_keysfile 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"
doneEnable 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 "
doneRestore 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"
doneValidation
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-
When static policy is enabled, all target nodes will have
"policyName":"static"in their echo, indicating successful configuration. -
When restoring default policy, all target nodes will have
"policyName":"none"in their echo, indicating successful configuration.