ivinco
Kubernetes Metrics Server: A comprehensive guide

Kubernetes Metrics Server: A comprehensive guide

Pavel RykovPavel Rykov··Updated

The advancement in computing has allowed for the growth and development of numerous technologies that enable businesses to improve their efficiency and effectiveness. Among these technologies is Kubernetes, an open-source container orchestration system for automating deployment, scaling, and management of containerized applications.

Within the Kubernetes ecosystem, the Metrics Server supplies the CPU and memory metrics that autoscaling and kubectl top use. This article will provide a comprehensive guide to the Kubernetes Metrics Server, including its background, installation, configuration, and usage.

What is Kubernetes Metrics Server?

Metrics Server is "a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines." Its CPU and memory metrics feed the Horizontal Pod Autoscaler (HPA), the Vertical Pod Autoscaler and kubectl top. Its README says it is "meant only for autoscaling purposes", so use a monitoring system such as Prometheus for history and alerts, as our Kubernetes monitoring guide explains.

Read more about optimizing performance of Kubernetes in Kubernetes Performance Tuning article.

Before the Metrics Server, Kubernetes used Heapster for gathering and accessing metrics. Heapster, however, was deprecated due to its limitations. Kubernetes introduced the Metrics Server as a more efficient, lightweight solution. It collects metrics from the Kubelet running on each node, then aggregates them and stores them in memory.

Installation of Metrics Server

Before starting the installation, make sure that you have a running Kubernetes cluster. If you do not have one, you can set it up using various services like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or tools like minikube for local development.

To install the Metrics Server, you can use Helm, a package manager for Kubernetes, or you can apply the necessary YAML files directly. In this guide, we will apply the YAML file directly:

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

Configuration

The Metrics Server collects data from each kubelet's /metrics/resource endpoint (node metrics). However, depending on your cluster's configuration, additional settings may be needed. You can configure the Metrics Server by modifying the components.yaml file before applying it.

For example, if your Kubernetes nodes use self-signed certificates, the Metrics Server must ignore certificate validation to collect data. To do this, add the following arguments under the Metrics Server deployment:

spec:
  template:
    spec:
      containers:
      - name: metrics-server
        args:
        - --kubelet-insecure-tls

Usage

Once the Metrics Server is up and running, it will start collecting and storing metrics data. Kubernetes components can now access this data using the Metrics API.

You can view node and pod metrics using the kubectl top command:

kubectl top nodes
kubectl top pods

These commands will show CPU and memory usage for your nodes and pods.

Read more about performance in Kubernetes Performance Tuning article.

Also, the Metrics Server is necessary for the functioning of the Horizontal Pod Autoscaler (HPA), which automatically scales the number of pods in a replication controller, deployment, or replica set based on observed CPU utilization.

Conclusion

The Metrics Server is an essential component of the Kubernetes ecosystem. It provides the metrics that autoscaling needs, replacing the deprecated Heapster with a more efficient, scalable solution. Once it is installed and configured, the autoscalers and kubectl top have the data they need.

Remember, the complexity of Kubernetes demands continuous learning and adaptation. The Kubernetes community is continually improving, so stay updated by frequently visiting the Kubernetes GitHub repository and the official Kubernetes documentation.


Need help with your Kubernetes setup? Our Kubernetes consulting team starts with read-only access and a written list of what to fix first. We have managed more than 1,350 production servers, and the first conversation is free.

Frequently Asked Questions

What is Kubernetes Metrics Server and why do you need it?

Metrics Server is a lightweight, in-memory aggregator of resource metrics from every node's kubelet. It provides the CPU and memory data behind the Horizontal Pod Autoscaler, the Vertical Pod Autoscaler and kubectl top. Without it or another Metrics API provider, the HPA can't scale on CPU or memory. It is meant only for autoscaling, so use Prometheus for monitoring.

How do you install Kubernetes Metrics Server?

Apply the official components YAML: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml. If kubelet certificates aren't signed by the cluster CA, --kubelet-insecure-tls skips the check, which the README allows for testing only. Alternatively, install via Helm with helm install metrics-server metrics-server/metrics-server. Verify with kubectl top nodes. If it returns data, the server is running.

What is the difference between Metrics Server and Prometheus?

Metrics Server provides real-time CPU and memory metrics with short retention (kept in memory only). It's designed for autoscaling decisions, not historical analysis. Prometheus scrapes and stores time-series metrics long-term, supports custom metrics, alerting rules, and complex queries via PromQL. Use Metrics Server for HPA and Prometheus for monitoring, dashboards, and alerting.

How does Metrics Server enable Horizontal Pod Autoscaling?

HPA queries the Metrics API (served by Metrics Server) every 15 seconds to check current CPU or memory utilization against your target threshold. If utilization exceeds the target, HPA increases pod replicas; if below, it scales down. Without Metrics Server installed, HPA has no data source and cannot make scaling decisions. When it shows <unknown> targets, see our autoscaling guide.