Kubernetes Monitoring: Metrics Server
Tags: kubernetes, monitoring, devops, sre
Published: 2021-05-29 08:04:08


**Kubernetes Monitoring Series: Understanding the what, why, how & behind the scenes — Metrics-server.**

![](https://cdn-images-1.medium.com/max/2000/1*QBms8ZxxCHJAb3OwxLsq2w.png)

## What is Metrics Server?

Metrics Server is another useful lightweight tool that can be added to your Kubernetes Monitoring arsenal. As the name suggests Metrics Server provides metrics for resource utilization like CPU & Memory. Metrics Server discovers all the nodes in the cluster and queries the [kubelet](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/) which is an agent that runs on each node and further forwards the details for resource utilization i.e CPU/Memory to Kubernetes API Server which exposes it as a Metrics API endpoint.

In most of the managed Kubernetes as a service offering from cloud vendors, it does come as an addon by default, as well a couple of bootstrapping tools like Kube-up.sh. If you are using minkube it can be enabled as an addon.

## Why do I need the Metrics Server?

Although the Metrics Server misses all the glitter and glamour compared the rest of Monitoring tools offer with visualisation and dashboard, if you use features such as [Horizontal Pod AutoScaler](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/), [Vertical Pod AutoScaler](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler), kubectl top, or [Kubernetes Dashboard](https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/) then you do need the Metrics Server to provide resource utilisation metrics for them.

### How to view the Metrics Server?

There are two ways to capture the information in the Metrics Server.

* **kubectl top command**

* **Metrics endpoints** */apis/metrics.k8s.io/v1beta1*

**kubectl top commands** more of works like the top commands in Linux, which provides the details of the resources utilizes for pods & nodes.

    kubectl top nodes 
    kubectl top pod

![kubectl top nodes](https://cdn-images-1.medium.com/max/2000/1*A77D0vmdsCgO8wzWrBgcMw.png)

![kubectl top pods](https://cdn-images-1.medium.com/max/2000/1*SNmWKq6YT9s-f2TiqxcvBg.png)

Both the commands, provide metrics details about the resources consumptions (CPU/Memory) and displays the order based on the resources usage.

**Kubernetes metrics endpoint** can be accessed directly as follows.

    kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/default/pods/

    kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes

This returns a response about resource utilization for nodes and pods present in the cluster.

## How to install Metrics Server?

Before installing the metrics Server we need to ensure that the Aggregation layer is present. [Aggregation Layer](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/) helps to further extend the existing core Kubernetes APIs by adding additional APIs.

**Enabling metrics server as an addon in minikube**

    minikube addons enable metrics-server

Cloning the Metrics Server repository and applying the manifest.

    git clone [https://github.com/kubernetes-sigs/metrics-server](https://github.com/kubernetes-sigs/metrics-server)
    cd metrics-server
    kubectl apply -f manifests/base/
> # OR

You can also deploy the v3.0.6 that works with some minor tweaks.

    kubectl apply -f [https://gist.githubusercontent.com/chrisedrego/0dc6d79f25c2235934c2835d0e8952c9/raw/e22fdd4fa6476165a46968120afe4bd52538ae0b/kubernetes_metrics_server.yaml](https://gist.githubusercontent.com/chrisedrego/0dc6d79f25c2235934c2835d0e8952c9/raw/e22fdd4fa6476165a46968120afe4bd52538ae0b/kubernetes_metrics_server.yaml)

This basically installs metrics-server along with related Kubernetes Objects.

* Service Account

* APIService (v1beta1.metrics.k8s.io)

* Deployment

* ClusterRole/ClusterRoleBinding/Roles/RoleBinding

* Service.

### Conclusion
>  Metrics Server overall is best suited where we have Horizontal Pod Autoscaler/Vertical Pod Scaler, Kubernetes Dashboard or view the stats emiited by kubectl top command, it’s not ideal solution where the metrics emitted can be scrapped by Prometheus.

### Troubleshooting

Finally, once the metrics server is installed we can wait for few seconds and then try running **kubectl top nodes **or **kubectl top pods **or by hitting the **Metrics Server endpoint **by kubectl get — raw “/apis/metrics.k8s.io/v1beta1/nodes.

Well in case if you do run into an issue where you are not able to scrape the metrics in that case, you can head over the issues tab for the metrics-server repository. [https://github.com/kubernetes-sigs/metrics-server/issues/](https://github.com/kubernetes-sigs/metrics-server/issues/)
> # if you found this article useful, feel free click ❤️ Heart, many times or share it with your friends.