🎓 A1Learner
🔍
0 of 51 lessons completed

Kubernetes: Resource Requests and Limits

A resource request is how much CPU and memory a pod asks for. A resource limit is the most a container may use. A pod that passes its memory limit is killed with OOMKilled.

Requests and limits in YAML

A request is a promise of room on a node. A limit is a ceiling. Here the pod asks for 64Mi of memory and may use up to 128Mi.

resources:
  requests:
    memory: "64Mi"
    cpu: "250m"
  limits:
    memory: "128Mi"
    cpu: "500m"

Read what the scheduler saw

The describe output shows both numbers. The scheduler uses the request to pick a node with room. The limit is enforced by the runtime.

kubectl describe pod web-7d9f4b6c8d-abcde

A pod past its memory limit

A pod that uses more memory than its limit is killed. The status reads OOMKilled and the exit code is 137. That is the limit doing its job.

kubectl get pod web-7d9f4b6c8d-abcde

Example

kubectl apply -f web-limits.yaml
kubectl get pod web-7d9f4b6c8d-abcde

Key points

  • Resource request: the CPU and memory a pod asks for, which the scheduler uses to place it
  • Resource limit: the most CPU and memory a pod may use
  • m means millicpu, and 1000m is one CPU. Mi means mebibyte, roughly a megabyte.
  • Use requests so the scheduler puts the pod on a node that can fit it
  • Use limits so one hungry pod cannot starve its neighbours
  • OOMKilled: the state of a container killed for passing its memory limit, exit code 137
  • Exit code: the number a stopped container reports. 137 means killed for memory.

In this example, the first command creates a pod with a 64Mi memory limit from the file. The second command reads the pod back. The pod shows OOMKilled with exit code 137, which proves the container used more memory than its limit allowed.

Note

a CPU limit only slows a pod down, while a memory limit kills it

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

A pod shows OOMKilled with exit code 137. What happened?