Kubernetes scheduling predicates and priorities¶
Kubernetes scheduling predicates and priorities represent the two-stage mechanism used by the default kube-scheduler to determine the most suitable Node for a pending Pod. The process involves filtering out nodes that cannot run the Pod (Predicates) and then ranking the remaining nodes to select the best fit (Priorities).^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
Predicates¶
The Predicates phase acts as a filtering mechanism. The scheduler runs multiple sets of rules concurrently—typically using 16 Goroutines against all nodes in the cluster—to determine which nodes meet the necessary requirements to host the Pod^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
General Predicates¶
These rules handle the most fundamental scheduling constraints, primarily verifying that the Node has sufficient CPU and Memory resources^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
During this calculation, the scheduler evaluates the resource requests defined in the Pod spec^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Volume-Related Predicates¶
This set of rules checks constraints related to persistent storage^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. It ensures that the Node can satisfy the Pod's volume requirements, such as mounting specific volumes or accessing storage provisioners.
Node-Related Predicates¶
These rules verify that the Node itself meets specific conditions. A prominent example is PodToleratesNodeTaints, which checks if the Pod can tolerate the "taints" applied to a Node^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Pod-Related Predicates¶
These rules govern the relationships between the pending Pod and the existing Pods on a Node^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
- PodAffinityPredicate: This rule checks for affinity and anti-affinity relationships^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. For instance, it may enforce that two Pods must or must not reside on the same Node.
Priorities¶
Once the Predicates stage outputs a list of feasible Nodes, the Priorities stage assigns a score to each node (typically ranging from 0 to 10)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. The Node with the highest score is selected to host the Pod^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
LeastRequestedPriority¶
This is one of the most commonly used priority strategies^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. It calculates a score based on the fraction of resources that are "free" (available capacity minus requested resources)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. The algorithm selects the Node with the highest amount of idle CPU and Memory resources^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
BalancedResourceAllocation¶
This rule is designed to prevent resource skew, such as a situation where a Node has exhausted its CPU quota but has plenty of free Memory^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. It calculates the variance between the allocated fractions of different resources (CPU, Memory, Volume)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. The scheduler prefers the Node where resource consumption is most balanced across all resource types^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Other Priority Strategies¶
Additional priority strategies include:
- NodeAffinityPriority: Scoring based on Node affinity rules^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
- TaintTolerationPriority: Scoring based on how well the Pod tolerates Node taints^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
- InterPodAffinityPriority: Scoring based on inter-Pod affinity rules^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Administrators can configure the scheduler to enable or disable specific strategies and adjust their weights via a ConfigMap or configuration file^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].