DEV Community

loading...
Cover image for Kubernetes Monitoring: Metrics Server

Kubernetes Monitoring: Metrics Server

chrisedrego
Senior DevOps | SRE. Love playing with Systems OpenSource Enthusiast. I Produce Tech contents with Meme's
・3 min read

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

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 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, Vertical Pod AutoScaler, kubectl top, or Kubernetes 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
Enter fullscreen mode Exit fullscreen mode

kubectl top nodes

kubectl top pods

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
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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/

if you found this article useful, feel free click ❤️ Heart, many times or share it with your friends.

Discussion (0)