Home KubernetesKubernetes Resource Limits: Stop OOMKilled Pods Fast

Kubernetes Resource Limits: Stop OOMKilled Pods Fast

by Knowledge Base Linux
Kubernetes resource limits with CPU and memory gauges controlling pods on a cluster node

A pod that works fine in testing can still take down its neighbours in production — by eating all the node’s memory until the kernel kills something, or by starving other pods of CPU. Kubernetes resource limits and requests are the mechanism that prevents this. Requests tell the scheduler how much a pod needs; limits cap how much it may use. Get them wrong and you get pending pods, throttled applications, or the dreaded OOMKilled status.

Kubernetes resource limits dividing CPU and memory among pods on a node

This guide explains requests and limits in practical terms, shows how to set them, and how to tune them from real usage data. If you have not set up health checks yet, read our Kubernetes probes guide first, and for scaling based on usage, see our Kubernetes HPA guide.

Prerequisites

  • A working Kubernetes cluster (local or cloud) with kubectl configured.
  • A deployment you can edit and redeploy safely.
  • The Metrics Server installed if you want kubectl top data (kubectl top pods should return numbers, not an error).

Kubernetes Resource Limits and Requests Explained

CPU is measured in cores and millicores: 500m means half a CPU core, 1 means one full core. Memory is measured in bytes with suffixes such as Mi (mebibytes) and Gi (gibibytes). Requests and limits behave differently for each:

  • Requests are what the scheduler uses for placement. A pod is only scheduled on a node that has at least the requested CPU and memory free. Requests are a reservation, not a cap.
  • Limits are the hard ceiling. A container that exceeds its CPU limit is throttled; a container that exceeds its memory limit is killed with OOMKilled.
  • CPU is compressible: throttling slows a container but does not kill it. Memory is incompressible: there is no graceful slowdown, so exceeding the limit means termination.

That asymmetry drives the standard advice: set memory limits carefully and generously, and treat CPU limits as a guard against runaway processes rather than a tight budget.

Setting Kubernetes Resource Limits in a Deployment

Requests and limits live under resources in each container spec. Here is a typical web application container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          resources:
            requests:
              cpu: "250m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"

Apply it with kubectl apply -f deployment.yaml, then confirm the values took effect:

kubectl describe pod -l app=web | grep -A 4 "Limits\|Requests"
kubectl get pods -l app=web

If you omit requests entirely, the scheduler treats the pod as needing zero resources, which leads to overpacked nodes. If you omit limits, a single misbehaving container can consume the whole node. Setting both, even as rough first estimates, is better than setting neither.

Diagnosing OOMKilled and CPU Throttling

When a container is killed for exceeding its memory limit, Kubernetes reports OOMKilled as the last termination reason. Check it with:

kubectl get pods
kubectl describe pod <pod-name> | grep -A 5 "Last State"

If you see Reason: OOMKilled with an exit code of 137, the fix is to raise the memory limit — or fix the memory leak, if the container’s usage grows without bound. Raising the limit on a leaking container only delays the kill; watching kubectl top pods over time tells you which case you have.

CPU throttling is sneakier because nothing crashes. The symptom is higher latency under load despite low reported CPU usage. Check throttling with kubectl top pods combined with container metrics, or look for container_cpu_cfs_throttled_seconds_total in Prometheus if you run it. If a latency-sensitive service is throttled, raise its CPU limit or remove it — many teams run latency-critical services with no CPU limit at all, relying on requests for scheduling fairness.

Tuning Kubernetes Resource Limits from Real Usage

Guessing limits once and forgetting them is how pods end up with ten times the memory they need, or a tenth of what they need. Tune from observation instead. First, gather a week of usage data:

kubectl top pods -A --sort-by=memory
kubectl top pods -A --sort-by=cpu

Then apply a simple rule of thumb. Set requests close to the container’s typical (median) usage, so the scheduler packs nodes efficiently. Set memory limits at roughly the observed peak plus 20–30% headroom, so normal spikes do not kill the pod. For CPU, set limits at peak plus headroom only for batch or background workloads; for user-facing services, consider leaving CPU limits unset and relying on requests plus monitoring.

For ongoing tuning, the Vertical Pod Autoscaler (VPA) in recommendation mode suggests requests and limits from historical usage without applying them. Run it in recommendation mode first, compare its suggestions with your measurements for a week, and only then let it adjust values automatically — and never use VPA and HPA on the same metric for the same workload, because the two controllers will fight each other.

Namespaces, Quotas, and LimitRanges

Per-pod settings are only half the story. A ResourceQuota caps total usage across a namespace, so one team’s runaway deployment cannot starve the rest of the cluster. A LimitRange supplies defaults and min/max bounds for every pod in the namespace, so nothing runs without limits by accident:

apiVersion: v1
kind: LimitRange
metadata:
  name: default-limits
  namespace: team-a
spec:
  limits:
    - default:
        cpu: "500m"
        memory: "512Mi"
      defaultRequest:
        cpu: "250m"
        memory: "256Mi"
      type: Container

Apply quotas and limit ranges per team namespace as part of cluster onboarding, not after the first incident. They are cheap insurance, and they make resource hygiene the default instead of a favour teams must remember.

Troubleshooting Common Problems

Pods stuck in Pending. The scheduler cannot find a node with enough free capacity for the requests. Check with kubectl describe pod (look for “Insufficient cpu” or “Insufficient memory”) and either lower the requests or add node capacity.

Repeated OOMKilled after raising the limit. Suspect a memory leak or an unbounded cache. Profile the application, check for caches without size caps, and watch usage grow over hours with kubectl top before raising the limit again.

High latency but low CPU in metrics. Classic CPU throttling. Raise or remove the CPU limit for that container and re-measure latency.

HPA not scaling as expected. HPA on CPU utilization divides usage by the request, so a missing or wrong request makes the percentage meaningless. Set sensible CPU requests before trusting HPA percentages.

Conclusion

Kubernetes resource limits are a contract with the scheduler and the kernel: requests say what your pod needs, limits say what it may never exceed, and memory violations are fatal while CPU violations merely slow things down. Start with requests at typical usage and memory limits at peak plus headroom, tune from kubectl top data, and enforce defaults with LimitRanges and quotas. The authoritative reference is the Kubernetes resource management documentation. Done well, limits are invisible — pods stay up, neighbours stay healthy, and nobody pages you about a node that ran out of memory.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.