Sub topic 11
Requests, limits, and QoS classes
This chapter assumes you can read a Pod manifest and know that a node is a machine that runs Pods. It covers how a container declares the CPU and memory it needs, and how Kubernetes uses those numbers.
Requests and limits
A request is the amount of a resource the container is guaranteed. The scheduler adds up the requests of the Pods already on a node. It places a new Pod only on a node with enough unreserved capacity.
A limit is the most the container may use. Kubernetes enforces it at runtime.
CPU is measured in cores, and 500m means 500 millicores, or half a core. Memory is measured in bytes, with suffixes such as Mi and Gi.
The two resources behave differently when a container hits its limit:
| Resource | Type | At the limit |
|---|---|---|
| CPU | Compressible | The container is throttled and runs slower. |
| Memory | Incompressible | The container is killed with `OOMKilled` (exit code 137). |
The init-container example earlier in this tutorial sets a CPU request, a memory request, and a memory limit, but no CPU limit. Many teams leave CPU limits off on purpose. Throttling can add latency even when the node has idle CPU.
QoS classes
Kubernetes assigns each Pod a Quality of Service (QoS) class from its requests and limits. You never set the class yourself. It decides which Pods are evicted first when a node runs low on memory.
- Guaranteed: every container has CPU and memory limits, and the requests equal them. These Pods are evicted last.
- Burstable: at least one container has a request or limit, but the Pod does not meet the Guaranteed rules. Eviction order depends on how far each Pod exceeds its requests.
- BestEffort: nothing is set. These Pods are evicted first.
Example: a Guaranteed Pod
apiVersion: v1
kind: Pod
metadata:
name: web-guaranteed
spec:
containers:
- name: nginx
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 250m
memory: 128MiCheck the class that Kubernetes assigned:
kubectl apply -f pod.yaml
kubectl get pod web-guaranteed -o jsonpath='{.status.qosClass}'The output is Guaranteed. If you remove the cpu limit, the class becomes Burstable.
Common errors
- A Pod stuck in
Pending.kubectl describe podshowsInsufficient cpuorInsufficient memory. No node has enough unreserved capacity for the requests. Lower the requests or add capacity. OOMKilledrestarts. The memory limit is lower than the application needs. Raise the limit, or fix the leak.- Only a limit is set. Kubernetes copies the limit into the request. This can reserve more than you intended.
- Requests set far too high. The scheduler reserves capacity the Pod never uses, and nodes look full while they sit idle.
Exercise
A Pod has one container with a memory request of 64Mi, a memory limit of 128Mi, and no CPU settings. Which QoS class does it get, and what happens if the container allocates 200Mi of memory?
Show answer
It is Burstable, because it sets some resources but not equal CPU and memory requests and limits. The container exceeds its 128Mi memory limit, so it is killed with OOMKilled (exit code 137). The kubelet then restarts it according to the Pod's restartPolicy.