TExcelTAD
Home › Kubernetes › Requests, Limits & QoS

Requests, Limits & QoS

Every container can declare how much CPU and memory it needs. These numbers drive scheduling, protect neighbours, and decide who gets evicted first when a node runs short.

yaml
containers:
  - name: api
    image: ghcr.io/acme/api:2.3.1
    resources:
      requests:
        cpu: 250m          # 0.25 of a CPU core
        memory: 256Mi
      limits:
        cpu: "1"
        memory: 512Mi
Request Limit
Meaning Guaranteed amount, reserved on the node Hard maximum
Used by The scheduler (does the Pod fit?) The kernel (cgroups) at run time
CPU over the limit – Throttled (slowed down), never killed
Memory over the limit – Container is OOM-killed (exit code 137) and restarted

Units

  • CPU: 1 = one core (vCPU), 500m = 500 millicores = half a core. 0.1 = 100m.
  • Memory: binary suffixes Ki, Mi, Gi (powers of 1024) or decimal k, M, G (powers of 1000). 128Mi ≠ 128M!
python · try it yourself
SUFFIX = {"Ki": 2**10, "Mi": 2**20, "Gi": 2**30, "Ti": 2**40, "k": 10**3, "M": 10**6, "G": 10**9, "T": 10**12}

def cpu_millicores(q):
    return int(q[:-1]) if q.endswith("m") else round(float(q) * 1000)

def memory_bytes(q):
    for s in sorted(SUFFIX, key=len, reverse=True):
        if q.endswith(s):
            return int(float(q[:-len(s)]) * SUFFIX[s])
    return int(q)

for q in ["250m", "1", "0.1", "1.5"]:
    print(f"cpu {q:5} = {cpu_millicores(q)}m")
for q in ["128Mi", "128M", "1Gi", "1G", "512Ki"]:
    print(f"memory {q:6} = {memory_bytes(q):>13,} bytes")
cpu 250m  = 250m
cpu 1     = 1000m
cpu 0.1   = 100m
cpu 1.5   = 1500m
memory 128Mi  =   134,217,728 bytes
memory 128M   =   128,000,000 bytes
memory 1Gi    = 1,073,741,824 bytes
memory 1G     = 1,000,000,000 bytes
memory 512Ki  =       524,288 bytes

How scheduling uses requests

A node's allocatable capacity (total minus what the system reserves) is divided by the requests of the Pods on it, not their actual usage. If the requests don't fit, the Pod stays Pending with an event like 0/3 nodes are available: 3 Insufficient cpu.

python · try it yourself
node = {"cpu": 4000, "memory": 8 * 1024}      # allocatable: millicores, MiB
pods = [("api-1", 500, 1024), ("api-2", 500, 1024), ("worker", 2000, 2048), ("cache", 500, 3072), ("report", 1000, 512)]

used_cpu = used_mem = 0
for name, cpu, mem in pods:
    if used_cpu + cpu <= node["cpu"] and used_mem + mem <= node["memory"]:
        used_cpu += cpu
        used_mem += mem
        print(f"{name:7} fits     cpu {used_cpu:4}/{node['cpu']}m  memory {used_mem:4}/{node['memory']}Mi")
    else:
        reason = "Insufficient cpu" if used_cpu + cpu > node["cpu"] else "Insufficient memory"
        print(f"{name:7} Pending  ({reason})")
api-1   fits     cpu  500/4000m  memory 1024/8192Mi
api-2   fits     cpu 1000/4000m  memory 2048/8192Mi
worker  fits     cpu 3000/4000m  memory 4096/8192Mi
cache   fits     cpu 3500/4000m  memory 7168/8192Mi
report  Pending  (Insufficient cpu)

QoS classes

From requests and limits, Kubernetes assigns each Pod a Quality of Service class:

Class Rule Eviction priority under node pressure
Guaranteed Every container has CPU and memory limits, and requests equal limits Evicted last
Burstable At least one container has a CPU or memory request or limit, but not Guaranteed In between
BestEffort No container has any requests or limits Evicted first
python · try it yourself
def qos_class(containers):
    any_set = False
    guaranteed = True
    for c in containers:
        req, lim = c.get("requests", {}), c.get("limits", {})
        if req or lim:
            any_set = True
        for res in ("cpu", "memory"):
            if res not in lim:
                guaranteed = False
            elif req.get(res, lim[res]) != lim[res]:   # a missing request defaults to the limit
                guaranteed = False
    if not any_set:
        return "BestEffort"
    return "Guaranteed" if guaranteed else "Burstable"

examples = {
    "no resources": [{}],
    "requests == limits": [{"requests": {"cpu": "500m", "memory": "256Mi"}, "limits": {"cpu": "500m", "memory": "256Mi"}}],
    "limits only": [{"limits": {"cpu": "1", "memory": "1Gi"}}],
    "requests < limits": [{"requests": {"cpu": "250m", "memory": "256Mi"}, "limits": {"cpu": "1", "memory": "512Mi"}}],
    "memory only": [{"requests": {"memory": "128Mi"}}],
    "one container unset": [{"limits": {"cpu": "1", "memory": "1Gi"}}, {}],
}
for name, containers in examples.items():
    print(f"{name:20} -> {qos_class(containers)}")
no resources         -> BestEffort
requests == limits   -> Guaranteed
limits only          -> Guaranteed
requests < limits    -> Burstable
memory only          -> Burstable
one container unset  -> Burstable

Note "limits only": when you set a limit without a request, the request defaults to the limit, so that Pod is Guaranteed.

bash
kubectl get pod api-7d9f-x2k4p -o jsonpath='{.status.qosClass}'
kubectl top pods                 # actual usage (needs metrics-server)
kubectl describe node demo-worker | grep -A6 'Allocated resources'

Practical advice

  • Always set requests from real usage measurements (e.g. a little above the p95 of kubectl top). Without requests, the scheduler packs Pods blindly.
  • Always set a memory limit: a leak in one Pod shouldn't take down the node. Many teams set memory request = limit.
  • CPU limits are debated: they cause throttling even when the node is idle. A common choice is CPU requests without CPU limits, so Pods can burst into spare capacity.
  • Exit code 137 with reason OOMKilled in kubectl describe pod means "raise the memory limit or fix the leak".

Test yourself

What happens when a container exceeds its CPU limit?

  1. It is OOM-killed
  2. It is throttled
  3. The Pod is evicted
  4. Nothing, CPU limits are ignored

Which values does the scheduler use to decide whether a Pod fits on a node?

  1. Limits
  2. Requests
  3. Current usage
  4. Image size

A Pod's only container has requests: {cpu: 100m} and no limits. What is its QoS class?

  1. Guaranteed
  2. Burstable
  3. BestEffort
  4. Critical