Kubernetes Monitoring Series: Understanding the what, why, how & behind the scenes — 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.
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.
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
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.
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
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/
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.
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.
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/