Browse topics

Resource requests and limits

Version
1.37
Review
Draft

Summary#

The scheduler places a Pod using its resource requests. Limits cap a running container: CPU use above the limit is throttled, and exceeding the memory limit can get a process killed for being out of memory. The settings also determine the Pod's QoS class.

Syntax#

yaml
resources:
  requests:
    cpu: 100m
    memory: 32Mi
  limits:
    cpu: 200m
    memory: 64Mi

Example#

The last two commands read limits from the container's control group. They require Linux nodes with cgroup v2 and CPU quota enforcement enabled.

sh
(
  set -eu
  ns="ref-resources-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl apply -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: burstable
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
      resources:
        requests:
          cpu: 100m
          memory: 32Mi
        limits:
          cpu: 200m
          memory: 64Mi
---
apiVersion: v1
kind: Pod
metadata:
  name: guaranteed
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
      resources:
        limits:
          cpu: 100m
          memory: 32Mi
---
apiVersion: v1
kind: Pod
metadata:
  name: unset
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
EOF
  kubectl wait -n "$ns" --for=condition=Ready pod --all --timeout=120s >/dev/null

  kubectl get -n "$ns" pods --sort-by=.metadata.name \
    -o custom-columns='NAME:.metadata.name,QOS:.status.qosClass,CPU_REQUEST:.spec.containers[0].resources.requests.cpu,MEMORY_LIMIT:.spec.containers[0].resources.limits.memory'
  kubectl exec -n "$ns" burstable -- cat /sys/fs/cgroup/memory.max
  kubectl exec -n "$ns" burstable -- awk '$1 != "max" {print 1000 * $1 / $2 "m"}' /sys/fs/cgroup/cpu.max
)

Expected output#

Text
NAME         QOS          CPU_REQUEST   MEMORY_LIMIT
burstable    Burstable    100m          64Mi
guaranteed   Guaranteed   100m          32Mi
unset        BestEffort   <none>        <none>
67108864
200m

64Mi appears as bytes. Dividing the CPU quota by its period gives the fraction of a CPU, which the example prints in millicpu. The guaranteed Pod received requests equal to its limits.

Common mistakes#

OOMKilled as the reason a container terminated, often with exit code 137, means that the kernel stopped it for running out of memory. Compare its memory use with the limit, then raise the limit or reduce what the process uses.

Suffixes are case sensitive. A memory value of 400m means 0.4 bytes; use 400Mi for mebibytes or 400M for megabytes. In CPU values, m means one thousandth of a CPU.

Version applicability#

The example targets Kubernetes 1.37. In-place resize of a running Pod's CPU and memory through the resize subresource is stable since 1.35. Pod-level spec.resources is a beta feature, enabled by default since 1.34.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0