Getting started with Kube-State-Metrics and Kubernetes Dashboard

Introduction to Kube-State-Metrics

kube-state-metrics is an open-source project to generate metrics about the state of the Kubernetes Cluster Objects. It is a service that listens to the Kubernetes API server. It does not perform any modification on Kubernetes API just reads the required data for metrics.

Where we can use it?

kube-state-metrics exposes raw data unmodified from the Kubernetes API. This allows users to have all the data they require and perform heuristics as they see fit. These metrics are designed to be consumed either by Prometheus itself or by a scraper that is compatible with scraping a Prometheus client endpoint like Kubernetes dashboard, Elasticsearch Metricbeat etc.

How we can use it?

We can build and run kube-state-metrics inside a Kubernetes pod that has access to a service account token that has read-only access to the Kubernetes cluster. This pod will be able to fetch the data and expose it via service. We can deploy other components that can use this data for monitoring, reporting, etc.

How to Deploy?

kube-state-metrics is publicly available over GitHub for build and deployment. You can use docker image as per your deployment strategy as well. Standard deployment of Metrics require the following objects to be created in Kubernetes Cluster -

  1. Service Account
  2. Cluster Role
  3. Cluster Role Binding
  4. Deployment Object for Metrics Docker Image
  5. Service for exposing the metrics

We will deploy it using objects YAML available over GitHub as an example.

$ git clone https://github.com/kubernetes/kube-state-metrics.git$ kubectl apply -f kube-state-metrics/examples/standard/# above command will deploy bare min components to run metrics 
KSM Deployment

Once deployment is done please verify it using following command

$ kubectl get pods --all-namespaces | grep metrics$ kubectl get deployments kube-state-metrics -n kube-system$ kubectl get service -n kube-system | grep metrics
Metrics Component Status

Kube state metrics service exposes all the metrics on /metrics.We will access this service using port-forward for a quick check. Once you port-forward the service, use any browser to access the port. You can see two hyperlinks on the root page.

kubectl port-forward svc/kube-state-metrics 30135:8080 -n kube-system

That’s it! kube-state-metrics is ready to be scrapped. You can do the deployment using helm as well.

Introduction to Kubernetes Dashboard

Kubernetes Dashboard is a web-based Kubernetes user interface that can be used to containerized applications to a Kubernetes cluster, troubleshoot your containerized application, and manage/monitor the cluster resources.

How to Deploy

We can deploy the dashboard using Kubectl directly.

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml

Access the dashboard using proxy

$ kubectl proxy --port=30135Now you can access the dashboard using following URL$ http://localhost:30135/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.

Once you access the above URL it will take you to the authentication page. You can pass your kubeconfig file or you can use the token mechanism as well.

For the token authentication method, you need to create a service account for the Kubernetes dashboard. Bind this service account to the cluster-admin role. We can then use the bearer token for the service account to log in to the dashboard. Steps as follows -

$ kubectl create serviceaccount dash-admin$ kubectl create clusterrolebinding dash-admin \
--clusterrole=cluster-admin --serviceaccount=default:dash-admin

Service account creation has created a secret as well. You can describe that secret to get the token value

$ kubectl get secrets | grep dash-admin$ kubectl describe secret dash-admin-token-XXXX

Once you pass the token value, you can access the dashboard.

You can monitor node resources as well.

Install kube-state-metrics to expose statistics about the resource utilization of Kubernetes objects. Metric integrations allow Dashboard to show cpu/memory usage graphs and sparklines of resources running inside the cluster. dashboard-metrics-scraper uses the Metrics API to gather metrics.

Error if you don't have metrics deployed

That's it for this post. In the next post, I will show how to monitor clusters using Elasticsearch and Metricbeat.

Stay Safe and Keep learning. 🌎

PS: If this article has been useful to you, please leave few claps! (you are allowed up to 50)

In quest of understanding How Systems Work !

Get the Medium app