QA
Intermediate LevelDocker & Deployment

How does the Horizontal Pod Autoscaler (HPA) scale pods based on CPU/RAM metrics?

❓ Interview Prompt: Explain HPA control loop, Metrics Server, target utilization, scale-up/scale-down stabilization, and CPU-based autoscaling configuration.

The Horizontal Pod Autoscaler (HPA) automatically adjusts the number of Pod replicas in a Deployment (or StatefulSet/ReplicaSet) based on observed metrics.

Control Loop (every 15 seconds):

1
HPA queries the Metrics Server (or Custom Metrics API) for the current metric values.
2
Calculates the desired replica count:

desiredReplicas = ceil(currentReplicas × (currentMetric / targetMetric))

3
Updates the Deployment's spec.replicas if the desired count differs.

Scale-Up vs Scale-Down:

•
Scale-Up: Happens quickly (within 1–2 control loop cycles) to handle traffic spikes.
•
Scale-Down: Delayed by a stabilization window (default: 5 minutes) to prevent flapping — rapid scale-up then immediate scale-down wastes resources.

Prerequisite: Metrics Server

HPA requires the Kubernetes Metrics Server to be installed — it aggregates CPU/RAM metrics from Kubelet on each node.

Resource Requests are Mandatory:

HPA calculates CPU utilization as: currentCPU / request.cpu. Without resource requests defined, HPA cannot calculate utilization percentages.

Code Implementation Example

yaml
# HPA: scale Deployment based on CPU utilization
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api
  minReplicas: 2     # never scale below 2 (availability floor)
  maxReplicas: 20    # never exceed 20 replicas (cost ceiling)
  
  metrics:
    # Scale when average CPU > 70% of requested CPU
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    
    # Also scale when average memory > 80%
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
  
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300   # 5min cooldown prevents flapping
💡HPA v2 scaling on both CPU and memory — min 2 replicas, max 20, with 5-minute scale-down stabilization.
🎯 Interview Key Takeaway

HPA requires Metrics Server + resource requests. Scale on CPU utilization target (not usage). Use scale-down stabilization window to prevent replica count flapping.

#Kubernetes#HPA#Autoscaling#Metrics Server#CPU Scaling#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?