Sub topic 4
Metrics Server: where scaling metrics come from
This chapter assumes you can apply manifests with kubectl, you know how Deployments and Pods relate, and you have set CPU and memory requests as covered in the previous chapter. It explains the component that feeds CPU and memory numbers to the Horizontal Pod Autoscaler (HPA), and how to confirm it works.
What Metrics Server does
Metrics Server is a cluster add-on that collects current CPU and memory usage for every node and Pod. It does not store history. It keeps only the latest sample in memory.
The data path has four steps:
- Each kubelet measures container usage on its node.
- Metrics Server scrapes every kubelet, by default every 15 seconds.
- Metrics Server registers itself with the Kubernetes API server as the
metrics.k8s.ioAPI. - Clients such as
kubectl topand the HPA read from that API.
Because the HPA reads through the API server, it never talks to Metrics Server directly. If the metrics.k8s.io API is unavailable, the HPA cannot read resource metrics.
What it is not
Metrics Server is built for autoscaling, not monitoring. It has no history, no graphs, and no alerting. Use a system such as Prometheus for those. Custom and external metrics come from other APIs, covered in a later chapter.
Installing and checking it
Many managed clusters include Metrics Server. Check before you install:
kubectl get apiservices v1beta1.metrics.k8s.io
kubectl top nodesThe first command should show AVAILABLE as True. If the APIService is missing, install Metrics Server from its release manifest, or use your distribution's add-on, such as minikube addons enable metrics-server.
Local clusters such as kind often fail because kubelet certificates are not signed for the node address. For a throwaway lab only, you can add --kubelet-insecure-tls to the Metrics Server container arguments. Do not use that flag in production.
Once it is running, read Pod usage:
kubectl top podsRight after installation, or right after a Pod starts, kubectl top may report that metrics are not yet available. Wait about a minute and retry.
Why the HPA needs requests
For a Utilization target, the HPA divides a Pod's measured usage by its requested amount. A Pod using 140m CPU with a 200m request is at 70% utilization. If a container has no CPU request, the HPA has nothing to divide by.
| Symptom in `kubectl describe hpa` | Likely cause |
|---|---|
| `unable to get metrics for resource cpu` | Metrics Server missing, or its APIService is not `Available` |
| `missing request for cpu` | A container in the Pods has no CPU request |
| `<unknown>/70%` in `kubectl get hpa` | Either of the above, or Pods too new to have samples |
Note that the HPA counts all containers in a Pod. A sidecar without a request breaks the calculation for the whole Pod, even if your main container has one.
Reading the HPA's view
kubectl get hpa web
kubectl describe hpa webIn the describe output, look at the Conditions section. AbleToScale and ScalingActive should be True. If ScalingActive is False, the reason field names the metrics problem. The Events section records failures such as FailedGetResourceMetric.
Practical limits
- Usage is a short-window average, so brief spikes may not appear.
- Metrics Server's memory use grows with cluster size, so large clusters need more resources for it.
- A single replica is a single point of failure for autoscaling. Production setups often run two replicas.
Exercise
An HPA shows <unknown>/70% for CPU, but kubectl top pods returns numbers for the same Pods. What should you check next, and why?
Show answer
Check that every container in the Pods, including sidecars, declares a CPU request. The HPA computes utilization as usage divided by requested CPU, so a container without a request makes the calculation impossible even though Metrics Server is working. Confirm with kubectl describe hpa and look for missing request for cpu.