# Monitoring Kubernetes Cluster  with Prometheus and Grafana

In this blog, we will know how to monitor the Kubernetes cluster and

**What is a Kubernetes cluster?**

A Kubernetes cluster is a system for orchestrating and managing containerized applications, providing a platform for automating the deployment, scaling, and operation of application containers.

The cluster consists of two main components: the **master node** and **worker nodes**. The master node controls and manages the overall state of the cluster, including scheduling applications, maintaining cluster configuration, and responding to events. On the other hand, worker nodes host the actual containers and execute the workloads. They communicate with the master node, receive instructions, and ensure that containers are running as intended.

Kubernetes automates tasks such as container scheduling, load balancing, and self-healing, providing a robust and scalable platform for deploying and managing applications across diverse environments.

**Why monitoring is required?**

Monitoring a Kubernetes cluster is important because it helps you to:

* Detect and resolve issues with the cluster and its applications before they become major problems
    
* Optimize the performance of the cluster and its applications by identifying bottlenecks and other inefficiencies
    
* Track the health and utilization of cluster resources such as nodes, pods, deployments, and persistent storage
    

**How monitoring of the Kubernetes Cluster is done?**

In this blog, monitoring of the Kubernetes cluster will be done using Prometheus and Grafana. It involves deploying Prometheus within the cluster to collect metrics from various components and store them in a time-series database.

Grafana is then installed to create customized dashboards that visualize real-time metrics fetched from Prometheus. This monitoring stack is often set up using Helm charts, simplifying deployment and configuration.

Let's delve into the monitoring of clusters practically.

Start the Minikube cluster

```xml
minikube start
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705491616097/5a1f6bce-3c48-4127-a4b1-8f83af24851e.png align="center")

Our local cluster is ready using the Minikube and it is in running state.

We will install the Prometheus and Grafana using the helm. If the helm is not installed use this [link](https://helm.sh/docs/intro/install/#:~:text=Installing%20Helm%201%20From%20The%20Helm%20Project%20The,simple%20as%20getting%20a%20pre-built%20helm%20binary.%20).

Add the Helm chart for Prometheus

```bash
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
```

Update the Helm repo for the latest updates

```bash
helm repo update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705492122758/8d38fcff-598d-43d7-bba0-fe019461a1ed.png align="center")

Install the Prometheus

```bash
helm install prometheus prometheus-community/prometheus
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705492398347/58c26d9f-0936-43e7-9093-b5e15f215a75.png align="center")

Our Prometheus is installed and can be accessed via port 9091

Let's verify the Prometheus installation

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705492544154/21b83850-126e-48b2-a3a5-e53da0baea82.png align="center")

The Prometheus pods are running along with the Prometheus server. Kube-state-metrics as seen in the image is used to expose some Kubernetes metrices like API servers, deployments, pods, etc.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705493091667/ad58d339-36e3-4b3f-a147-3b75fc4b55fc.png align="center")

The Prometheus server is created using the ClusterIP mode. Let's convert this service into a Nodeport service.

**Expose Prometheus Service**

```bash
kubectl expose service prometheus-server --type=NodePort --target-port=9090 --name=prometheus-server-ext
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705493554843/a3141a51-a19c-47c8-8bfd-dbada266abf6.png align="center")

We exposed the Prometheus server using the node port and got the Kubernetes cluster ip using Minikube IP. Now we will access the Prometheus using http://192.168.58.2:31958

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705493690690/60c4144d-dbe1-4010-84f5-fafb57cf00ba.png align="center")

Our Prometheus server is ready to serve. The first step for monitoring the cluster is done.

Grafana

Add the helm repo

```bash
helm repo add grafana https://grafana.github.io/helm-charts
```

Update the helm repo

```bash
helm repo update
```

Install the Grafana

```bash
helm install grafana grafana/grafana
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705494259743/7ea7a8c0-2ba4-42d9-92c6-5ae4c59ac70e.png align="center")

So our Grafana is installed and running.

To get the password for the Grafana run the command as shown in the reference image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705494388139/6a0d9690-22a1-44af-90fc-b6ea9d58a2f4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705494473429/5b136bd9-11d2-42d5-90da-9458b16be2e0.png align="center")

We can see that the grafana is running in the ClusterIP.

Expose the Grafana

```bash
kubectl expose service grafana — type=NodePort — target-port=3000 — name=grafana-ext
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705494671735/4921a16e-c1d0-4bc3-99a6-0945226ae56b.png align="center")

Grafana is exposed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705494920973/10b77c35-4e91-4109-b7c8-ab3fb9e37ade.png align="center")

We can easily access the Grafana login page.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705496165712/f9edf955-426a-4005-9ccb-67339842fd70.png align="center")

Successfully logged into the Grafana dashboard.

Now we will add Prometheus as the data source.

Click on Data Source &gt; choose Prometheus.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705496745822/eece165d-0d08-4746-bc98-56b0a83acec6.png align="center")

In the connection: insert the Prometheus URL.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705496824429/222d7471-b9a5-4543-9f4e-d0f82df562dc.png align="center")

Click on Save and test.

Now click on Building a dashboard or from the homepage we can create the dashboard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705497158258/212fb12f-92fc-4070-96ea-4d7cbc5ff422.png align="center")

Instead of creating the dashboard from the beginning, we can simply import the dashboard that is already pre-built.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705497632051/ea99f494-876d-403d-822f-54547379e1f3.png align="center")

From here we can copy the ID of the dashboard i.e. 13332

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705497698682/b3b2a7fc-05a1-4c4e-8636-1201e5340cbd.png align="center")

Click on load to load the dashboard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705497744710/2316dae9-62f0-4c80-b9e8-0bbc8d3a8e09.png align="center")

Click on Import.

You can import the multiple dashboards.

Using the id:15282  
And the Dashboard can be visualized as below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705497526701/1038ce80-463a-4274-a170-6f52902908bd.png align="center")

Using the dashboard:3662

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705501431302/28080b03-b771-40bf-99d7-81372f43d369.png align="center")

We can also expose the kube-state metrices.

To expose Kube-state metrics

```bash
kubectl expose service prometheus-kube-state-metrics  --type=NodePort --target-port=8080 --name=prometheus-kube-state-metrics-ext
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705502161149/06bcbaa9-ec28-42d8-aea3-d3cc1c8c139f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705502169854/530608f8-015e-45e1-9313-ee8abf0df97c.png align="center")

We can also set this kube-state-metrics endpoint as a job in Kubernetes to know about the kube-state details.

Hence, the detailed steps covered the installation of both monitoring tools (Prometheus and Grafana) using Helm charts. The exposure of services via NodePort for simplicity, and the integration of pre-built dashboards in Grafana to visualize Kubernetes metrics.

It is important to note, however, that while using NodePort for external access is suitable for local or testing environments, it may not be the best practice for production setups. In production, a more secure and scalable approach involves utilizing an Ingress controller or LoadBalancer service type for external access to Prometheus and Grafana. These options provide better control over routing and security, ensuring a more robust and production-ready monitoring solution for Kubernetes clusters.

Happy Learning!!
