Autoscaling & Resource Management
Cluster Autoscaler — Adding and Removing Nodes
Scaling underlying cloud compute VM counts based on pending pods.
Introduction
Pod-level scaling (HPA and VPA) is bounded by the total resource capacity of your physical or virtual nodes. If your cluster runs out of compute nodes, new pods cannot be scheduled. The Cluster Autoscaler resolves this by automatically adding or removing nodes in the cluster's node groups based on workload demand.
It communicates with cloud providers (such as AWS, GCP, or Azure) to scale VM Auto Scaling Groups up when there are resource shortages, and scale them down when nodes are under-utilized.
Why It Matters
Without a Cluster Autoscaler, pod scaling is hard-capped. A sudden scale-out request will leave pods stuck in a Pending state, causing application performance degradation or outages. Alternatively, during low traffic periods, you pay for idle virtual machines, inflating infrastructure costs.
Real-World Analogy
Think of a hotel during a holiday rush. If all rooms are fully booked and there is a queue of guests waiting in the lobby (Pending pods), the hotel manager opens up a secondary wing of rooms (adding nodes). Once the holiday ends and rooms sit empty, the manager closes down the wing to save on heating, electricity, and housekeeping costs (removing nodes).
How It Works
The Cluster Autoscaler runs a continuous monitoring loop, evaluating two primary conditions:
- Scale-Up Condition: If any pods are in a
Pendingstate because the scheduler could not find a node with enough CPU, memory, ports, or affinity rules (marked with aFailedSchedulingevent), Cluster Autoscaler triggers the cloud provider's API to launch new nodes in the appropriate node group. - Scale-Down Condition: If a node's resource utilization (calculated as the sum of its running pods' Resource Requests divided by node capacity) is below a target threshold (default 50%) for a sustained period (typically 10 minutes), CA simulates whether the pods on that node can be rescheduled onto other active nodes. If yes, it drains the node and deletes the VM.
Internal Architecture
Unlike HPA/VPA, Cluster Autoscaler does not monitor CPU/Memory metrics. Instead, it simulates the Kube-Scheduler's routing algorithm. When pods are pending, CA determines which Node Group (or Auto Scaling Group) can satisfy the pods' resource, nodeSelector, toleration, and affinity requirements. It then makes an API call to the host cloud provider (e.g. AWS ASG, Azure VMSS, or GCP MIG) to increment the group size.
Visual Explanation
Practical Example
Here is a configuration snippet showing typical startup arguments for the Cluster Autoscaler deployment running in a cloud environment:
Common Mistakes
- Cloud IAM Permission Failures: Failing to attach correct permissions (e.g. AWS IAM permissions for AutoScaling and EC2) to the service account running the CA. This blocks CA from editing the VM scaling groups.
- Missing Pod Disruption Budgets (PDBs): If PDBs are not defined, scale-down evictions can terminate critical replicas, violating SLA requirements during scaling events.
- Blocks on Scale-Down: Pods with local ephemeral storage (like
emptyDir) or pods without controllers (naked pods) will prevent Cluster Autoscaler from terminating a node to avoid data loss, unless explicit command-line flag overrides are defined.
Quick Quiz
Q1: Which event directly triggers the Cluster Autoscaler to invoke cloud APIs to add new node instances?
A) CPU utilization on a node exceeds 90%.
B) A pod enters a Pending state due to a FailedScheduling event.
C) The HPA scales a deployment to its maximum replicas.
D) Kubelet reports a DiskPressure condition.
Answer: B — Cluster Autoscaler is reactive to pods that are unschedulable (Pending due to FailedScheduling), not raw resource usage percentages.
Q2: Why does a pod using an emptyDir volume prevent Cluster Autoscaler from scaling down a node by default?
A) Because local volumes are automatically replicated across other nodes.
B) To protect against data loss since emptyDir data is stored locally on the node and deleted when the pod is evicted.
C) Because emptyDir volumes require physical disks to be detached manually by an administrator.
D) Because it forces the pod to run in Guaranteed QoS class.
Answer: B — Terminating a node terminates its local disk volumes. CA prevents scaling down nodes containing local storage pods to protect applications from data loss unless overridden via flags.
Scenario-Based Challenge
Production Scenario:
You deploy a critical web app with 3 replicas. During a scale-down cycle, Cluster Autoscaler terminates a worker node. The web app drops below its minimum available count, causing brief downtime. How do you prevent Cluster Autoscaler from evicting too many replicas of your application during node scale-downs?
View Solution
To protect your application's availability during node drains and scaling events, configure a PodDisruptionBudget (PDB) for the application:
Create a PDB manifest that guarantees a minimum number of available replicas:
When CA attempts to drain a node, the eviction API will respect this PDB. If evicting a pod would bring the active replicas below 2, the eviction is blocked, forcing the autoscaler to wait or reschedule other pods first.
Debugging Exercise
Broken Configuration:
Your pods are stuck in Pending state, but the cloud provider is not adding nodes. Checking the autoscaler logs reveals:
Reason: The Cluster Autoscaler pod does not have permission to modify the cloud provider's Auto Scaling Group. In this case, the IAM role mapped to the Kubernetes Service Account lacks the autoscaling:SetDesiredCapacity permission.
The Fix:
1. Locate the IAM Policy attached to the cluster-autoscaler role.
2. Add the required permissions (e.g. AWS Auto Scaling permissions):
3. Verify the service account matches the role annotations and restart the autoscaler pod.
Interview Questions
1. Conceptual: How does Cluster Autoscaler determine which node group (ASG) to scale up if multiple groups are available?
Cluster Autoscaler uses expanders to select the node group. Common expanders are:
- random: Default, selects a group at random.
- most-pods: Selects the group that can schedule the highest number of pending pods.
- least-waste: Selects the group that will have the least idle CPU/Memory resources after scheduling.
- priority: Selects based on user-assigned node group priorities (useful for prioritizing spot instances over on-demand nodes).
2. Technical: What are Pod Disruption Budgets (PDBs) and how do they interact with Cluster Autoscaler scale-down actions?
A Pod Disruption Budget defines the minimum number or percentage of healthy replicas that must be running for a set of pods. During node scale-down, Cluster Autoscaler must drain a node by evicting its pods. The eviction controller checks the target pods' PDB. If evicting a pod violates the PDB limit (meaning it would drop healthy replicas below the budget), the eviction fails, and Cluster Autoscaler cancels the scale-down event for that node to protect application availability.
Production Considerations
Use **PriorityClass ballooning** to pre-warm nodes. This involves running low-priority pause pods that consume resources but are evicted instantly when normal-priority application pods need to start, enabling near-instantaneous scaling. Always set limits on node group size (min and max) to protect against unexpected cloud resource costs.