How to Use kubectl autoscale for Kubernetes Pods
Use
kubectl autoscale to create a Horizontal Pod Autoscaler (HPA) that automatically adjusts the number of pods in a deployment based on CPU usage or other metrics. The basic command is kubectl autoscale deployment [name] --min=[minPods] --max=[maxPods] --cpu-percent=[targetCPU]. This helps keep your app responsive and efficient.Syntax
The kubectl autoscale command creates a Horizontal Pod Autoscaler for a deployment or replicaset. Here is the syntax:
kubectl autoscale deployment [DEPLOYMENT_NAME]: Targets the deployment to autoscale.--min=[MIN_PODS]: Minimum number of pods to keep running.--max=[MAX_PODS]: Maximum number of pods allowed.--cpu-percent=[TARGET_CPU]: Target average CPU utilization percentage to maintain.
bash
kubectl autoscale deployment [DEPLOYMENT_NAME] --min=[MIN_PODS] --max=[MAX_PODS] --cpu-percent=[TARGET_CPU]
Example
This example creates an autoscaler for a deployment named webapp. It keeps at least 2 pods, scales up to 5 pods, and targets 50% CPU usage per pod.
bash
kubectl autoscale deployment webapp --min=2 --max=5 --cpu-percent=50
Output
horizontalpodautoscaler.autoscaling/webapp autoscaled
Common Pitfalls
Common mistakes when using kubectl autoscale include:
- Not having resource requests set on pods, so CPU usage can't be measured properly.
- Setting
--minhigher than--max, which causes errors. - Using
kubectl autoscaleon unsupported resources like pods directly instead of deployments or replicasets. - Expecting immediate scaling; autoscaling reacts based on metrics over time.
bash
kubectl autoscale deployment webapp --min=5 --max=3 --cpu-percent=50 # This will fail because min is greater than max # Correct usage: kubectl autoscale deployment webapp --min=2 --max=5 --cpu-percent=50
Output
error: min cannot be greater than max
horizontalpodautoscaler.autoscaling/webapp autoscaled
Quick Reference
| Option | Description |
|---|---|
| --min | Minimum number of pods to run |
| --max | Maximum number of pods allowed |
| --cpu-percent | Target average CPU utilization percentage |
| deployment [NAME] | Name of the deployment to autoscale |
Key Takeaways
Use kubectl autoscale to create a Horizontal Pod Autoscaler for deployments.
Always set resource requests on pods for accurate CPU-based autoscaling.
Ensure the minimum pod count is less than or equal to the maximum pod count.
Autoscaling adjusts pods based on CPU usage over time, not instantly.
kubectl autoscale works on deployments or replicasets, not individual pods.