Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 78 ยท Pods

Resource Requests and Limits

Understand CPU and memory requests and limits, millicores and Mi units, QoS classes, OOMKilled, CPU throttling, and a first look at LimitRange and ResourceQuota.

A cluster has a fixed amount of CPU and memory, and many Pods compete for it. Kubernetes needs two numbers from each container to share that capacity sensibly. The first is how much the container needs so the scheduler can find a node with room. The second is how much it is allowed to use so that one greedy container cannot starve its neighbors. These numbers are called requests and limits.

Getting them wrong causes two of the most common production problems: Pods that cannot be scheduled because they ask for too much, and Pods that are killed or slowed down because they were given too little. In this lesson you learn the units, what the scheduler and the kernel each do with the numbers, the three Quality of Service classes, and the difference between a memory limit (which kills) and a CPU limit (which slows). You also meet LimitRange and ResourceQuota, the namespace-level guard rails. The manifests are read-only, and the Python stand-ins calculate the rules for you.

Request versus limit

Request What the scheduler reserves for you.
Limit The most the container may use.
Two enforcers Scheduler for requests, kernel for limits.
1 / 2

Writing requests and limits

Resources are set per container, under resources:

YAML
apiVersion: v1
kind: Pod
metadata:
  name: api
spec:
  containers:
    - name: api
      image: example/api:1.4
      resources:
        requests:
          cpu: 250m
          memory: 256Mi
        limits:
          cpu: "1"
          memory: 512Mi

The Pod's total request is the sum over its containers (plus the larger of any init container, see Init Containers and Sidecars). The scheduler places the Pod only on a node whose unreserved capacity is at least that sum. It looks at requests, never at actual usage (How the Scheduler Works).

CPU units

CPU is measured in cores. 1 is one vCPU (one hyperthread or one core, depending on the machine), and 0.5 is half. Because fractions get awkward, Kubernetes uses millicores: 500m means 500 thousandths of a core. 100m is the smallest value you will use for a small service, and 250m is a quarter of a core. CPU is a compressible resource: when demand exceeds supply, processes wait for their turn, but nothing dies.

Memory units

Memory is measured in bytes with suffixes. The binary suffixes Ki, Mi, Gi are powers of 1024 (so 256Mi is 268,435,456 bytes). The decimal suffixes k, M, G are powers of 1000. Prefer Mi and Gi, which match what the OS reports. Watch out for the lowercase m: 128m means 128 millibytes, which is a tiny fraction of one byte and a bug waiting to happen. Memory is incompressible: you cannot make a process use less memory by waiting.

What a request does

A request has two jobs. It tells the scheduler how much of a node to reserve. It also sets a relative weight for CPU when the node is busy: if two containers compete for CPU, the one with the larger request gets a proportionally larger share. When the node is idle, a container may use more CPU than it requested, up to its limit.

If you set no request, it defaults to the limit (when a limit is set), or to zero. A Pod with zero requests looks free to the scheduler, so many of them can be crammed onto one node, and then they fight for the real resources. That is the cause of mysterious slowness on crowded nodes.

What a limit does

Limits are enforced on the node by the Linux kernel's control groups (the same mechanism behind Docker's --memory and --cpus, see Docker resource limits), and the two kinds behave very differently.

  • Memory limit. If the container's memory use goes above the limit, the kernel's out-of-memory killer ends the process. The container is restarted according to restartPolicy, and kubectl describe pod shows Reason: OOMKilled with exit code 137.
  • CPU limit. The kernel gives the container a time budget per scheduling period (100 ms by default). A limit of 500m is 50 ms of CPU time per 100 ms period. If the container uses its budget early, all its threads are paused until the next period. This is throttling: no crash, but latency spikes, which can be severe for multi-threaded services.
TEXT
Last State:     Terminated
  Reason:       OOMKilled
  Exit Code:    137

A common modern practice is to always set a memory limit (usually equal to the request, so memory behaves predictably) and to be careful with CPU limits, because throttling can hurt latency even when the node has idle CPU. Many teams set CPU requests but no CPU limits on latency-sensitive services. It is a trade-off, not a rule.

Quality of Service classes

From the requests and limits of all containers, Kubernetes assigns each Pod a QoS class. You cannot set it directly. It decides who is evicted first when a node runs out of memory.

Class Condition Eviction order
Guaranteed Every container has CPU and memory requests and limits, and request equals limit for each. Last
Burstable At least one container has a request or a limit, but the Pod is not Guaranteed. Middle
BestEffort No container has any request or limit. First

You can read it with kubectl get pod api -o jsonpath='{.status.qosClass}'. Guaranteed Pods are the most protected, at the price of reserving exactly what they might use. BestEffort Pods are fine for throwaway work and a bad idea for anything you care about.

A QoS class calculator

This stand-in parses the units and classifies Pods using the rules above. It is a good way to check your understanding of the edge cases, such as a Pod where one container is fully specified and another has nothing.

Python Units and QoS classes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the last two rows. A Pod where one container is bare is only Burstable, even though the other is perfectly specified, because every container must qualify. A Pod that sets only memory (request equal to limit) is also Burstable: Guaranteed requires both CPU and memory.

Watching limits act

The second stand-in shows the difference between the two limits. It feeds a container's usage into the rules: memory above the limit is a kill, while CPU above the limit is throttling that stretches the work out. The CPU part uses the real arithmetic: a limit of L millicores is L / 10 milliseconds of CPU time per 100 ms period.

Python OOMKilled versus throttled

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The memory case is a cliff, and the CPU case is a slope. This is why out-of-memory problems show up as restarts, while CPU limits that are too low show up as slow responses with no error at all. If you only watch restarts you will miss throttling. Cluster metrics expose it as container_cpu_cfs_throttled_periods_total (Metrics and Monitoring with Prometheus).

Namespace guard rails: LimitRange and ResourceQuota

Two objects keep resource settings under control for a whole namespace (Namespaces, Quotas and Limit Ranges has the full story).

A LimitRange sets defaults and per-container minimums and maximums. A container with no resources receives the default values, so nothing is accidentally BestEffort:

YAML
apiVersion: v1
kind: LimitRange
metadata:
  name: defaults
  namespace: team-a
spec:
  limits:
    - type: Container
      defaultRequest:
        cpu: 100m
        memory: 128Mi
      default:
        cpu: 500m
        memory: 256Mi
      max:
        memory: 2Gi

A ResourceQuota caps the total a namespace may use, such as the sum of all requests, the number of Pods or the number of Services. Creating a Pod that would push the namespace over its quota is rejected.

YAML
apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-a-quota
  namespace: team-a
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.memory: 16Gi
    pods: "20"

If a quota covers CPU or memory, every new Pod in the namespace must specify those values (directly or through a LimitRange default), otherwise it is refused.

Setting good values

Start from measurement. Run the service under realistic load, watch its usage (kubectl top pod shows the current CPU and memory, once metrics-server is installed), and set the request near typical usage and the memory limit with headroom above the peak. Revisit the numbers regularly, or let a tool such as the Vertical Pod Autoscaler suggest them (Vertical Autoscaling and Cluster Autoscaler). Requests that are too high waste money, because nodes fill up with reservations nobody uses. Requests that are too low cause crowded nodes and surprise evictions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the scheduler use when it decides which node can host a Pod?

    The scheduler compares requests against what is still unreserved on each node. It does not look at real usage or at limits.
  2. 2. A container exceeds its memory limit. What happens?

    Memory cannot be slowed down, so the kernel's OOM killer ends the process. kubectl describe shows OOMKilled with exit code 137.
  3. 3. Which Pod gets the Guaranteed QoS class?

    Guaranteed needs both resources set on every container with request equal to limit. Smaller requests give Burstable, and nothing set gives BestEffort.
  4. 4. What does a CPU limit of 500m do when the container wants more CPU?

    CPU is compressible. The kernel pauses the container's threads once its per-period budget is used up, which shows up as latency rather than errors.
  5. 5. What does memory: 128m mean in a manifest?

    Lowercase m is the milli prefix. For memory you almost always want Mi or Gi.

Summary

  • Requests tell the scheduler what to reserve and weight CPU sharing. Limits are hard caps enforced by the kernel.
  • CPU is measured in cores and millicores (500m), memory in bytes with Mi and Gi (beware lowercase m).
  • Going over a memory limit gets the container OOMKilled, while going over a CPU limit throttles it silently.
  • QoS classes (Guaranteed, Burstable, BestEffort) come from your settings and decide eviction order under memory pressure.
  • LimitRange sets per-container defaults and bounds, and ResourceQuota caps the total for a namespace.

Next, see how Pods get setup work and helpers in Init Containers and Sidecars.