How to install Metrics Server on Kubernetes cluster

 Background

This post is about installing the Metrics Server on a Kubernetes cluster. We had provisioned a 3 node Kubernetes cluster in the previous post. In a multi-node cluster, the workloads will get scheduled on different nodes. If we want to find out which pods are consuming more resources or which nodes are having more resources, we need some way to aggregate the pod level metrics. This is where Metrics Server comes into the picture.

How to install Metrics Server on Kubernetes cluster

Kubernetes does not provide a default metrics aggregator. We need to install Metrics Server which helps to collect the container level metrics like CPU or RAM usage. These metrics can then be used by other Kubernetes APIs and objects like the Horizontal Pod Autoscaler (HPA) or Vertical Pod Autoscaler (VPA).

We will use a manifest file to install the required components onto our Kubernetes cluster. The manifest file is available in the Github repo for the Metrics Server project. Run the following command to apply the manifest

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

This will create the service account, RBAC for cluster role, cluster role binding, expose the metrics server as service etc. Verify these by listing the pods in the kube-system namespace


We can see that the metrics-server pod is running but the container is not ready. Use the describe command of Kubectl to identify the reason.

kubectl -n kube-system describe pod metrics-server-<<dynamic-name>>


In the events section, we can see there is a warning which suggests that the readiness probe failed. This happens because the pod is unable to establish communication with the Kube API server. We need to override some defaults to fix this issue.

Override the metrics server command

We will go and edit the metrics-server deployment. Use the following command to edit the deployment

k -n kube-system edit deploy metrics-server

scroll down into the section where we define the settings for the container. Add the following lines just before the image tag.

command:

        - /metrics-server

        - --kubelet-insecure-tls

        - --kubelet-preferred-address-types=InternalIP

Save the changes. The deployment will kill the older version of the pod and recreate a new pod for the metrics server. After making the above changes we will be able to successfully run the metrics server on our Kubernetes cluster.

Verify Metrics Server functionality

Run the top command with node and pods subcommands to find out the resource usage at node & pods levels respectively. We can also examine the resource usage at the individual container level where there are multi-container pods by passing the --containers flag to the kubectl top pods --containers. Below screenshots show the output of these commands




Youtube video

All the steps mentioned here are demonstrated in the Youtube video.


Conclusion

Metrics Server helps to aggregate the CPU & RAM related metrics across nodes in a multi-node Kubernetes cluster. Hope you found this post useful. The default settings need to be overridden to allow insecure traffic between the kubelet and the metrics server. 

Until next time, Code with Passion, Strive for Excellence

Share:
spacer

No comments:

Post a Comment