Q: What is Bin Packing in Kubernetes? How does kube-scheduler implement bin packing using MostAllocated vs LeastAllocated scoring strategies, and how do tools like Descheduler and Karpenter consolidate nodes to cut cloud costs?
Architectural guide to Bin Packing in Kubernetes: understanding kube-scheduler node scoring plugins (MostAllocated vs LeastAllocated), descheduler defragmentation, and Karpenter consolidation to reduce cloud spend by 40%.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
The Mathematical Concept & FinOps Goal
Understanding the bin packing problem in cloud computing:
- Problem: You have varying-sized pods (CPU/Memory requests) and fixed-capacity worker nodes (bins).
- Default Behavior (LeastAllocated): Kube-scheduler defaults to spreading pods across nodes to minimize resource contention (NodeAllocationScoring:
LeastAllocated). While resilient, this creates cluster fragmentation where 10 nodes each run at 25% utilization. - Bin Packing Goal (MostAllocated): Schedule pods onto nodes that are already heavily utilized. This fills nodes to 85-90% capacity, leaving empty nodes that the cluster autoscaler can terminate to reduce EC2 bills by 30-50%.
Kube-Scheduler Configuration: MostAllocated Plugin
Customizing the Kubernetes scheduler profile for bin packing:
- The
MostAllocatedstrategy scores nodes with higher allocated percentages higher, encouraging denser pod packing.
Day-2 Defragmentation: Descheduler & Karpenter
Why the scheduler alone cannot solve bin packing long term:
- The Scheduler Limitation: Kube-scheduler only evaluates pods at the moment of creation. It never moves already-running pods as workloads change over time.
- The Kubernetes Descheduler: A cron-based controller that detects fragmented clusters, respects PodDisruptionBudgets (PDBs), and evicts pods from sparsely populated nodes so they re-schedule densely onto remaining nodes.
- Karpenter Consolidation: Modern EKS clusters leverage Karpenter's native
consolidation: enabledpolicy. Karpenter constantly calculates if running pods can fit onto smaller or fewer nodes, gracefully draining instances and terminating empty nodes within seconds.
Bin Packing Risks & Critical Guardrails
Preventing blast-radius failures when packing densely:
- PodDisruptionBudgets (PDB): Mandatory on all multi-replica services (
minAvailable: 1) to prevent consolidation from evicting all instances simultaneously. - TopologySpreadConstraints: Enforce zone spreading across AWS Availability Zones so bin packing doesn't pack all replicas into a single failure domain.
- Node Eviction Headroom: Leave 10-15% buffer for DaemonSets (fluentbit, datadog, kube-proxy) and OS kernel memory overhead.
- Bin packing in Kubernetes is the practice of scheduling pods onto the fewest possible worker nodes to maximize resource utilization and allow empty nodes to be scaled down.
- While kube-scheduler defaults to spreading workloads evenly across nodes, we configure NodeResourcesFit with MostAllocated scoring to concentrate workloads.
- In production, we combine PodDisruptionBudgets, topology spread constraints, and Karpenter automated consolidation to safely pack nodes to 85% capacity without sacrificing high availability.