Kubernetes · Chapter 12 of 28
Resources and QoS
By the end of this chapter you can tell the scheduler how much CPU and memory a container needs (requests) and the kernel how much it may use (limits), read why a pod stays Pending or was OOMKilled, see what pods and nodes use with kubectl top, and say which pods a busy node evicts first. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running apps
- 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 four nodes have 3800m of CPU and 7600 MiB of memory each for pods. On Dhanteras evening the storefront, cart and orders all scale up at once, and they share those nodes with notifier, whose 1.1 release has a memory leak that grows by 8 MiB a second. Two things must hold that evening: the scheduler must not pack more pods onto a node than it can run, and one leaking app must not starve the cart running next to it.
So far no pod said what it needs. The scheduler, which picks a node for each pod (chapter 1), then has nothing to count: it will happily put the tenth hungry pod on a full node. And nothing stops a leak: notifier would grow until the node itself runs out of memory, and the kernel would kill whatever it picks, the cart included. Requests and limits fix both, and they also decide who goes first when a node is short.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
cart.yaml: requests and limits for the cart
Give the cart requests and limits, so the cluster knows how much it needs.
1. Open cart.yaml with nano.
2. In the cart container, set requests: cpu 100m and memory 128Mi.
3. Set limits: cpu 500m and memory 256Mi.
4. Keep 2 replicas, and apply the file.
To pass: the four values are exactly these amounts (0.1 is the same as 100m, but 128M is not 128Mi), and 2 ready cart pods have them, in the QoS class Burstable.
- Task 2
why.txt: why notifier restarts, and a limit that fits 1.2
Find out why notifier keeps restarting, and run the fixed version.
1. Describe the notifier pod. Read the container's Last State.
2. Write its Reason and its Exit Code in why.txt, below the notes.
3. Change the image to jhatpat/notifier:1.2. It fixes the bug in 1.1 and uses about 120 MiB.
4. Set requests memory 128Mi and limits memory 192Mi, in notifier.yaml or with kubectl set. Apply the change.
To pass: why.txt has the reason and the exit code, the Deployment runs jhatpat/notifier:1.2 with those two memory values, and a notifier 1.2 pod is ready and has not restarted.
- Task 3
pending.txt: a pod that stays Pending, and why
Find out why rider-tracker stays Pending, and fix its request.
1. Describe the rider-tracker pod. Find its FailedScheduling event.
2. Copy the scheduler's message, as kubectl prints it, into pending.txt below the notes.
3. Set rider-tracker's CPU request to 250m. Keep its memory request of 256Mi.
4. Apply the change, in rider-tracker.yaml or with kubectl set.
To pass: pending.txt has the scheduler's message, the Deployment requests 250m CPU and 256Mi memory, and a rider-tracker pod with that request is ready.
- Task 4 · interview question
Interview: what happens at the limit
Interview question: what happens to a container at its CPU limit, and at its memory limit?
1. Say what happens at the CPU limit. Is the container stopped, or only slowed down?
2. Say what happens at the memory limit. Think of notifier's Last State in task 2.
3. Write one or two sentences of your own below the notes in answer.txt.
To pass: answer.txt says what happens at the CPU limit, and what happens at the memory limit.
What you will learn
- Requests: what the scheduler reserves for a container on a node.
- Limits: what the kernel lets the container use.
- Units: CPU in millicores (250m, 1), memory in Mi and Gi (and why M is different).
- Pending with Insufficient cpu or Insufficient memory, and kubectl describe node's Allocated resources.
- OOMKilled: the memory limit, exit code 137 and lastState; CPU above the limit is throttled, not killed.
- QoS classes: Guaranteed, Burstable, BestEffort, and who is evicted first.
- kubectl top for pods and nodes.
More in Running apps
Build "Resources and QoS" 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.