All guides

Kubernetes requests and limits: set them from what you measure

The scheduler places a pod by its requests, and the kernel enforces its limits. A CPU limit throttles and a memory limit can kill, so set both from measured usage.

Lines of code on a monitor, seen at an angle

What a request does

The scheduler places a pod by its requests. For each resource, the sum of the requests of the containers scheduled on a node must stay below the node's capacity. Actual usage plays no part: the scheduler refuses the node even when usage on it is very low. If no node fits, the pod remains unscheduled, and each failed attempt produces a FailedScheduling event.

When a node is contended, a workload with a larger CPU request gets more CPU time than one with a small request.

What a limit does

On Linux nodes the kernel enforces limits with cgroups, and the two resources behave differently.

  • A CPU limit throttles. It is a hard ceiling on CPU time. When the container exceeds it within a scheduling interval, the kernel makes the container wait before it runs again. Container runtimes do not terminate containers for excessive CPU usage.
  • A memory limit kills. When a container uses more memory than its limit, the kernel may terminate it, and kubectl describe pod shows OOMKilled as the reason for the last state. Enforcement is reactive: terminations happen only when the kernel detects memory pressure.

If you set a limit and no request, and no admission-time mechanism applies a default request, Kubernetes copies the limit and uses it as the request.

Replace the amounts in this container with your own measurements:

YAML

containers:
  - name: my-app
    image: registry.example.com/my-app:1.4.2
    resources:
      requests:
        cpu: 250m
        memory: 256Mi
      limits:
        memory: 256Mi

250m is 0.25 CPU. For memory, mind the case of the suffix: the documentation warns that 400m is a request for 0.4 bytes, where 400Mi or 400M was meant.

QoS classes

Kubernetes assigns every pod a Quality of Service class, based on the requests and limits of its containers.

  • Guaranteed: every container has a CPU and a memory request and limit, and each limit equals its request.
  • Burstable: the pod does not meet the Guaranteed criteria, and at least one container has a CPU or memory request or limit.
  • BestEffort: no container has any CPU or memory request or limit.

When a node runs out of resources, Kubernetes evicts BestEffort pods first, then Burstable, then Guaranteed. The task page Configure Quality of Service for Pods reads a pod's class this way:

Shell

kubectl get pod <pod-name> -o jsonpath='{.status.qosClass}{"\n"}'

The CPU limit choice

A CPU limit is optional. The Kubernetes documentation describes each choice.

  • No CPU limit. The container has no upper bound on the CPU it can use and could use all the CPU available on its node, unless a LimitRange in the namespace assigns a default limit.
  • A CPU limit above the request. The documentation gives two effects: the pod can have bursts of activity that use CPU that happens to be available, and the burst is limited to a reasonable amount.
  • Limits equal to requests for CPU and memory in every container. The pod is Guaranteed, and Guaranteed pods can also use exclusive CPUs under the static CPU management policy.

A container with no memory limit could use all the memory on its node, which could invoke the OOM killer.

Measure with kubectl top

Shell

kubectl top pod --containers --namespace=<namespace>
kubectl top pod --sort-by=memory --all-namespaces

kubectl top fetches recent CPU and memory usage from the Metrics Server, which must be installed in the cluster. The kubectl reference describes the command as a tool for spot checks and points to a dedicated monitoring solution for historical analysis.

Vertical Pod Autoscaler recommendations

The Vertical Pod Autoscaler (VPA) analyzes current and historical usage and computes recommended requests. It is a custom resource that must be installed separately, and it requires a metrics source such as the Metrics Server. In Off mode it generates recommendations and does not apply them:

YAML

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: my-app
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  updatePolicy:
    updateMode: "Off"

The recommendation is stored in the object's .status.recommendation field: a target, a lower bound and an upper bound. The project's quick start reads it with kubectl describe vpa.

Initial sets requests only when pods are first created. Recreate evicts pods whose requests differ significantly from the recommendation. InPlaceOrRecreate attempts to update running pods and falls back to eviction. Auto is deprecated since VPA 1.4.0 and is an alias for Recreate. InPlace never evicts and is an alpha feature in VPA 1.7.0.

InPlaceOrRecreate relies on in-place pod resize, stable since Kubernetes v1.35, which modifies the CPU and memory of a running pod through the pod's resize subresource.

First steps

  1. Find the pods in which no container sets a CPU or memory request or limit. They are BestEffort, the class Kubernetes evicts first.
  2. Check usage with kubectl top pod --containers, and read the recommendation of a VPA in Off mode.
  3. Set requests and limits from those numbers.
  4. Watch two signals. A FailedScheduling event with the message insufficient cpu means no node has room for the CPU request. A last state of OOMKilled tells you the container went for more memory than its limit allows.
  5. In a shared namespace, a LimitRange can set default requests and limits, and a ResourceQuota limits the aggregate consumption of the namespace. Under a quota for CPU or memory, a new pod that sets neither a request nor a limit for that resource may be rejected.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides