|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes v1.32: QueueingHint Brings a New Possibility to Optimize Pod Scheduling" |
| 4 | +date: 2024-xx-xxT00:00:00-08:00 |
| 5 | +slug: scheduler-queueinghint |
| 6 | +--- |
| 7 | + |
| 8 | +**Author:** [Kensei Nakada](https://github.com/sanposhiho) (Tetrate.io) |
| 9 | + |
| 10 | +The Kubernetes [scheduler](/docs/concepts/scheduling-eviction/kube-scheduler/) is the core |
| 11 | +component that decides which node any new Pods should run on. |
| 12 | +Basically, it schedules Pods **one by one**, |
| 13 | +and thus the larger your cluster is, the more crucial the throughput of the scheduler is. |
| 14 | + |
| 15 | +For the Kubernetes project, the throughput of the scheduler has been an eternal challenge |
| 16 | +over the years, SIG Scheduling have been putting effort to improve the scheduling throughput by many enhancements. |
| 17 | + |
| 18 | +In this blog post, I'll introduce a recent major improvement in the scheduler: a new |
| 19 | +[scheduling context element](/docs/concepts/scheduling-eviction/scheduling-framework/#extension-points) |
| 20 | +named _QueueingHint_. |
| 21 | +We'll go through the explanation of the basic background knowledge of the scheduler, |
| 22 | +and how QueueingHint improves our scheduling throughput. |
| 23 | + |
| 24 | +## Scheduling queue |
| 25 | + |
| 26 | +The scheduler stores all unscheduled Pods in an internal component that we - SIG Scheduling - |
| 27 | +call the _scheduling queue_. |
| 28 | + |
| 29 | +The scheduling queue is composed of three data structures: _ActiveQ_, _BackoffQ_ and _Unschedulable Pod Pool_. |
| 30 | +- ActiveQ: It holds newly created Pods or Pods which are ready to be retried for scheduling. |
| 31 | +- BackoffQ: It holds Pods which are ready to be retried, but are waiting for a backoff period, which depends on the number of times the scheduled attempted to schedule the Pod. |
| 32 | +- Unschedulable Pod Pool: It holds Pods which should not be scheduled for now, because they have a Scheduling Gate or because the scheduler attempted to schedule them and nothing has changed in the cluster that could make the Pod schedulable. |
| 33 | + |
| 34 | +## Scheduling framework and plugins |
| 35 | + |
| 36 | +The Kubernetes scheduler is implemented following the Kubernetes |
| 37 | +[scheduling framework](/docs/concepts/scheduling-eviction/scheduling-framework/). |
| 38 | + |
| 39 | +And, each scheduling requirements are implemented as a plugin. |
| 40 | +(e.g., [Pod affinity](/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity) |
| 41 | +is implemented in the `PodAffinity` plugin.) |
| 42 | + |
| 43 | +The first phase, called the _scheduling cycle_, takes Pods from activeQ **one by one**, runs all plugins' logic, |
| 44 | +and lastly decides in which Node to run the Pod, or concludes that the Pod cannot go to anywhere for now. |
| 45 | + |
| 46 | +If the scheduling is successful, the second phase, called the _binding cycle_, binds the Pod with |
| 47 | +the Node by communicating the decision to the API server. |
| 48 | +But, if it turns out that the Pod cannot go to anywhere during the scheduling cycle, |
| 49 | +the binding cycle isn't executed; instead the Pod is moved back to the scheduling queue. |
| 50 | +Although there are some exceptions, unscheduled Pods enter the _unschedulable pod pool_. |
| 51 | + |
| 52 | +Pods in Unschedulable Pod Pool are moved to ActiveQ/BackoffQ |
| 53 | +only when Scheduling Queue identifies changes in the cluster that might be schedulable if we retry the scheduling. |
| 54 | + |
| 55 | +That is a crucial step because scheduling cycle is performed for Pods one by one - |
| 56 | +if we didn't have Unschedulable Pod Pool and kept retrying the scheduling of any Pods, |
| 57 | +multiple scheduling cycles would be wasted for Pods that have no chance to be scheduled. |
| 58 | + |
| 59 | +Then, how do they decide when to move a Pod back into the ActiveQ? How do they notice that Pods might be schedulable now? |
| 60 | +Here QueueingHints come into play. |
| 61 | + |
| 62 | +## QueueingHint |
| 63 | + |
| 64 | +QueueingHint is callback function per plugin to notice an object addition/update/deletion in the cluster (we call them cluster events) |
| 65 | +that may make Pods schedulable. |
| 66 | + |
| 67 | +Let's say the Pod `pod-a` has a required Pod affinity, and got rejected in scheduling cycle by the `PodAffinity` plugin |
| 68 | +because no Node has any Pod matching the Pod affinity specification for `pod-a`. |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +When an unscheduled Pod is put into the unschedulable pod pool, the scheduling queue |
| 73 | +records which plugins caused the scheduling failure of the Pod. |
| 74 | +In this example, scheduling queue notes that `pod-a` was rejected by `PodAffinity`. |
| 75 | + |
| 76 | +`pod-a` will never be schedulable until the PodAffinity failure is resolved somehow. |
| 77 | +The scheduling queue uses the queueing hints from plugins that rejected the Pod, which is `PodAffinity` in the example. |
| 78 | + |
| 79 | +A QueueingHint subscribes to a particular kind of cluster event and make a decision whether an incoming event could make the Pod schedulable. |
| 80 | +Thinking about when PodAffinity failure could be resolved, |
| 81 | +one possible scenario is that an existing Pod gets a new label which matches with `pod-a`'s PodAffinity. |
| 82 | + |
| 83 | +The `PodAffinity` plugin's `QueueingHint` callback checks on all Pod updates happening in the cluster, |
| 84 | +and when it catches such update, the scheduling queue moves `pod-a` to either ActiveQ or BackoffQ. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +We actually already had a similar functionality (called `preCheck`) inside the scheduling queue, |
| 89 | +which filters out cluster events based on Kubernetes core scheduling constraints - |
| 90 | +for example, filtering out node related events when nodes aren't ready. |
| 91 | + |
| 92 | +But, it's not ideal because this hard-coded `preCheck` refers to in-tree plugins logic, |
| 93 | +and it causes issues for custom plugins (for example: [#110175](https://github.com/kubernetes/kubernetes/issues/110175)). |
| 94 | + |
| 95 | +## What's new in v1.29 |
| 96 | + |
| 97 | +Within SIG Scheduling, we have been working on the development of QueueingHint since |
| 98 | +Kubernetes v1.28. |
| 99 | +In v1.28, only one alpha plugin (DRA) supported QueueingHint, |
| 100 | +and in v1.29, some stable plugins started to implement QueueingHints. |
| 101 | + |
| 102 | +QueueingHint is not something user-facing, but we have a feature gate (`SchedulerQueueingHints`) as a safety net |
| 103 | +because QueueingHint changes a critical path of the scheduler and adds some memory overhead, depending on how busy a cluster is. |
| 104 | + |
| 105 | +## Getting involved |
| 106 | + |
| 107 | +These features are managed by Kubernetes [SIG Scheduling](https://github.com/kubernetes/community/tree/master/sig-scheduling). |
| 108 | + |
| 109 | +Please join us and share your feedback. |
| 110 | + |
| 111 | +## How can I learn more? |
| 112 | + |
| 113 | +- [KEP-4247: Per-plugin callback functions for efficient requeueing in the scheduling queue](https://github.com/kubernetes/enhancements/blob/master/keps/sig-scheduling/4247-queueinghint/README.md) |
0 commit comments