When an exception occurs on a node, Kubernetes evicts the pods on the node to ensure the workload availability.
In Kubernetes, both kube-controller-manager and kubelet can evict pods.
kube-controller-manager consists of multiple controllers, and eviction is implemented by the node controller. The controller periodically checks the status of all nodes. When a node is in the NotReady state for a period of time, all pods on the node are evicted.
kube-controller-manager provides the following startup parameters to control evictions:
If resources of a node are to be used up, kubelet executes the eviction policy based on the pod priority, resource usage, and resource request. If pods have the same priority, the pod that uses the most resources or requests for the most resources will be evicted first.
kube-controller-manager evicts all pods on a node, whereas kubelet evicts certain pods on a node. Pods to be evicted are determined by the pod QoS. kubelet periodically checks the memory and disk resources of the node. If the resources are insufficient, pods are evicted based on the priority.
There are soft eviction thresholds and hard eviction thresholds.
kubelet provides the following parameters to control evictions:
If the pods are not evicted when the node is faulty, perform the following steps to locate the fault:
After the following command is run, the command output shows that many pods are in the Evicted state.
kubectl get pods
cat /var/paas/sys/log/kubernetes/kubelet.log | grep -i Evicted -C3
Troubleshooting methods are sorted based on the occurrence probability of the possible causes. You are advised to check the possible causes from high probability to low probability to quickly locate the cause of the problem.
If the fault persists after a possible cause is rectified, check other possible causes.
Use kubectl, or locate the row containing the target workload and choose More > Edit YAML in the Operation column to check whether tolerance is configured for the workload. For details, see Taints and Tolerations.
If the number of nodes in a cluster is smaller than 50 and the number of faulty nodes accounts for over 55% of the total nodes, the pod eviction will be suspended. In this case, Kubernetes will not attempt to evict the workload on the faulty node. For details, see Rate limits on eviction.
An evicted container is frequently scheduled to the original node.
Possible Causes
A node evicts a container based on the node resource usage. The evicted container is scheduled based on the allocated node resources. Eviction and scheduling are based on different rules. Therefore, an evicted container may be scheduled to the original node again.
Solution
Properly allocate resources to each container.
A pod in the workload fails and is being redeployed constantly.
Analysis
After a pod is evicted and scheduled to a new node, if pods in that node are also being evicted, the pod will be evicted again. Pods may be evicted repeatedly.
If a pod is evicted by kube-controller-manager, it would be in the Terminating state. This pod will be automatically deleted only after the node where the container is located is restored. If the node has been deleted or cannot be restored due to other reasons, you can forcibly delete the pod.
If a pod is evicted by kubelet, it would be in the Evicted state. This pod is only used for subsequent fault locating and can be directly deleted.
Solution
Run the following command to delete the evicted pods:
kubectl get pods <namespace> | grep Evicted | awk '{print $1}' | xargs kubectl delete pod <namespace>
In the preceding command, <namespace> indicates the namespace name. Configure it based on your requirements.