Resource Requests and Limits
You'll learn: How to define CPU and memory requirements in Kubernetes to ensure proper pod scheduling and prevent resource contention.
What Are Resource Requests and Limits?
When you deploy a pod in Kubernetes, you can specify two resource values for each container: requests and limits. Requests tell the scheduler how much CPU and memory your container needs to run, while limits define the maximum it's allowed to consume.
Think of it like booking a hotel room. Your request is the minimum room size you need (the scheduler won't place you somewhere smaller), and your limit is the maximum space you can occupy without disturbing other guests.
Why This Matters
Without resource requests, Kubernetes doesn't know what your containers need. It might schedule too many pods on one node, causing all of them to fight for resources—resource contention. Without limits, one misbehaving container could consume all available CPU or memory, starving its neighbors.
Requests enable intelligent scheduling. Kubernetes sums up all requests on a node to decide if there's room for new pods. Limits enforce boundaries, protecting the cluster from runaway processes.
CPU vs Memory Behavior
CPU is a compressible resource—if you exceed your limit, Kubernetes throttles you. Memory is incompressible—if you exceed your memory limit, your pod gets killed (OOMKilled).
Typical values: CPU in millicores (e.g., 100m = 0.1 CPU core), memory in bytes (e.g., 256Mi).
Practical Guidance
Set requests based on observed baseline usage. Set limits higher to handle bursts, but not so high that failures cascade. Monitoring (covered earlier via health checks) helps you tune these values over time.
Key Takeaway: Resource requests ensure proper scheduling by reserving capacity; limits prevent resource contention by capping consumption—both are essential for stable, efficient Kubernetes clusters.