Sub topic 13
Troubleshooting autoscalers
This chapter assumes you can apply manifests with kubectl, read YAML, and know how the Horizontal Pod Autoscaler (HPA) computes replicas from earlier chapters. It covers how to find out why an autoscaler shows unknown metrics or does not act.
Start with three commands
Work from the data source outward. Metrics Server feeds the HPA, and the HPA changes the replica count.
kubectl top pods
kubectl get hpa web
kubectl describe hpa webkubectl top pods proves that the resource metrics API works. If it fails, fix Metrics Server before looking at the HPA. kubectl get hpa shows current and target values, such as <unknown>/70%. kubectl describe hpa shows the conditions and events that explain what the HPA decided.
Metrics show unknown
When get hpa prints <unknown> for the current value, the HPA cannot compute a utilization. Common causes:
- Metrics Server is missing or unhealthy. Check that its Pod runs in
kube-systemand thatkubectl top podsreturns numbers. New Pods can take a minute or so before metrics appear. - No resource requests. Utilization targets are a percentage of the request. A container without a CPU request gives the HPA nothing to divide by. Add
requests.cpuas in the Deployment from the resource chapter. - A sidecar without a request. With a Pod-level
Resourcemetric, every container in the Pod needs a request. One container without one makes the whole metric unavailable. - Wrong
scaleTargetRef. A mistyped name or kind means the HPA finds no Pods.
The describe output names the cause in its conditions. AbleToScale reports whether the HPA can change the target. ScalingActive is False when it cannot read a metric, with a message such as FailedGetResourceMetric. ScalingLimited is True when the result was clamped by minReplicas, maxReplicas, or a behavior policy.
The HPA computes a value but replicas do not change
Check these in order:
- Tolerance. The HPA skips scaling when the ratio of current to target is within 0.1 of 1.0. At a 70% target, 74% usage does nothing.
- Limits. If
ScalingLimitedisTrue, the desired count hitmaxReplicasor a policy. Raise the limit or loosen the policy. - Stabilization window. The
scaleDownwindow of 300 seconds keeps the highest recommendation from the last five minutes. A drop in load will not shrink the Deployment immediately. This is expected. - Manual replicas in the manifest. If you reapply a Deployment that sets
spec.replicas, it resets the count that the HPA chose. Omitreplicasfrom manifests that an HPA manages. - Not-ready Pods. The HPA treats Pods that are starting or not yet ready cautiously, which can delay scale-up.
Replicas increase but Pods stay Pending
If the HPA raised the count, but new Pods show Pending, the problem is capacity, not the HPA. Run kubectl describe pod on a pending Pod and read the events. A FailedScheduling event such as Insufficient cpu means no node can fit the request. With a cluster autoscaler, check its logs and whether the node group has reached its maximum size. Oversized requests are a common cause, because the scheduler reserves what you request, not what the Pod uses.
Scaling that flaps
If replicas rise and fall repeatedly, a short scale-down window or a metric that swings sharply is usually the cause. Lengthen stabilizationWindowSeconds for scale-down and limit the rate with policies. Also check that the target is not so close to normal load that small changes cross it.
Exercise
kubectl get hpa web shows <unknown>/70%, but kubectl top pods returns values for the web Pods. Name two likely causes and where you would confirm them.
Show answer
The Pods probably lack a CPU request, or a sidecar container in the Pod lacks one. Utilization is a percentage of the request, so it cannot be computed. Confirm by reading the Pod spec for resources.requests.cpu on every container, and by reading the ScalingActive condition and events in kubectl describe hpa web.