Sub topic 2
Resource requests and why pods go Pending
This chapter assumes you know Pods, Deployments and Nodes, and that you can read a YAML manifest and run kubectl. It explains the signal that every node autoscaler reacts to: a pod the scheduler cannot place.
Requests drive scheduling
The scheduler places a pod on a node by comparing the pod's resource requests with what the node has left. It does not look at live CPU or memory use. A node's allocatable capacity is what remains after the operating system, the kubelet and eviction thresholds take their share. The scheduler subtracts the requests of pods already on the node from that figure.
A node with 4 CPUs allocatable and 3.5 CPUs requested can be nearly idle in practice. It still has no room for a pod that requests 1 CPU. Limits are not part of this calculation.
What Pending means
A pod is Pending when it has been accepted by the API server but has no node. The scheduler marks it unschedulable and records a FailedScheduling event. Common reasons are:
- No node has enough unrequested CPU or memory.
- No node matches the pod's node selector, affinity rules or tolerations.
- A topology spread constraint cannot be met.
- The pod requests more than any single node could ever hold.
Node autoscalers watch for pods that are Pending because of capacity. They ignore pods that are Pending for other reasons.
Worked example
Create a Deployment that starts at zero replicas and asks for 1 CPU and 1Gi per pod.
apiVersion: apps/v1
kind: Deployment
metadata:
name: inflate
spec:
replicas: 0
selector:
matchLabels:
app: inflate
template:
metadata:
labels:
app: inflate
spec:
containers:
- name: inflate
image: registry.k8s.io/pause:3.9
resources:
requests:
cpu: "1"
memory: 1GiScale it up and inspect the result:
kubectl scale deployment inflate --replicas=10
kubectl get pods -l app=inflate
kubectl describe pod -l app=inflate | grep -A5 Events
kubectl get nodes -wIf the cluster lacks room for ten such pods, some stay Pending. The events show a message like 0/3 nodes are available: 3 Insufficient cpu. With a node autoscaler installed, new nodes appear in the kubectl get nodes -w output and the pods then start.
Common errors
- Requests set too high. Pods reserve capacity they never use, so the autoscaler adds nodes that sit mostly idle.
- Requests missing. Pods with no requests look free to the scheduler. Nodes fill up, and the autoscaler sees no demand until performance suffers.
- A request larger than any node. The pod stays Pending forever. A new node of the same size will not help.
Exercise
A node has 8 CPUs allocatable. Pods on it request 7.5 CPUs in total but use only 1 CPU. A new pod requests 1 CPU. Will the scheduler place it on this node, and why?
Show answer
No. The scheduler compares requests, not usage. Only 0.5 CPU is unrequested, which is less than the 1 CPU the new pod asks for. The pod goes Pending unless another node has room, and a node autoscaler may add one.