What is Kubernetes Horizontal Pod Autoscaler (HPA)?
Why Interviewers Ask This
This is a classic screening question for Kubernetes (K8s) roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
The Horizontal Pod Autoscaler (HPA) automatically scales the number of pod replicas in a Deployment, ReplicaSet, or StatefulSet based on observed metrics (CPU, memory, custom metrics). HPA spec: apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Resource resource: name: memory target: type: AverageValue averageValue: 200Mi. How it works: every 15 seconds, HPA queries the Metrics Server (or custom metrics API) for current resource usage → computes desired replica count = ceil(currentReplicas × currentMetric / targetMetric) → scales up or down within min/max bounds. Scale-up cooldown: 3 minutes (default) before scaling up again — prevents flapping; Scale-down cooldown: 5 minutes (default) before scaling down. Custom and external metrics: scale on any metric via Prometheus Adapter (HTTP requests/second, queue depth, latency). Example: type: External external: metric: name: sqs_queue_depth selector: matchLabels: queue: orders target: type: AverageValue averageValue: "5". Metrics Server: must be installed in the cluster (not default): kubectl apply -f metrics-server.yaml. KEDA (Kubernetes Event-Driven Autoscaling): extends HPA with 60+ event sources (Kafka, SQS, Redis, Azure Service Bus, Prometheus). Scale to zero when no events. VPA: scale pod resource requests vertically (more CPU/memory) — can't run simultaneously with HPA on same metric.
Common Mistake
Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Kubernetes (K8s) experience.