Metrics Server

Difficulty: Intermediate

Question

What is the Metrics Server in Kubernetes, how does it collect data, and what depends on it?

Answer

Metrics Server is a lightweight cluster add-on that collects current CPU and memory usage for nodes and Pods from every kubelet and exposes it through the Kubernetes API as metrics.k8s.io. kubectl top, the Horizontal Pod Autoscaler's resource metrics and the Vertical Pod Autoscaler's recommender all read from it. It is not a monitoring system: it keeps only the latest values in memory and stores no history.

How it collects data

  1. Each kubelet exposes resource usage on its /metrics/resource endpoint, gathered from the container runtime and cgroups.
  2. Metrics Server scrapes every kubelet on a fixed interval (15 seconds by default, set with --metric-resolution).
  3. It keeps the latest sample per node and Pod in memory.
  4. It registers an APIService for v1beta1.metrics.k8s.io, so the API server's aggregation layer forwards requests for that group to Metrics Server.
  5. Clients like the HPA controller or kubectl top call the API server, which proxies to Metrics Server.

Because it is served through the normal API, access is controlled by RBAC like any other resource.

What depends on it

  • HPA with CPU or memory targets: without Metrics Server, an HPA shows <unknown>/70% and events like failed to get cpu utilization: unable to get metrics for resource cpu.
  • kubectl top nodes and kubectl top pods: fail with error: Metrics API not available.
  • VPA recommender: uses it for live usage (and optionally Prometheus for history).
  • Dashboards like the Kubernetes Dashboard show live usage from it.

Managed clusters such as GKE and AKS install it by default. On EKS it is available as an add-on, and on kind or kubeadm you install it yourself.

Metrics Server vs a monitoring stack

Metrics ServerPrometheus
DataCPU and memory onlyAny metric, including app metrics
RetentionLatest value, in memoryTime series stored on disk
APImetrics.k8s.ioPromQL
PurposeAutoscaling and kubectl topMonitoring, alerting, dashboards

For autoscaling on custom metrics such as requests per second or Kafka lag, you need a different API: custom.metrics.k8s.io or external.metrics.k8s.io, typically served by Prometheus Adapter or KEDA.

Common problems

  • TLS errors in local clusters: kubelets often use self-signed serving certificates, so Metrics Server fails to scrape. In kind or minikube people add --kubelet-insecure-tls; in production, use properly signed kubelet serving certs instead.
  • Pods without requests: HPA computes utilization as usage divided by request, so missing CPU requests make CPU-based scaling impossible.
  • Wrong expectations: it is not accurate enough for billing or capacity planning, and restarts lose all data. Heapster, its predecessor, was removed years ago.
Where Metrics Server data comes from. Kubelets report usage, Metrics Server caches it, and the API server serves it to HPA and kubectl top.
  1. Container runtime and cgroups: Raw CPU and memory usage per container.
  2. kubelet /metrics/resource: Each node exposes its resource metrics.
  3. Metrics Server: Scrapes every 15s and keeps the latest values in memory.
  4. API aggregation: metrics.k8s.io: API server proxies requests to Metrics Server.
  5. HPA, VPA, kubectl top: Consumers read current usage.

Code examples

Install and verify Metrics Server

# Install the latest release
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# Is the aggregated API registered and available?
kubectl get apiservice v1beta1.metrics.k8s.io

# Raw API call (what kubectl top does under the hood)
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes | head -c 400

# Consumers
kubectl top nodes
kubectl get hpa

If the APIService shows `AVAILABLE False`, check the metrics-server Pod logs for kubelet scrape or TLS errors before debugging the HPA itself.

Key points

  • Metrics Server collects CPU and memory from kubelets and serves them via `metrics.k8s.io`.
  • It stores only the latest values in memory; it is not for monitoring or history.
  • Required by HPA resource metrics, `kubectl top`, and used by VPA.
  • Custom and external metrics need Prometheus Adapter, KEDA or similar.
  • Default scrape interval is 15 seconds.

Common follow-up questions

  • How does the HPA calculate the desired number of replicas from metrics?
  • How would you scale on requests per second instead of CPU?
  • What is the API aggregation layer and why does Metrics Server use it?
  • Why might `kubectl top` work while the HPA still shows unknown?

Interview tips

  • Stress that Metrics Server is for autoscaling, not monitoring; confusing it with Prometheus is a common mistake.
  • Mention the aggregation layer and `APIService`; it shows you understand how it plugs into the API.
  • Bring up the requests requirement for percentage-based HPA, which interviewers often probe.

Concepts covered

Metrics Server, metrics.k8s.io, Aggregation Layer, kubelet resource metrics, HPA

More Kubernetes & Orchestration questions