The Horizontal Pod Autoscaler (HPA) automatically changes the number of Pod replicas in a Deployment (or StatefulSet) based on observed metrics, so the application scales out under load and scales in when load drops. How it works 1. Metrics collection: the metrics-server collects CPU and memory usage from the kubelets. For other metrics, adapters expose custom metrics (for example from Prometheus) or external metrics (queue length, request rate). 2. Control loop: the HPA controller checks metrics periodically (every 15 seconds by default). 3. Calculation: it computes the desired number of replicas: desiredReplicas = ceil(currentReplicas × currentMetricValue / targetMetricValue) Example: 4 Pods averaging 90% CPU with a 60% target → ceil(4 × 90 / 60) = 6 Pods. 4. Scaling: it updates the Deployment's replica count, within the configured minReplicas and maxReplicas. Important: CPU and memory utilisation are calculated as a percentage of the container's resource requests. If Pods have no CPU requests, the HPA cannot calculate utilisation — always set resource requests. Example manifest (autoscaling/v2) apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: orders-api spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: orders-api minReplicas: 2 maxReplicas: 20 metrics: - type: Resource resource: name: cpu target: { type: Utilization, averageUtilization: 60 } - type: Pods pods: metric: { name: http_requests_per_second } target: { type: AverageValue, averageValue: "100" } behavior: scaleUp: stabilizationWindowSeconds: 0 policies: [{ type: Percent, value: 100, periodSeconds: 60 }] scaleDown: stabilizationWindowSeconds: 300 policies: [{ type: Pods, value: 2, periodSeconds: 60 }] When multiple metrics are configured, the HPA calculates replicas for each and uses the highest. Stabilisation and behaviour - Scale-down stabilisation (5 minutes by default) prevents "flapping" when load fluctuates. - The behavior section controls how fast scaling happens (percentage or number of Pods per period). - A tolerance (about 10%) avoids scaling for tiny metric differences. Metric types - Resource: CPU or memory utilisation. - Pods: custom per-Pod metrics such as requests per second. - Object: a metric describing another Kubernetes object (for example requests per second on an Ingress). - External: metrics from outside the cluster, such as Azure Service Bus or Kafka queue length. KEDA (Kubernetes Event-Driven Autoscaling) - Extends autoscaling with scalers for queues, streams and databases (Service Bus, Kafka, RabbitMQ, Redis, SQL queries, Prometheus) and supports scaling to zero. Azure Container Apps uses KEDA internally. Related autoscalers - Vertical Pod Autoscaler (VPA): adjusts CPU and memory requests of Pods rather than replica count. Avoid using VPA and HPA on the same CPU or memory metric. - Cluster Autoscaler or Karpenter (and AKS node autoprovisioning): add or remove nodes when Pods cannot be scheduled or nodes are underused. HPA adds Pods; the cluster autoscaler adds machines for them. Best practices - Set accurate resource requests and readiness probes so new Pods receive traffic only when ready. - Choose metrics that reflect load: CPU for compute-bound APIs, queue length for workers, request rate or latency for I/O-bound services. - Keep minReplicas at 2 or more for availability, and set sensible maximums to control costs and protect dependencies such as databases. - Ensure the application starts quickly and handles graceful shutdown during scale-in. - Load-test autoscaling behaviour before production peaks. Interview summary: "The HPA periodically compares current metrics with targets and adjusts the replica count using desired = ceil(current × current metric / target), within min and max limits and with stabilisation windows. It works on CPU, memory, custom and external metrics, and is often combined with KEDA and the cluster autoscaler."
Ravindra Wadile · · 0 likes