Prometheus Monitoring K8S cluster

Yst@IT
4 min readMay 12, 2020

For this post, I will be using cAdvisor and kube-state-metrics to monitor K8S cluster itself as well as the pods and containers running on the cluster.

What is cAdvisor?

For more details, please refer github and prometheus.

What is kube-state-metrics?

For more details, please refer github.

Before we start, you should have a K8S cluster ready. Things we will be doing

  1. Deploy Prometheus with Configmap
  2. Deploy Grafana, setup and import dashboards
  3. Deploy cAdvisor
  4. Deploy kube-state-metrics
  5. Expose Prometheus internally and Grafana to internet

Let’s get started.

Prometheus

First create a Configmap yaml file with content below.

Next, create a deployment file with content below.

Now deploy Configmap and Prometheus with the yaml files.

Prometheus Monitoring K8S cluster
Prometheus Monitoring K8S cluster

Prometheus is up and running now.

Grafana

For this demo, I am using the simplest way I know to deploy Grafana.

kubectl create deploy grafana --image grafana/grafana
Prometheus Monitoring K8S cluster

cAdvisor

I am using helm to install cAdvisor. We add a repository first, then use helm to install cAdvisor.

helm repo add code-chris https://code-chris.github.io/helm-charts

Prometheus Monitoring K8S cluster

kube-state-metrics

Install it from github project so first clone it to your computer.

git clone https://github.com/kubernetes/kube-state-metrics.git

Prometheus Monitoring K8S cluster

Change all the namespace variables under /examples/standard/ from kube-system to default, then deploy it.

Prometheus Monitoring K8S cluster
kubeclt apply -f kube-state-metrics/examples/standard/kubectl get all -l app.kubernetes.io/name=kube-state-metrics -n kube-system
Prometheus Monitoring K8S cluster

Expose Prometheus and Grafana

Expose Prometheus to ClusterIP. Since my K8S is built on AWS, therefore I will expose Grafana through CLB.

kubectl expose deploy prometheus --type ClusterIP --port 9090
kubectl expose deploy grafana --type LoadBalancer --port 3000
Prometheus Monitoring K8S cluster

Grafana Configuration

Access to CLB url at port 3000. Default credential is admin / admin.

Prometheus Monitoring K8S cluster

Change default password once login.

Prometheus Monitoring K8S cluster

First add Prometheus as data source. After “Add data source”, select Prometheus.

Prometheus Monitoring K8S cluster

Just enter Prometheus deployment location then scroll down to the bottom and save & test.

Prometheus Monitoring K8S cluster
Prometheus Monitoring K8S cluster

Next we import Grafana dashboard for cAdvisor and kube-state-metrics.

Prometheus Monitoring K8S cluster

Input 9706 and click the cursor away so that it will load the information automatically.

Prometheus Monitoring K8S cluster

Choose Prometheus as data source and click import.

Prometheus Monitoring K8S cluster

Once redirect to kube-state-metrics dashboard, change time range and refresh interval. Wait for a few minutes for Prometheus to collect datas and Grafana will show it a while later.

Prometheus Monitoring K8S cluster

Repeat same step to add dashboard for cAdvisor. The ID is 3119.

Prometheus Monitoring K8S cluster

That’s it!

--

--

Yst@IT

Cloud Solution Architect, focusing on Oracle Cloud Infrastructure currently.