Resource requests and limits
Sharing a node fairly.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/resources.tar.gz && tar -xzf resources.tar.gz, which creates ~/lab/resources/. SHA-256: b54b5162883e2144aaa903bc1df11fc15451e0e8044b1932e8f86dc9a1ae0a88Every Pod so far has run without saying how much CPU or memory it needs. On a three-node lab that is harmless. On a busy cluster it means the scheduler packs Pods onto nodes blind, and one app with a memory leak can starve its neighbours.
Two numbers per container change that. A request is what the container says it needs: the scheduler only places a Pod on a node with that much left unreserved. A limit is the most it may use, enforced by the node while it runs. CPU and memory limits are enforced very differently, as the broken Pods below show.
How much does a node have?
Each node offers 4 CPUs and about 5.8 GiB of memory. All three are containers on the same machine, so they all report the whole VM. On a real cluster each node reports its own size, minus what the system keeps for itself.
Requests and limits on a Pod
CPU is counted in cores, and m means thousandths, so 250m is a quarter of one CPU. Memory is counted in bytes, with Mi meaning mebibytes:
apiVersion: v1kind: Podmetadata:name: webspec:containers:- name: nginximage: nginx:1.30.6-alpineresources:requests: # what the scheduler reserves on a nodecpu: 250m # a quarter of one CPUmemory: 64Milimits: # the most the container may usecpu: 500mmemory: 128Mi
Allocated resources adds up the requests and limits of every Pod on the node the scheduler chose. The node shows 350m CPU and 114Mi memory requested, more than the web Pod asked for. The rest belongs to system Pods that kind runs on every node. The scheduler only looks at the Requests column. It never checks how much CPU a node is really using at this moment. The note above the table also matters: limits may add up to more than 100%, because Kubernetes lets nodes be overcommitted on limits.
Over the memory limit
This Pod reads /dev/zero. tail waits for the end of a line, and /dev/zero never sends one, so it keeps buffering in memory until something stops it:
# Reads /dev/zero, which never ends a line, so tail keeps buffering until it hits the limit.apiVersion: v1kind: Podmetadata:name: hogspec:containers:- name: hogimage: busybox:1.38.0command: ["tail", "/dev/zero"]resources:limits:memory: 32Mi
Apply it, then run kubectl get pod hog every few seconds until RESTARTS shows 1, and ask describe why:
RESTARTS went to 1 within about five seconds. Last State says why: Terminated, reason OOMKilled, exit code 137. That code is 128 plus 9, the number of the KILL signal. When a container goes over its memory limit, the kernel kills it, with no warning and no chance to clean up. The kubelet then restarts it, it fills up again, and before long it ends up in CrashLoopBackOff. If you see exit code 137 with OOMKilled, either the limit is too low or the app has a leak.
A request no node can meet
Requests protect the node, but a request that is too big means the Pod never runs:
apiVersion: v1kind: Podmetadata:name: bigspec:containers:- name: nginximage: nginx:1.30.6-alpineresources:requests:cpu: "64" # 64 CPUs: more than any node in the lab has
The Pod is Pending and stays that way. The event reads like a small report: of 3 nodes, the control-plane node has a taint that keeps apps off it, and the 2 workers have Insufficient cpu. Preemption, which can evict lower-priority Pods to make room, would not help either, since no node has 64 CPUs at all. Nothing else happens. The Pod waits until a big enough node appears or you lower the request. Pending with Insufficient cpu or Insufficient memory is one of the most common reasons a new Pod will not start.
QoS classes
Kubernetes also gives every Pod a QoS class, worked out from its requests and limits. A Pod is Guaranteed when every container has CPU and memory requests equal to its limits, BestEffort when no container sets any request or limit, and Burstable otherwise.
# Burns CPU in a loop, capped at a fifth of one CPU.apiVersion: v1kind: Podmetadata:name: spinspec:containers:- name: spinimage: busybox:1.38.0command: ["sh", "-c", "while true; do :; done"]resources:requests:cpu: 200mmemory: 16Milimits:cpu: 200mmemory: 16Mi
spin sets equal requests and limits for both, so it is Guaranteed. web has requests below its limits, so it is Burstable. hog set only a memory limit. Kubernetes copies a limit into the request when the request is missing, but hog has no CPU values at all, so it is Burstable too.
The class matters when a node runs short of memory. The kernel's OOM killer uses it: Kubernetes sets each container's oom_score_adj from the class, so BestEffort containers are killed first and Guaranteed ones last. The kubelet's own eviction, which removes whole Pods before memory runs out completely, does not sort by class. It looks first at whether a Pod uses more than it requested, then at the Pod's priority, then at how far over its request it is. A BestEffort Pod requested nothing, so any memory it uses counts as over, and it ends up near the front of the line anyway.
CPU limits throttle
spin runs a busy loop that would happily use a whole CPU. Its limit is 200m. Look at its cgroup, the kernel feature that enforces the limit:
cpu.max reads 20000 100000: in every 100 millisecond period the container may run for 20 milliseconds, which is 200m. Once it has used its share, the kernel pauses it until the next period. nr_throttled counts those pauses, already a handful in the first few seconds. And the Pod is still Running with 0 restarts. That is the big difference from memory: going over a CPU limit makes a container slow, never dead. A container that is often throttled shows up as slow responses, not as errors.
Hands-on
1. Start a Pod with no resources at all: kubectl run plain --image=nginx:1.30.6-alpine.
2. Wait for it: kubectl wait --for=condition=Ready pod/plain --timeout=60s.
3. Read its class: kubectl get pod plain -o jsonpath='{.status.qosClass}{"\n"}'.
4. Clean up with kubectl delete pod web hog big spin plain, then check with kubectl get pods.
What you should see: step 2 prints pod/plain condition met, and step 3 prints BestEffort, since kubectl run sets no requests or limits. Its memory use is all above its zero request, so it is among the first Pods the kubelet would evict under memory pressure. Step 4 removes all five Pods, including big, which never ran, and kubectl get pods says No resources found in default namespace.
Remember
Requests decide where a Pod is placed, and a request no node can meet leaves it Pending. Going over a memory limit gets a container OOMKilled with exit code 137, while going over a CPU limit only slows it down.
describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. What happened?spin kept running with 0 restarts.hog reached its 32Mi limit and the kernel killed it; 137 is 128 plus signal 9.kubectl get pod show?2 Insufficient cpu.hog set only a memory limit and its class was Burstable.