Workloads: Deployments, rollouts and jobs · 5 MIN READ

Resource requests and limits, CPU throttling, OOMKills and QoS classes

Set requests and limits deliberately, predict how the scheduler and the kernel use them, recognise CPU throttling and OOMKilled containers, and know which QoS class each workload gets.

Without resource settings, the scheduler places Pods as if they cost nothing and the node discovers the truth under load. Requests tell the scheduler what to reserve; limits tell the kernel where to stop a container. CPU and memory behave very differently at that limit: one slows down, the other is killed.

kubernetes · A MAP OF THE IDEA

  1. Measure usage under load
  2. Requests from typical usage
  3. Memory limit near the peak
  4. Watch top and OOMKilled

Useful when

  • The scheduler never overcommits what Pods request.
  • Guaranteed Pods are the last to be evicted under pressure.

Watch out

  • CPU limits throttle latency even at low average usage.
  • A memory limit below the working set means an OOMKill loop.

Implementation & operation cost: Load tests and monitoring to learn real usage, and a periodic review of every workload's requests and limits.

01

Requests reserve, limits cap

A request is what the scheduler reserves: a Pod fits on a node only if the requests already placed there plus its own stay within the node's allocatable capacity. Actual usage plays no part in that decision. At runtime the CPU request also sets the container's share of CPU when the node is busy. A limit is enforced by the Linux kernel through cgroups and is never used for scheduling. CPU is measured in cores, so 250m is a quarter of a core; memory in bytes, with Mi and Gi as binary units, so 128Mi is not the same as 128M. If you set only a limit, the request defaults to the same value. Requests and limits are set per container, and a Pod's request is essentially the sum of its containers'.

02

Three QoS classes

Kubernetes derives a quality-of-service class from the resources of every container in the Pod. Guaranteed requires a CPU and a memory limit on every container, with requests equal to limits. Burstable means at least one request or limit is set but the Pod does not qualify as Guaranteed. BestEffort means no requests or limits at all. You cannot choose the class directly; kubectl describe pod and status.qosClass show the result.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: api
spec:
  containers:
  - name: api
    image: gopherforge/web:v2
    env:
    - name: GOMEMLIMIT
      value: "230MiB"
    resources:
      requests:
        cpu: 500m
        memory: 256Mi
      limits:
        cpu: 500m
        memory: 256Mi
Read the code

Requests equal limits for both resources, so the Pod is Guaranteed and the scheduler reserves exactly what the kernel will allow. GOMEMLIMIT asks the Go garbage collector to work harder as the heap approaches about 90 percent of the limit instead of growing into an OOMKill. Lowering either request or removing the CPU limit would make the Pod Burstable.

03

CPU is throttled, memory is killed

CPU is compressible. A container that reaches its CPU limit is throttled by the CFS quota: it may use its quota within each 100 ms period and then waits for the next one, so a request needing 30 ms of CPU can take far longer under load even though average usage looks modest. Memory is not compressible. When a container's memory exceeds its limit, the kernel's OOM killer ends the process; the kubelet reports Last State: Terminated, Reason: OOMKilled, Exit Code: 137, which is 128 plus SIGKILL's 9, and restarts it with back-off. Since Go 1.25 the runtime derives GOMAXPROCS from the cgroup CPU limit; set GOMEMLIMIT a little below the memory limit so that the garbage collector gets a chance before the kernel does.

04

QoS classes and eviction order

When a node runs short of memory or disk, the kubelet evicts Pods before the kernel has to kill processes at random. It first considers Pods whose usage exceeds their requests, ordered by Pod priority and then by how far above the request they are. BestEffort Pods request nothing, so any usage counts as excess and they go first; Burstable Pods using more than they asked for follow; Guaranteed Pods and Burstable Pods within their requests are evicted last. The QoS class also sets the oom_score_adj the kernel uses if it must act before the kubelet. Evicted Pods are not restarted in place: their controller creates replacements, possibly on another node.

05

kubectl top and right-sizing

kubectl top pods and top nodes read the Metrics API that metrics-server provides: current usage sampled about every 15 seconds, with no history. Use them to spot a Pod creeping towards its memory limit, and use Prometheus or a similar system for percentiles over days. A sound starting point is a CPU request near typical usage, a memory request near the peak working set, a memory limit equal to or a little above the memory request, and either no CPU limit or a generous one for latency-sensitive services. Revisit the numbers after load tests and significant releases: requests that are too high waste nodes, requests that are too low invite contention and eviction.

06

Try it in the lab terminal

Two Pods show the two failure modes. leak requests 64Mi and is limited to 128Mi while its memory grows by several MiB per second; cluster tick advances virtual time so you can watch it approach the limit. hog asks for six CPUs on a lab node with four allocatable, so no amount of waiting will schedule it. describe pod and describe node show where each decision is recorded, and the last command prints the QoS class of both Pods.

bash
kind create cluster
cat > leak.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: leak
spec:
  containers:
  - name: web
    image: gopherforge/web:leak
    ports:
    - containerPort: 8080
    resources:
      requests:
        cpu: 100m
        memory: 64Mi
      limits:
        memory: 128Mi
EOF
kubectl apply -f leak.yaml
cluster tick 12s
kubectl top pod leak
cluster tick 14s
kubectl get pod leak
kubectl describe pod leak
cat > hog.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: hog
spec:
  containers:
  - name: app
    image: gopherforge/web:v1
    resources:
      requests:
        cpu: "6"
        memory: 256Mi
      limits:
        cpu: "6"
        memory: 256Mi
EOF
kubectl apply -f hog.yaml
kubectl get pod hog
kubectl describe pod hog
kubectl describe node kind-control-plane
kubectl get pods -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass
Read the code

top pod shows leak at 60Mi a few seconds after it starts. Soon after it passes 128Mi, get pod reports CrashLoopBackOff, or OOMKilled if you are quick, with RESTARTS 1, and describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137 and QoS Class: Burstable. hog stays Pending with a FailedScheduling warning: 0/1 nodes are available: 1 Insufficient cpu. The node lists 4000m allocatable CPU, and its Allocated resources section counts only the requests of Pods already placed there. The last command prints Guaranteed for hog and Burstable for leak.

TAKE THIS WITH YOU

Requests decide placement and CPU shares under contention; limits are enforced by the kernel, which throttles CPU and kills on memory. Size memory from measured peaks, treat OOMKilled with exit code 137 as a sizing or leak problem, and know which QoS class each workload gets.

Check your understanding

A Pod restarts repeatedly and describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. What happened?

Go to the source

Progress stays in this browser. No account needed.