Requests, limits and autoscaling (HPA)
MediumKubernetesAutoscalingResources
What are resource requests and limits in Kubernetes, and how does autoscaling (HPA) work?
TL;DR
A request is what the scheduler reserves for a container: a Pod only lands on a node with enough unreserved capacity. A limit is the hard ceiling at runtime. Going over the CPU limit makes the container throttled (slowed down); going over the memory limit gets it OOMKilled. The Horizontal Pod Autoscaler (HPA) adds or removes replicas to keep a metric near a target, such as average CPU at 70% of requests. The Cluster Autoscaler (or Karpenter) adds nodes when Pods can't be scheduled.
Key points
- Always set requests. Without them the scheduler can't place Pods well and HPA CPU percentages mean nothing.
- Memory: set the limit equal to or close to the request, since memory can't be throttled, only killed.
- CPU limits are debated: they cause throttling even when the node has spare CPU. Many teams set CPU requests and skip CPU limits for latency-sensitive services.
- QoS classes:
Guaranteed(requests = limits),Burstable,BestEffort(nothing set; evicted first under pressure). - HPA can scale on custom metrics (requests per second, queue length). KEDA scales on event sources, including down to zero.
- VPA (Vertical Pod Autoscaler) recommends or adjusts requests; don't combine it with HPA on the same CPU metric.
- Set a PodDisruptionBudget so node upgrades don't take down too many replicas at once.
Example
yaml
resources:
requests: { cpu: 250m, memory: 256Mi }
limits: { memory: 256Mi }
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: api }
spec:
scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api }
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } }Common follow-ups
- Why is my Pod
Pending? Usually no node has enough unreserved CPU or memory for its requests, or a node selector or taint excludes every node.kubectl describe podsays which. - Why is my app slow with low average CPU? CPU throttling against a limit. Check
container_cpu_cfs_throttled_seconds_total.
What interviewers look for
Tip
Requests = scheduling, limits = runtime ceiling, CPU throttles vs memory OOM-kills, and HPA targets measured against requests.
- Red flag: no requests set, then wondering why autoscaling misbehaves.