📖 Scenario: You manage a web application running on Kubernetes. To handle varying user traffic, you want to automatically adjust the number of pods based on CPU usage.
🎯 Goal: Learn how to create a Horizontal Pod Autoscaler (HPA) in Kubernetes that scales the number of pods of a deployment based on CPU usage.
📋 What You'll Learn
Create a deployment named webapp with 2 replicas
Add resource requests for CPU in the deployment
Create a Horizontal Pod Autoscaler targeting the webapp deployment
Set the CPU utilization target to 50%
Verify the HPA configuration
💡 Why This Matters
🌍 Real World
Horizontal Pod Autoscaler helps keep applications responsive by automatically adjusting the number of pods based on CPU load, just like how a car adjusts speed based on traffic.
💼 Career
Understanding HPA is essential for Kubernetes administrators and DevOps engineers to ensure applications scale efficiently and cost-effectively.
Progress0 / 4 steps
1
Create a deployment named webapp with 2 replicas
Write a Kubernetes deployment YAML named webapp-deployment.yaml that creates a deployment called webapp with 2 replicas running the image nginx. Include container port 80.
Kubernetes
Hint
Use kind: Deployment, set replicas: 2, and specify image: nginx with port 80.
2
Add CPU resource requests to the deployment
In the webapp-deployment.yaml, add CPU resource requests of 100m under resources.requests.cpu for the container named nginx.
Kubernetes
Hint
Under the container, add resources: then requests: with cpu: 100m.
3
Create a Horizontal Pod Autoscaler targeting the webapp deployment
Write a YAML file named webapp-hpa.yaml that creates a Horizontal Pod Autoscaler targeting the deployment webapp. Set the minimum replicas to 2, maximum replicas to 5, and target average CPU utilization to 50%.
Kubernetes
Hint
Use kind: HorizontalPodAutoscaler, set scaleTargetRef to deployment webapp, and set CPU target to 50%.
4
Display the Horizontal Pod Autoscaler status
Write the exact command to display the status of the Horizontal Pod Autoscaler named webapp-hpa in the default namespace.
Kubernetes
Hint
Use kubectl get hpa webapp-hpa to see the autoscaler status.
Practice
(1/5)
1. What is the main purpose of a Horizontal Pod Autoscaler (HPA) in Kubernetes?
easy
A. Manually increase pod count when needed
B. Automatically adjust the number of pods based on resource usage
C. Scale the number of nodes in the cluster
D. Backup pod data automatically
Solution
Step 1: Understand HPA functionality
HPA monitors resource usage like CPU or memory and adjusts pod count automatically.
Step 2: Differentiate from other scaling types
HPA scales pods, not nodes or manual actions, and does not handle backups.
Final Answer:
Automatically adjust the number of pods based on resource usage -> Option B
Quick Check:
HPA = automatic pod scaling [OK]
Hint: HPA changes pod count automatically by resource use [OK]
Common Mistakes:
Confusing HPA with manual scaling
Thinking HPA scales nodes instead of pods
Assuming HPA handles backups
2. Which of the following is the correct command to create a Horizontal Pod Autoscaler for a deployment named webapp targeting 50% CPU usage with minimum 2 and maximum 5 pods?
easy
A. kubectl autoscale deployment webapp --cpu=50 --min=2 --max=5
B. kubectl create hpa webapp --cpu=50 --min-pods=2 --max-pods=5
C. kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5
D. kubectl autoscale deployment webapp --cpu-percent=50 --min-pods=2 --max-pods=5
Solution
Step 1: Recall correct kubectl autoscale syntax
The correct command is kubectl autoscale deployment [name] --cpu-percent=[value] --min=[minPods] --max=[maxPods].
Step 2: Match options with correct syntax
kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 matches the correct syntax exactly with proper flags and values.
Final Answer:
kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 -> Option C
A. The pods do not expose memory metrics, so HPA has no data
B. minReplicas is set too low
C. maxReplicas is less than minReplicas
D. The deployment name is incorrect
Solution
Step 1: Check metric type and data availability
HPA needs metrics from pods. If memory metrics are not exposed or collected, HPA cannot scale.
Step 2: Verify other spec values
minReplicas and maxReplicas are valid (2 and 5). Deployment name looks correct. So likely missing metrics cause no scaling.
Final Answer:
The pods do not expose memory metrics, so HPA has no data -> Option A
Quick Check:
Missing metrics = no scaling [OK]
Hint: HPA needs metrics; no metrics means no scaling [OK]
Common Mistakes:
Assuming minReplicas too low stops scaling
Confusing minReplicas and maxReplicas values
Ignoring metric availability
5. You want to create an HPA that scales a deployment named api-server based on both CPU usage (target 70%) and custom metric requests_per_second (target 100). The minimum pods should be 3 and maximum 10. Which YAML snippet correctly defines this HPA?