Kubernetes Resource Limits

Master Kubernetes requests, limits, QoS classes (Guaranteed, Burstable, BestEffort), CPU throttling, OOMKilled, and interview questions.

Resource management in Kubernetes determines how containers request and consume compute resources (CPU and memory) across worker nodes. By configuring requests (used by the kube-scheduler for Pod placement) and limits (enforced at runtime by Linux cgroups), teams prevent noisy-neighbor contention, prevent node starvation, and establish predictable Quality of Service (QoS) tiers.

Requests vs Limits

Kubernetes treats CPU and memory fundamentally differently because CPU is compressible (throttled when saturated) while memory is incompressible (cannot be throttled; containers exceeding limits are terminated):

  • requests: The minimum guaranteed amount of CPU or memory allocated to a container. The kube-scheduler uses the sum of container requests to decide whether a node has enough allocatable capacity to accept the Pod.
  • limits: The hard maximum ceiling a container is allowed to consume. The container runtime configures Linux cgroups to enforce these boundaries.

Quality of Service (QoS) Classes

Kubernetes automatically assigns one of three QoS classes to every Pod based on the relationship between its requests and limits:

QoS Class Condition Eviction Priority Under Pressure Best Suited For
Guaranteed Every container in the Pod has CPU and memory requests == limits (and requests are non-zero). Lowest (Evicted last; OOM score -997) Critical databases, cache tiers, latency-sensitive APIs
Burstable At least one container has a CPU or memory request set, but requests and limits are not identical across all containers. Medium (Evicted when no BestEffort Pods remain; OOM score 2-999) General web services, microservices, background workers
BestEffort No container in the Pod defines any CPU or memory requests or limits. Highest (First to be evicted when node faces pressure; OOM score 1000) Non-critical dev jobs, experimental scratch tasks

Deployment Manifest with Resource Constraints

The following manifest defines an API deployment with Burstable QoS alongside a namespace-level LimitRange:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: payment-api
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: payment-api
  template:
    metadata:
      labels:
        app: payment-api
    spec:
      containers:
        - name: payment-api
          image: payment-api:2.4.0
          resources:
            requests:
              cpu: 250m       # 0.25 CPU cores (250 millicores)
              memory: 512Mi   # 512 Mebibytes
            limits:
              cpu: 1000m      # 1 full CPU core
              memory: 1Gi     # 1 Gibibyte (1024 Mi)
---
apiVersion: v1
kind: LimitRange
metadata:
  name: default-limits
  namespace: production
spec:
  limits:
    - type: Container
      default:
        cpu: 500m
        memory: 512Mi
      defaultRequest:
        cpu: 100m
        memory: 128Mi

Monitoring and Diagnostics with kubectl

# View real-time resource consumption by Pod
kubectl top pods -n production

# View CPU and memory usage per node
kubectl top nodes

# Inspect assigned QoS class for a running Pod
kubectl get pod payment-api-7b89f-2xk9l -n production -o jsonpath='{.status.qosClass}'
# Output: Burstable

# Inspect node capacity, allocatable pool, and allocated requests
kubectl describe node worker-node-01

Common Mistakes

  • Overtight CPU limits causing CFS throttling: Setting CPU limits close to average usage causes the Linux Completely Fair Scheduler (CFS) to throttle threads aggressively, inducing high latency spikes even when node CPU is idle.
  • Missing memory limits leading to node OOM: Omitting memory limits allows memory leaks to consume entire host RAM until the Linux kernel OOM-killer terminates arbitrary node processes.
  • Confusing megabytes (MB) and mebibytes (Mi): 100M means $100 \times 10^6$ bytes (decimal), while 100Mi means $100 \times 2^{20}$ bytes (binary). Mixing them up causes subtle allocation discrepancies.
  • Requests set equal to peak capacity: Setting requests to peak capacity underutilizes physical infrastructure and prevents bin-packing, drastically inflating cloud infrastructure bills.

Best Practices

  • Always define explicit requests and limits for all production containers to guarantee predictable scheduling.
  • Configure LimitRange objects on all tenant namespaces to enforce default requests/limits on Pods created without them.
  • Set identical memory requests and limits for stateful or latency-critical workloads to guarantee Guaranteed QoS.
  • Monitor container_cpu_cfs_throttled_periods_total in Prometheus to detect silent CPU throttling.

Interview Questions

1. What is the fundamental difference between resource requests and resource limits?

requests represent the guaranteed baseline resources required for a container to run; the kube-scheduler sums all container requests in a Pod to find a node with sufficient allocatable capacity. limits define the hard ceiling enforced at runtime by Linux cgroups. If a container exceeds its memory limit it is terminated (OOMKilled), whereas if it exceeds its CPU limit it is throttled.

2. How are the three Kubernetes QoS classes assigned?

Kubernetes assigns Guaranteed if every container in the Pod has CPU and memory requests equal to limits. It assigns Burstable if at least one container has a CPU or memory request and the criteria for Guaranteed are not met. It assigns BestEffort if no container defines any requests or limits.

3. What happens when a container exceeds its CPU limit vs its Memory limit?

Because CPU is a compressible resource, exceeding a CPU limit results in the Linux CFS cgroup throttling container CPU execution slices without killing the process. Because memory is incompressible, exceeding a memory limit causes the Linux kernel OOM killer or container runtime to terminate the container process immediately with exit code 137 (OOMKilled).

4. How does the scheduler handle overcommitting node resources?

The scheduler only permits node overcommitment with respect to limits, never requests. It ensures that $\sum \text{requests} \le \text{Node Allocatable}$. However, the sum of limits across all Pods on a node can exceed total physical node capacity, relying on the assumption that not all containers peak simultaneously.

5. What is the difference between 500m CPU and 0.5 CPU in a Pod manifest?

There is no functional difference. In Kubernetes, CPU is measured in millicores (or thousandths of a core). 500m equals 0.5 cores, and 1000m equals 1 vCPU / CPU core. The millicores notation is standard in YAML manifests to avoid floating-point syntax ambiguity.

6. In what order does the kubelet evict Pods under node memory pressure?

When a node falls below eviction thresholds (such as memory.available < 100Mi), the kubelet evicts Pods in reverse order of Quality of Service: first BestEffort Pods, followed by Burstable Pods exceeding their requests, and finally Guaranteed Pods only as a last resort.

Summary

  • requests guide scheduler placement; limits enforce runtime cgroup boundaries.
  • CPU is compressible (throttling); memory is incompressible (OOMKilled exit code 137).
  • QoS tiers (Guaranteed, Burstable, BestEffort) dictate eviction priority under node pressure.
  • Combine requests and limits with namespace LimitRange for cluster stability.

Continue Learning

Previous: Job and CronJob | Next: Health Checks

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.