In this lesson, you’ll learn about the Horizontal Pod Autoscaler (HPA) in Kubernetes and discover how it automatically adjusts the number of running pods based on the current resource usage. As its name implies, the HPA scales the pods horizontally—deploying more instances when demand increases and removing excess pods when demand decreases.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
The HPA is one of Kubernetes’ many controllers that works by adjusting the replica count of a Deployment. It scales up the replicas as demand grows and scales them down when the pressure eases.
How the HPA Works
The HPA monitors your application’s resource consumption by leveraging metrics collected from a metrics server. This server continuously tracks resource usage across nodes and pods in the cluster, providing accurate data for the HPA to decide when to scale. The HPA configuration typically involves:- Defining a Deployment for the application.
- Setting resource limits and requests within the Deployment.
- Creating an HPA object that targets the Deployment.
Example Configuration
Below is an example YAML configuration for a Deployment that uses a container image with defined CPU resource limits:Explanation
- The
scaleTargetRefsection specifies that the HPA targets the Deployment named “myapp”. - The autoscaler will maintain the replica count between 1 and 10 based on CPU usage.
- It continuously monitors the average CPU utilization and aims to maintain a 50% utilization threshold across all pods.
Managing the HPA
To create the HPA, run the following command:- The “TARGETS” column shows both the current and the desired average CPU utilization.
- “MINPODS” and “MAXPODS” indicate the minimum and maximum replica limits.
- “REPLICAS” reflects the current number of running pods.
- “AGE” shows the duration for which the HPA has been active.
Ensure your metrics server is properly configured and running, as the HPA relies on accurate metrics to make scaling decisions.