正常情况下,当一个 Pod 调度失败后,它就会被暂时“搁置”起来,直到 Pod 被更新,或者集群状态发生变化,调度器才会对这个 Pod 进行重新调度。
当一个高优先级的 Pod 调度失败后,该 Pod 并不会被“搁置”,而是会“挤走”某个 Node 上的一些低优先级的 Pod 。这样就可以保证这个高优先级 Pod 的调度成功
优先级
提高集群的资源利用率最常见的做法就是采用优先级的方案。
通过给 Pod 设置高优先级,让其比其他 Pod 显得更为重要,通过这种“插队”的方式优先获得调度器的调度
PriorityClass
Kubernetes 在初始化的时候就自带了两个 PriorityClasses
1 2 3 4
[root@k8s01 ~]# kubectl get priorityclass NAME VALUE GLOBAL-DEFAULT AGE system-cluster-critical 2000000000 false 24d system-node-critical 2000001000 false 24d
// HighestUserDefinablePriority is the highest priority for user defined priority classes. Priority values larger than 1 billion are reserved for Kubernetes system use. HighestUserDefinablePriority = int32(1000000000) // SystemCriticalPriority is the beginning of the range of priority values for critical system components. SystemCriticalPriority = 2 * HighestUserDefinablePriority
自定义PriorityClass
high-priority.yaml
1 2 3 4 5 6 7
apiVersion: scheduling.k8s.io/v1 # 使用的 API 版本是 scheduling.k8s.io/v1。这个对象是个集群级别的定义,并不属于任何 namespace,可以被全局使用 kind: PriorityClass metadata: name: high-priority value: 1000000 globalDefault: false # 是否将该 PriorityClass 的数值作为默认值,并将其应用在所有未设置 priorityClassName 的 Pod 上(新增 Pod ,之前的存量pod优先级默认为 0). 整个 Kubernetes 集群中只能存在一个 globalDefault 设为 true 的 PriorityClass 对象。 description: "This priority class should be used for XYZ service pods only."
[root@k8s01 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-low-pc 1/1 Running 0 4m33s
[root@k8s01 ~]# kubectl apply -f nginx-high-priority.yaml pod/nginx-high-pc created
# 调度器会尝试寻找一个节点,通过移除一个或者多个比该 Pod 的优先级低的 Pod, 尝试使目标 Pod 可以被调度。 (当前 k8s01,k8s03节点已设置不能被调度) # kubectl taint nodes k8s01 node-role.kubernetes.io/master=:NoSchedule # kubectl taint nodes k8s03 node-role.kubernetes.io/master=:NoSchedule [root@k8s01 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-high-pc 0/1 Pending 0 7s nginx-low-pc 0/1 Terminating 0 87s # 低优先级的被挤占 [root@k8s01 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-high-pc 1/1 Running 0 12s
非抢占式
并不希望 Pod 被驱逐掉,只是希望可以优先调度
preemptionPolicy.yaml
1 2 3 4 5 6 7 8
apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority-nonpreempting value: 1000000 preemptionPolicy: Never globalDefault: false description: "This priority class will not cause other pods to be preempted."