Sub topic 1
Requests and limits: what the scheduler and the kubelet each use
Prerequisites
This chapter assumes you can read and apply YAML manifests with kubectl and know what Pods, Deployments, and Nodes are. You should also know roughly what CPU and memory mean for a container, and that the kube-scheduler picks a node while the kubelet runs the Pod on it.
Two fields, two consumers
Each container can set resources.requests and resources.limits. Different components read them for different purposes.
- A request is the amount the container is guaranteed. The scheduler uses it to decide where a Pod fits.
- A limit is the most the container may use. The kubelet and the container runtime enforce it on the node.
What the scheduler does with requests
The scheduler adds up the requests of all Pods already assigned to a node. It places a new Pod only if its requests fit in what remains of the node's allocatable capacity. Allocatable is the node's total minus what the system and the kubelet reserve.
The scheduler ignores actual usage. A node running idle Pods can still be "full" if their requests add up to its allocatable capacity.
kubectl describe node worker-1 | grep -A 8 "Allocated resources"The output shows total requests and limits as a percentage of allocatable. If a Pod stays Pending, its events will say something like 0/3 nodes are available: 3 Insufficient cpu. That means no node has enough unrequested CPU left. It does not mean the nodes are busy.
What the kubelet does with limits
The kubelet passes the values to the runtime, which writes them to Linux cgroups.
- A CPU request becomes a relative weight. When the CPU is contended, containers share it in proportion to their requests.
- A CPU limit becomes a quota. A container that exceeds it is throttled.
- A memory limit becomes a hard ceiling. A container that exceeds it is killed by the kernel, and the Pod shows
OOMKilled.
The next chapter covers why CPU and memory behave differently.
A worked example
apiVersion: v1
kind: Pod
metadata:
name: resource-demo
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: "250m"
memory: 128Mi
limits:
cpu: "500m"
memory: 256MiThe scheduler reserves 250m CPU and 128Mi of memory on the chosen node. At runtime the container can burst to half a core and 256Mi of memory. If it passes 256Mi, it is killed.
CPU is written in millicores, so 500m is half a core. Memory is written in bytes with suffixes, so Mi means mebibytes.
Defaults and common errors
- If you set a limit but no request, Kubernetes sets the request equal to the limit.
- If you set a request but no limit, the container can use any spare capacity on the node.
- A request larger than the biggest node's allocatable capacity leaves the Pod Pending forever.
- A limit lower than the request is rejected by the API server.
- Requests set far too high waste capacity, because the scheduler reserves them even when unused.
Exercise
A node has 4 CPUs allocatable. Three Pods on it each request 1 CPU but use only 100m each. A new Pod requests 1.5 CPUs. Will it be scheduled on this node, and why?
Show answer
No. The existing requests total 3 CPUs, leaving 1 CPU unrequested. The new Pod needs 1.5, so it does not fit. The scheduler goes by requests, not by actual usage.