HPA creates more pods than expected
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding HPA: Horizontal Pod Autoscaler
The Horizontal Pod Autoscaler (HPA) is a Kubernetes resource that automatically manages the number of pod replicas within a deployment or replication controller. By continuously monitoring the resource consumption (like CPU or memory usage) of pods, HPA makes scaling decisions to match the workload demands. However, there are instances when HPA may scale out more pods than expected, leading to potential resource inefficiencies.
Why HPA Creates More Pods
1. Misconfigured Target Metrics
HPA operates based on defined metrics, primarily targeting CPU utilization or custom metrics. If these targets are not accurately set, HPA may overestimate the required number of pods.
Example:
Suppose a deployment has a CPU target utilization of 50%. If the current average CPU utilization is 60% across 5 pods, HPA might conclude that more pods are needed. The formula HPA uses is:
In this case:
If such metrics are inaccurately high, scaling might produce more pods than necessary.
2. Incorrect Resource Requests or Limits
Pods are configured with resource requests and limits. If these are misconfigured (e.g., underestimated resource requests), it can result in HPA overscaling.
Example:
• Pods are set with CPU requests of 100m (millicores). • Actual usage spikes to 200m, but HPA assumes more pods can handle this when fewer might suffice with proper request settings.
3. Delayed Metrics and System Lag
Prometheus, a common metric server used with HPA, collects metrics at a regular interval. If there's a delay or lag in these metrics, HPA can overreact to outdated data.
Example:
Consider a scenario where metrics indicate a temporary spike in usage due to a short-lived process. By the time HPA scales up pods, the system load may have already returned to normal levels.
4. Burst Workloads
Kubernetes and HPA do not inherently understand the nature of workloads. If a deployment experiences intermittent burst workloads, HPA might scale more pods during peaks, leading to excess capacity during normal operations.
5. Stale Metrics or Configurations
Systems may still be referencing outdated metrics or configurations, leading HPA to make decisions on incorrect assumptions. It's essential to periodically review and update metric configurations.
Ensuring Accurate Scaling
Strategies
- Tune Metrics: Regularly adjust the target metrics to reflect realistic operating conditions.
- Review Resources: Accurately set pod resource requests and limits.
- Monitoring: Implement continuous monitoring to detect anomalies in HPA behavior.
- Update Intervals: Adjust the update intervals for metrics servers to reduce lag.
Observability Tools
Leverage observability tools such as Prometheus, Grafana, and custom scripts for real-time analysis of autoscaler behavior. These tools provide insights into whether HPA is functioning within expected norms.
Summary
Here's a quick summary of key considerations when dealing with unexpected pod creation:
| Issue | Description | Solution Suggestions |
| Misconfigured Target | Incorrect metric targets causing overestimation. | Regularly review and set accurate targets. |
| Resource Misconfiguration | Inaccurate resource requests or limits. | Ensure proper allocation of requests/limits. |
| Delayed Metrics | Lag in data may prompt unnecessary scaling. | Optimize metrics collection intervals. |
| Burst Workloads | Temporary spikes affecting scaling decisions. | Implement burst-specific policies. |
| Stale Configurations | Old configurations/metrics leading to inefficient scaling. | Update configurations frequently. |
Conclusion
The key to effective HPA performance lies in accurately defining the conditions under which pods scale and continuously monitoring real-world performance. By proactively managing and configuring HPA settings, it's possible to avoid situations where more pods are created than necessary, optimizing both resource use and cost.
Related reading
- HTCONDORkubernetes / k8s Unable to start minicondor image within k8s - condor_master not working
- HttpContext.Connection.RemoteIpAddress returns private address for asp.net core in kubernetes
- Hyper-v and VirtualBox conflict in Dockers with Minikube
- I need to get resource usage of Pods in a Kubernetes Cluster with kubernetes python client
- Hungarian Algorithm finding minimum number of lines to cover zeroes?
- Hungarian algorithm multiple jobs per worker
- I want to get the host MAC address in kubernetes pod where that pod runing on
- Identify Reason for application shutdown in Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.