Kubernetes on scheduling and scaling
Requests reserve, limits cap
- A request is what a container is promised. A limit is the most it may use.
- CPU is counted in cores, and 500m is half a core. Memory is in bytes, such as 256Mi.
- Over its CPU limit, a container is slowed down. Over its memory limit, it can be killed.
resources:
requests: {cpu: 250m, memory: 256Mi}
limits: {cpu: 500m, memory: 512Mi}The scheduler picks a node for every Pod
- It filters out nodes without room for the Pod's requests, then scores the rest.
- A Pod that fits nowhere stays Pending. “kubectl describe pod” shows why.
- A nodeSelector steers a Pod to nodes with a given label, such as disktype=ssd.
- A taint on a node keeps Pods away, unless they carry a matching toleration.
Requests set who is evicted first
- When a node runs short of memory or disk, the kubelet evicts Pods to recover.
- Pods with no requests or limits go first. Their class is called BestEffort.
- Pods where every request equals its limit go last. Their class is called Guaranteed.
Spread copies so one failure is survivable
- Topology spread constraints spread a workload's Pods evenly across zones or nodes.
- A PodDisruptionBudget (PDB) limits how many Pods planned work may take down at once.
- With minAvailable: 2 on 3 Pods, a node drain can evict only one of them at a time.
The HPA adds Pods when load rises
- A HorizontalPodAutoscaler (HPA) changes the replica count to hold a target metric.
- Target 60% CPU, and 4 Pods run at 90%: 4 x 90 / 60 = 6 Pods.
- CPU percent is measured against requests, and the numbers come from Metrics Server.
- It checks every 15 seconds, and waits 5 minutes before scaling down.
- Load risesCPU above target
- HPA adds Pods4 become 6
- No roomnew Pods stay Pending
- Node autoscaleradds a node
- Pods scheduledload is shared
Node autoscalers add machines when Pods do not fit
- Cluster Autoscaler and Karpenter add nodes when Pods cannot be scheduled.
- They also remove underused nodes, and the Pods there are recreated elsewhere.
- They count requests, not real usage. Wrong requests give you the wrong cluster size.
A running Pod can be resized in place
- Since v1.35, you can change a running Pod's CPU and memory without recreating it.
- You patch the Pod's resize subresource with the new requests and limits.
- The VerticalPodAutoscaler, an add-on, recommends requests based on real usage.