Kubernetes · Chapter 13 of 28

Scheduling

By the end of this chapter you can say how the scheduler picks a node for a pod, pin pods to nodes with a nodeSelector or node affinity, keep copies apart or together with pod affinity, spread them over Lucknow's three zones with a rule the scheduler must obey, and read a FailedScheduling message to find out why a pod stays Pending. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Scheduling and teams
  • 4 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

Jhatpat's cluster has three general nodes, one in each zone of the data centre: worker-1 in lucknow-a, worker-2 in lucknow-b, worker-3 in lucknow-c. A zone is a building with its own power and network. When lucknow-a lost power for 20 minutes in August, everything in it stopped. On Dhanteras the cart takes about 1,200 requests a second, and Ritika wants it to survive one zone going dark: three copies, one in each zone, so a cut takes one copy and two keep serving.

So far you have asked for 3 replicas and let the scheduler choose. It usually spreads them, but only as a wish. Here is a test Ammar ran: a nightly reports pod holds most of worker-3's CPU, and then he starts 3 catalog copies that each request 500m:

$ kubectl get pods -l app=catalog -o wide -L topology.kubernetes.io/zone
NAME                       READY   STATUS    RESTARTS   AGE   IP            NODE       ...   ZONE
catalog-6c787fd8c6-jl4tg   1/1     Running   0          3s    10.244.1.23   worker-1   ...   lucknow-a
catalog-6c787fd8c6-pqnbk   1/1     Running   0          2s    10.244.2.23   worker-2   ...   lucknow-b
catalog-6c787fd8c6-rq59j   1/1     Running   0          3s    10.244.1.16   worker-1   ...   lucknow-a

Two copies in lucknow-a, none in lucknow-c. If that happens to the cart on Dhanteras evening and lucknow-a goes dark, two thirds of the cart goes with it. Nothing you know yet can say "one per zone, or not at all". This chapter gives you the rules that can.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    cart.yaml: 3 copies in 3 zones

    Keep one cart copy in each of Lucknow's three zones, with a rule.

    1. Open cart.yaml with nano. It makes the Deployment cart: 3 replicas of jhatpat/cart:2.0, with REDIS_URL redis://redis:6379.

    2. In the pod template, add a rule over the zone label topology.kubernetes.io/zone that selects the pods with app: cart.

    3. The rule must be a must, not a wish: a topology spread constraint with maxSkew 1 and whenUnsatisfiable DoNotSchedule, or a required pod anti-affinity.

    4. Apply the file, and list the pods wide.

    To pass: the rule is in the pod template, and 3 cart pods with the rule are ready, each in a different zone. Pods that land in 3 zones without the rule do not pass.

  2. Task 2

    storefront.yaml: prefer lucknow-b

    Make the storefront prefer the zone lucknow-b, but still run in other zones when it must.

    1. Open storefront.yaml with nano. Keep 3 replicas of jhatpat/storefront:1.2.

    2. In the pod template, add a preferred node affinity, with any weight from 1 to 100.

    3. Its expression: key topology.kubernetes.io/zone, operator In, value lucknow-b.

    4. Apply the file, and wait for the rollout.

    To pass: the preference is in the Deployment, with no required node affinity and no nodeSelector, and 3 pods of the new template are ready, with no old ones left.

  3. Task 3

    rider-tracker.yaml: why it is Pending

    Find out why rider-tracker's 2 pods stay Pending, and fix the node rule.

    1. Describe a Pending rider-tracker pod, or run kubectl get events. Read the FailedScheduling message.

    2. Show the nodes with their jhatpat.in/pool label as a column. Compare the values with the one in the rule.

    3. Fix the value in rider-tracker.yaml. Keep the rule, so rider-tracker must still run on the general pool.

    4. Apply the file, and wait for the rollout.

    To pass: you ran kubectl describe or kubectl get events in the terminal (one that failed counts too), a required rule on jhatpat.in/pool (a required node affinity or a nodeSelector) is still in the Deployment, and 2 rider-tracker pods are ready on general pool nodes.

  4. Task 4 · interview question

    Interview: required or preferred

    Interview question: required or preferred, and what happens when no node fits?

    1. Say when you would make a placement rule required.

    2. Say when you would make it preferred.

    3. Say what happens to a pod when no node fits a required rule.

    4. Write two or three sentences of your own below the notes in answer.txt.

    To pass: answer.txt talks about required rules, preferred rules, and what a pod does when no node fits a required rule.

What you will learn

  • The scheduler: it filters out the nodes a pod cannot use, then scores the rest.
  • nodeSelector: a pod runs only on nodes with these labels.
  • Node affinity: required (a must) and preferred (a wish, with a weight), with operators.
  • Pod affinity and anti-affinity: near or away from other pods, per node or per zone (topologyKey).
  • Topology spread constraints: copies spread evenly over zones or nodes.
  • Pending and FailedScheduling: how to read "0/4 nodes are available: ...".

More in Scheduling and teams

Build "Scheduling" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.