Kubernetes Scheduler: How Two Pods Compete for a Node

Outline

Transcript

0:00 Kubernetes groups containers, the isolated places where programs run, into Pods it places together. Its scheduler picks a machine, called a node, for each Pod. Two Pods can fit the same node separately and still risk asking for too much together. We'll trace both Pods through one scheduler, using the Kubernetes docs and the code for one version. The links are in the description. Take a team releasing two Pods that can use only one machine. Each needs more than half its remaining processor capacity, or CPU. Once one gets placed, the other waits for space.

0:32 Yeah, the machine can offer 800 milli-CPU after counting Pods already on the machine. Each new Pod asks for 700. That's how much the scheduler has to account for when picking a machine. 1,000 milli-CPU is one CPU unit. Look at the Kubernetes API, the interface for reading and changing what Kubernetes has recorded. An assigned Pod shows its machine in the Node field. Both fields are empty. When does the first decision begin to count? The scheduler accounts for placement before the API knows about it.

1:05 That gives us two places to look for the same decision. First, Pod A asks for less than the machine has left. Its CPU request is the amount the scheduler counts when deciding where a Pod fits, so it can pick that machine for A. The scheduler code calls this assuming the Pod. The scheduler sets a node name on its own copy and adds it to its cache, the working view of nodes and Pods it keeps in memory. Compare the two rows on screen. The API still has no assigned node for A. In the scheduler cache, A is already counted against that machine.

1:43 That takes us from 800 down to 100. That empty field tells us nothing about how much CPU is left. B doesn't get another 700 to spend. Let's follow B's check while A is still waiting. Now carry that first choice forward. The scheduler starts binding A in the background. Binding is the API operation that records the chosen machine on the Pod. While that runs, the scheduler can start choosing a machine for B. It makes those choices one Pod at a time. It can have more than one binding running. My first thought was to hold off choosing for B until A's write finished.

2:23 That gives B a clear answer, with the cost of waiting for the write. What does its check use if it starts sooner? It checks the scheduler's own view, which includes A's request. B needs 700 and sees only 100 available. So B stays unassigned. Let's finish A's write and see if that changes. Once the binding succeeds, the API has A's chosen machine. For example, a fresh read of Pod A can show node-1 in its assignment. When the scheduler gets word that A has a node, it already has an entry for A. The cache code updates that same entry.

3:02 So now that row stays at 100. The same Pod moved from an assumed placement to an observed one. If the API rejects the write, what gives that capacity back? Now follow that rejection. The scheduler undoes its temporary choice when the API refuses the write and A definitely never got assigned. In the API row, no machine ever showed up for A. The space was held in the scheduler's own count. Once the cache forgets A, it takes A's request out of the machine's total. That brings us back to 800. The change can give Pods on the scheduler's waiting list another chance.

3:40 Consider a Pod already waiting for space, like B. I think I'd be tempted to give that space to B right away. A may still need a node too. The scheduler decides which to try next based on when they can retry and their order on the list. If B gets that next try here, 700 now fits. Earlier, it couldn't find space. A reached the write and hit a different problem. So let's say both node fields are blank when you look. How do you tell B's failed search from A's failed write? Read what the scheduler recorded for each Pod.

4:14 The note about B's attempt can say there wasn't enough CPU. For A, the scheduler's log, the record it writes about its own work, can say the binding attempt failed. So I'd compare the records of those attempts before drawing any conclusions from the blank fields. And when a node is recorded, the kubelet, the agent on that machine, can start the assigned containers. Thanks for listening to Learning Podcasts.