In this lesson, you will learn how to configure the Horizontal Pod Autoscaler (HPA) to automatically scale your application replicas based on real-time CPU consumption.
The Core Concept
The HPA is a Kubernetes controller that monitors your pods and adds or removes replicas to keep the average CPU usage close to a target percentage you define. Before the HPA can work, your deployment must have resources.requests configured, as the HPA uses these values to calculate the percentage of resource utilization. Once the Metrics Server is running, the HPA controller periodically checks the CPU usage against your target and adjusts the replicas count accordingly.
Creating the HPA Object
To implement this, you define an HorizontalPodAutoscaler object. This object tells Kubernetes which deployment to track, the minimum and maximum number of replicas allowed, and the CPU threshold that triggers a scaling event.
Consider this configuration:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: cpu-autoscaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-web-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
How it Works
In the example above, if the average CPU utilization across all pods in my-web-app exceeds 50% of the requested CPU, the HPA will spin up new pods until the load drops or the maxReplicas count (10) is reached. Conversely, when traffic subsides and CPU usage falls below the target, the HPA will scale the deployment back down toward the minReplicas (2).
By decoupling your replica count from static manual updates, the HPA ensures your application maintains performance during traffic spikes while optimizing infrastructure costs during idle periods. Ensure your resources.requests are accurate, as the HPA relies on these baseline values to perform its calculations; if no requests are set, the HPA will not be able to determine the percentage of utilization.
Key Takeaway: The Horizontal Pod Autoscaler uses the Metrics Server and your defined CPU resource requests to dynamically scale your application replicas, ensuring stability during high load and efficiency during low demand.