MODULE 4/LESSON 5
☸️ Docker & Kubernetes

☸️ K8s Pod Scaler Visualizer

Simulate Kubernetes Horizontal Pod Autoscaling

Interactive⚡ Interactive Tool
Watch pods automatically scale in and out based on CPU load thresholds in this interactive visualizer. Understand how the HPA controller maintains stability under varying traffic conditions.

Key Concepts

Target Utilization

The HPA compares current average CPU utilization across all pods against a target (e.g. 70%). If it exceeds the target, it scales out.

Scale Out (Spike)

When traffic spikes, new pods transition from 'Pending' to 'Running'. Once ready, the load is distributed, bringing average CPU back down.

Scale In (Cooldown)

When traffic drops, the HPA waits for a cooldown period (stabilization window) before terminating pods to prevent rapid thrashing.

Load Balancing

The K8s Service (ClusterIP) automatically load balances incoming requests across all healthy, ready pods in the ReplicaSet.

⚡ Interactive Architecture Simulator

Kubernetes Pod Autoscaler (HPA Visualizer)

Avg CPU: 0% (Target: 70%)
Total Pods: 0 / 12
Low Traffic (Idle)Massive Traffic Spike

Kubernetes Pod ReplicaSet

HPA: Idle (Watching CPU)
No pods running (Min 2 required)
Horizontal Pod Autoscaler (HPA) Live Metrics Timeline
Avg Pod CPU %: 0%
Total Pod Count: 0%
Target CPU (70%): 70%
0%28%55%84%

The Math Behind Kubernetes HPA

hpa-math.jsjavascript
1// Pseudocode for Kubernetes HPA Scaling Calculation
2function calculateDesiredReplicas(currentReplicas, currentMetricValue, targetMetricValue) {
3  // Formula: desiredReplicas = ceil[currentReplicas * ( currentMetricValue / targetMetricValue )]
4  const ratio = currentMetricValue / targetMetricValue;
5  
6  // Tolerance (default 0.1) prevents thrashing for small fluctuations
7  if (Math.abs(1.0 - ratio) <= 0.1) {
8    return currentReplicas; // No scaling needed
9  }
10  
11  return Math.ceil(currentReplicas * ratio);
12}
13
14// Example: 5 Pods, Target CPU 70%, Current CPU 95%
15// ratio = 95 / 70 = 1.357
16// desired = Math.ceil(5 * 1.357) = Math.ceil(6.785) = 7 Pods
💡
Senior Architect Insight: In a real K8s cluster, scaling up takes time (downloading images, starting processes). If your traffic spikes instantly, the existing pods might crash before new ones are ready. This is why 'Target Utilization' is often set to 60-70% rather than 90% — leaving buffer room to handle the spike while new pods boot.