POKE ME for any consultancy

Sunday, April 19, 2026

How Does Kubernetes Handle CPU and Memory Limits?

 Kubernetes manages CPU and memory resources for containers using two primary configurations: requests and limitswhich influence both scheduling and runtime enforcement.

1. CPU Management

CPU Requests

  • Represent the minimum guaranteed CPU container can access.
  • The kube-scheduler uses requests to place Pods on nodes with sufficient available CPU.
  • CPU is compressiblemeaning container can temporarily exceed its request if spare cycles exist.

CPU Limits

  • Define the maximum CPU container may consume.
  • Enforced via Linux cgroups using the Completely Fair Scheduler (CFS):
    • cpu.cfs_quota_us specifies microseconds per scheduling period (cpu.cfs_period_uscontainer may consume.
    • Exceeding the limit triggers CPU throttlingslowing the container without killing it.
  • Behavioral nuances:
    • Throttling occurs only when multiple containers compete for limited CPU.
    • Unused CPU is reallocated proportionally to containers based on their requests and priority (QoS class).

2. Memory Management

Memory Requests

  • Indicate the minimum memory guaranteed to container.
  • Scheduler ensures the host node has enough free memory for requested allocations.

Memory Limits

  • Specify the maximum memory container can use.
  • Memory is incompressibleexceeding the limit may trigger:
    • OOM (Out-of-Memory) kills by the Linux kernel.
    • If container is terminated and restartPolicy allows, Kubernetes restarts it.
  • Unlike CPU, exceeding memory limits usually results in immediate termination under memory pressure.

Notes on Enforcement

  • Memory enforcement is reactiveoccurring only if the kernel detects memory pressure.
  • Kubernetes tracks Pod QoS class (Guaranteed, Burstable, BestEffort) to prioritize OOM eviction:
    • Guaranteedrequest limit; least likely to be killed.
    • Burstablerequest limit; may be killed under pressure.
    • BestEffortno requests/limits; first to be evicted when memory is scarce.

3. Pod-Level Resource Aggregation

  • Pod’s total CPU or memory requests/limits are the sum of its containers’ configurations.
  • Kubernetes now supports Pod-level resource requests/limitsallowing containers within Pod to share idle resources dynamically.

4. Best Practices

  • Always set requests and limits to ensure scheduling stability and prevent runaway consumption.
  • For CPUprioritize requests over hard limits to avoid performance throttling in bursty workloads.
  • For memoryset realistic limits above expected peak usage to reduce OOM kills.
  • Use QoS classes, HPA (Horizontal Pod Autoscaler), and VPA (Vertical Pod Autoscaler) to dynamically adjust resources.
  • Monitor CPU and memory with tools like kubectl top podPrometheus metrics (container_cpu_cfs_throttled_seconds_totalcontainer_memory_usage_bytes), and Node PSI (Pressure Stall Information).
  • CPU is compressiblemeaning container can temporarily exceed its request if spare cycles exist.

CPU Limits

  • Define the maximum CPU container may consume.
  • Enforced via Linux cgroups using the Completely Fair Scheduler (CFS):
    • cpu.cfs_quota_us specifies microseconds per scheduling period (cpu.cfs_period_uscontainer may consume.
    • Exceeding the limit triggers CPU throttlingslowing the container without killing it.
  • Behavioral nuances:
    • Throttling occurs only when multiple containers compete for limited CPU.
    • Unused CPU is reallocated proportionally to containers based on their requests and priority (QoS class).

2. Memory Management

Memory Requests

  • Indicate the minimum memory guaranteed to container.
  • Scheduler ensures the host node has enough free memory for requested allocations.

Memory Limits

  • Specify the maximum memory container can use.
  • Memory is incompressibleexceeding the limit may trigger:
    • OOM (Out-of-Memory) kills by the Linux kernel.
    • If container is terminated and restartPolicy allows, Kubernetes restarts it.
  • Unlike CPU, exceeding memory limits usually results in immediate termination under memory pressure.

Notes on Enforcement

  • Memory enforcement is reactiveoccurring only if the kernel detects memory pressure.
  • Kubernetes tracks Pod QoS class (Guaranteed, Burstable, BestEffort) to prioritize OOM eviction:
    • Guaranteedrequest limit; least likely to be killed.
    • Burstablerequest limit; may be killed under pressure.
    • BestEffortno requests/limits; first to be evicted when memory is scarce.

3. Pod-Level Resource Aggregation

  • Pod’s total CPU or memory requests/limits are the sum of its containers’ configurations.
  • Kubernetes now supports Pod-level resource requests/limitsallowing containers within Pod to share idle resources dynamically.

4. Best Practices

  • Always set requests and limits to ensure scheduling stability and prevent runaway consumption.
  • For CPUprioritize requests over hard limits to avoid performance throttling in bursty workloads.
  • For memoryset realistic limits above expected peak usage to reduce OOM kills.
  • Use QoS classes, HPA (Horizontal Pod Autoscaler), and VPA (Vertical Pod Autoscaler) to dynamically adjust resources.
  • Monitor CPU and memory with tools like kubectl top podPrometheus metrics (container_cpu_cfs_throttled_seconds_totalcontainer_memory_usage_bytes), and Node PSI (Pressure Stall Information).

Key Takeaways:
  • CPU limits are throttledmemory limits are terminated.
  • Requests ensure Pod placement; limits enforce runtime constraints.
  • Pods’ performance and cluster stability depend on setting proper requests and limits, aligned with workload characteristics.