Horizontal Pod Autoscaler in Kubernetes - Time & Space Complexity
We want to understand how the time it takes for the Horizontal Pod Autoscaler (HPA) to adjust pods changes as the number of pods or metrics grows.
How does the HPA's work scale when it checks and updates pods?
Analyze the time complexity of the following Kubernetes Horizontal Pod Autoscaler configuration.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: example-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: example-deployment
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
This HPA watches CPU usage and adjusts the number of pods between 1 and 10 based on average CPU utilization.
The HPA controller regularly checks metrics and adjusts pods.
- Primary operation: Loop over all pods to get their CPU usage metrics.
- How many times: Once per check interval, for each pod in the deployment.
As the number of pods increases, the HPA must check more metrics, so the work grows with the number of pods.
| Input Size (pods) | Approx. Operations |
|---|---|
| 10 | 10 metric checks |
| 100 | 100 metric checks |
| 1000 | 1000 metric checks |
Pattern observation: The number of operations grows directly with the number of pods.
Time Complexity: O(n)
This means the time to check and adjust pods grows linearly as the number of pods increases.
[X] Wrong: "The HPA checks all pods instantly, so time does not grow with pod count."
[OK] Correct: Each pod's metrics must be checked individually, so more pods mean more work and longer checks.
Understanding how autoscaling scales with workload size shows you can think about system efficiency and resource management, a key skill in real-world DevOps.
What if the HPA used aggregated metrics instead of per-pod metrics? How would the time complexity change?
Practice
Horizontal Pod Autoscaler (HPA) in Kubernetes?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 BQuick Check:
HPA = automatic pod scaling [OK]
- Confusing HPA with manual scaling
- Thinking HPA scales nodes instead of pods
- Assuming HPA handles backups
webapp targeting 50% CPU usage with minimum 2 and maximum 5 pods?Solution
Step 1: Recall correct kubectl autoscale syntax
The correct command iskubectl 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 CQuick Check:
Correct autoscale syntax = kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 [OK]
- Using wrong flags like --min-pods or --cpu
- Misspelling 'autoscale' or 'deployment'
- Omitting required flags
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 1
maxReplicas: 4
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60What happens if the CPU usage stays at 80% for a while?
Solution
Step 1: Understand HPA scaling behavior
HPA increases pods when CPU usage exceeds target averageUtilization (60%). Here CPU is 80%, which is higher.Step 2: Check min and max replicas limits
Pods can scale between 1 and 4. Since CPU is high, pods will increase up to max 4 to reduce load.Final Answer:
The number of pods will increase up to 4 to reduce CPU load -> Option AQuick Check:
CPU > target => pods increase [OK]
- Thinking pods scale down when CPU is high
- Ignoring min/max replica limits
- Assuming deployment deletion on high CPU
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: test-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70What is the most likely reason for no scaling?
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 AQuick Check:
Missing metrics = no scaling [OK]
- Assuming minReplicas too low stops scaling
- Confusing minReplicas and maxReplicas values
- Ignoring metric availability
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?Solution
Step 1: Use autoscaling/v2 API for multiple metrics
autoscaling/v2 supports multiple metrics including resource and custom metrics.Step 2: Check metric types and targets
CPU uses type Utilization with averageUtilization; custom metric uses type Pods with metric name and AverageValue target.Step 3: Verify minReplicas and maxReplicas
Minimum 3 and maximum 10 pods are correctly set in spec.Final Answer:
YAML with cpu resource metric (Utilization) and pods metric for requests_per_second -> Option DQuick Check:
Multiple metrics with correct types = apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Pods pods: metric: name: requests_per_second target: type: AverageValue averageValue: "100" [OK]
- Using autoscaling/v1 which does not support multiple metrics
- Wrong metric types or target fields
- Mixing resource and custom metric syntax incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
