Install Grafana on Kubernetes for cluster monitoring

You can download this article in PDF format via the link below to support us.
Download the guide in PDF formatturn off

Grafana is an open source dashboard tool that can be used to display metrics for different systems. It can be integrated with various data sources such as Prometheus, InfluxDB Stackdriver, etc.

Grafana uses time series data of infrastructure and applications (such as disk I/O utilization, CPU and memory). These time series data are first loaded into analysis tools such as Prometheus and then analyzed.

This article explains how to set up a Grafana dashboard on Kubernetes using Prometheus as a data analysis tool.

The following steps will guide us how to configure an available Grafana instance on an existing Kubernetes cluster.

If you want to deploy a kubernetes cluster, you can follow the following post:

  • Use K3s to install a Kubernetes cluster on Ubuntu
  • Use kubeadm to install a Kubernetes cluster on Ubuntu
  • How to install MicroK8s Kubernetes cluster on CentOS
  • Use Ansible and Calico CNI to deploy a Kubernetes cluster on CentOS

After the Kubernetes cluster is installed and running normally, please proceed to the following configuration steps.

1: Deploy Grafana service in Kubernetes

Create a new project for deployment:

$ kubectl create namespace monitoring
namespace/monitoring created

Deploy the Grafana service to the namespace we just created.

$ kubectl create deployment grafana -n monitoring --image=docker.io/grafana/grafana:latest
deployment.apps/grafana created

This will deploy Grafana on the cluster and start it. After a few seconds, you can check whether the image has been deployed using the following methods:

$ kubectl get deployments -n monitoring

The output will be similar to the following:

$ kubectl get deployments -n monitoring
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
grafana   1/1     1            1           21s

2. Use NodePort to expose Grafana services

We must expose the grafana service through NodePort, because currently it can only be accessed using ClusterIP. Use the following command to expose the grafana deployment using the NodePort service:

$ kubectl -n monitoring expose deployment grafana --type="NodePort" --port 3000
service/grafana exposed

This will create the service and expose port 3000 (the default grafana port).

Use the following command to check if the service is exposed:

$ kubectl get service -n monitoring
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
grafana      NodePort    10.96.174.231   <none>        3000:32150/TCP   9s

We use describe service Command to find which port has been exposed externally.

[email protected]:~# kubectl describe service grafana
Name:                     grafana
Namespace:                default
Labels:                   app=grafana
Annotations:              <none>
Selector:                 app=grafana
Type:                     NodePort
IP:                       10.152.183.50
Port:                     <unset>  3000/TCP
TargetPort:               3000/TCP
NodePort:                 <unset>  32150/TCP
Endpoints:                10.1.196.7:3000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Grafana is now exposed to port 32150 and can be accessed from the outside through this port.

3. Access the Grafana web dashboard

Now you can open the Grafana dashboard in your browser using the following method http://<master_IP>:NodePort. E.g:

http://172.21.200.11:32150

This will open a Grafana login page. The default username and password of Grafana is admin/admin

When you log in for the first time, you will be asked to change your password.Install Grafana on Kubernetes for cluster monitoring

We need to test the Grafana instance to see if it works as expected. The simplest test method is to use the TestData DB method, which provides samples of visual data.Install Grafana on Kubernetes for cluster monitoring

Click on the “Create First Data Source” tab and select “TestData DB”. Click “Save and Test” as shown below.Install Grafana on Kubernetes for cluster monitoring

Click “Create New Dashboard” to create a dashboard.Install Grafana on Kubernetes for cluster monitoring

To view a panel with visualized data, click on “Heat Map” or “Graph”Install Grafana on Kubernetes for cluster monitoring

There are many pre-built Grafana templates available for various data sources. These templates can be used to create user-friendly dashboards that have pre-built features suitable for your environment.

you can View template from here.

The following steps will guide us how to monitor the Kubernetes cluster using the Grafana instance we just deployed.

1 Get the template ID from grafana public template as the picture shows:Install Grafana on Kubernetes for cluster monitoring2. Select the “Import” option from the Grafana dashboard.Install Grafana on Kubernetes for cluster monitoring3. Enter the dashboard ID obtained from GrafanaInstall Grafana on Kubernetes for cluster monitoring4. Click the load button:Install Grafana on Kubernetes for cluster monitoring5. Click Import to import the dashboard. This will redirect you to a dashboard showing the following metrics:Install Grafana on Kubernetes for cluster monitoring

In order to get more metrics on your kubernetes cluster, you need to integrate exporters such as Prometheus and Kube State Metrics.

These will be introduced in other articles on this website.

in conclusion

For dashboards, Grafana is a very lightweight but powerful tool. It can be integrated with multiple monitoring tools and is very useful for visualizing different workloads in different scenarios. This includes cloud systems, network environments and containers.

More guidelines:

Deploy Prometheus on the EKS Kubernetes cluster

Install Kubernetes Metrics Server on Amazon EKS cluster

Use VMware Octant to visualize and monitor Kubernetes workloads

Installation lens-best Kubernetes dashboard and IDE

You can download this article in PDF format via the link below to support us.
Download the guide in PDF formatturn off

Sidebar