
Requests vs Limits: How Kubernetes Schedules, Throttles and OOM-Kills Your Pods
- كاتب موثّق
محتويات المقال
limits: { cpu: "8", memory: 64Mi } to every service "to be safe", reasoning that a limit only caps what a container may use. Then a routine scale-up leaves one replica stuck in Pending, although every dashboard shows the node almost idle. Meanwhile, a worker restarts every few hours with exit code 137, and nobody connects it to that 64Mi.
The common misconception
resources block, use the same units, and templates often give them the same values. Kubernetes even fills in a missing request from the limit. And for memory, when both are equal, they really do behave like one number.
What actually happens
Requests are for the scheduler
Limits are for the kernel
Requests also set a weight and an eviction line
A limit without a request becomes the request
Watching it on a cluster
--context and -n are left out for space. The Deployment runs 3 replicas of busybox:1.37 serving HTTP. Here are the resources blocks of the two versions we applied:
resources: limits: # meant as "at most", also becomes the request cpu: "8" memory: 64Miresources: requests: # what the scheduler reserves cpu: 250m memory: 64Mi limits: # what the kernel enforces memory: 64Mikubectl apply -f api.yaml # 3 replicas, limits onlykubectl get pods# NAME READY STATUS RESTARTS AGE# api-5b99d6d69d-8gpp2 1/1 Running 0 8s# api-5b99d6d69d-h5hs7 0/1 Pending 0 8s# api-5b99d6d69d-k92w7 1/1 Running 0 8skubectl get pod api-5b99d6d69d-8gpp2 \ -o jsonpath='{.spec.containers[0].resources.requests}'# {"cpu":"8","memory":"64Mi"}kubectl get pod api-5b99d6d69d-8gpp2 -o jsonpath='{.status.qosClass}'# Guaranteedkubectl apply -f api.yaml # requests, plus a memory limit# deployment.apps/api createdkubectl get pods# NAME READY STATUS RESTARTS AGE# api-5fd9498bd-5h4jg 1/1 Running 0 10s# api-5fd9498bd-psf42 1/1 Running 0 10s# api-5fd9498bd-zq7sl 1/1 Running 0 10skubectl get pod api-5fd9498bd-5h4jg -o jsonpath='{.status.qosClass}'# Burstablekubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/cpu.weight# 10kubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/cpu.max# max 100000kubectl exec api-5fd9498bd-5h4jg -- cat /sys/fs/cgroup/memory.max# 67108864cpu.weight, there's no CPU ceiling (max), and the memory limit became memory.max, 64 MiB in bytes.
tail /dev/zero, which buffers forever, under a 64Mi memory limit, and a shell busy loop under a 200m CPU limit:
kubectl apply -f leak.yaml # tail /dev/zero, memory limit 64Mikubectl get pod leak# NAME READY STATUS RESTARTS AGE# leak 0/1 OOMKilled 1 (8s ago) 8skubectl get pod leak -o jsonpath='{..lastState.terminated.reason}'# OOMKilledkubectl apply -f spin.yaml # busy loop, cpu limit 200mkubectl exec spin -- cat /sys/fs/cgroup/cpu.max# 20000 100000kubectl exec spin -- \ grep -E "usage_usec|nr_periods|nr_throttled" /sys/fs/cgroup/cpu.stat# usage_usec 1359686# nr_periods 67# nr_throttled 67# (the same command, 10 seconds later)# usage_usec 3430017# nr_periods 171# nr_throttled 171cpu.weight | cpu.max, memory.max |
How to apply it in your project
Set requests from real usage Requests decide how many replicas fit on your nodes, so base them on usage at normal load. Too high wastes nodes and leaves Pods Pending; too low means you lose out under contention and get evicted first.Set the memory limit equal to the memory request In our view, that's the safest default: usage then can't exceed the request, so the Pod isn't among the first to be evicted, and its OOM threshold is a number you chose. Size both from the measured peak. Decide on CPU limits on purpose A CPU limit trades burst speed for predictability: the busy loop above got 0.2 of a core on a 20-CPU node. Without one, the container can use idle CPU, and requests still decide each Pod's share under contention. Check what you actually got Run kubectl get pod <name> -o jsonpath='{.status.qosClass}'and look at "Allocated resources" inkubectl describe node. A LimitRange in your namespace may have added limits you didn't write.
Key takeaways
Requests are reservations for scheduling; limits are ceilings the kernel enforces. The scheduler adds up requests, not usage, so oversized requests leave Pods Pendingon idle nodes.Above a CPU limit a container is throttled; above a memory limit it's OOM-killed with exit code 137. A limit without a request becomes the request, which can reserve far more than you meant. Size memory request and limit from the measured peak, and choose CPU limits deliberately.
التعليقات (0)