Grafana is an open-source dashboard tool that can be used to display metrics of different systems. It can be integrated with a variety of data sources like Prometheus, InfluxDB Stackdriver, etc.
Grafana uses time series data for infrastructure and applications (such as disk I/O utilization, CPU, and memory) that is first loaded into the analysis tool, e.g Prometheus, then analyzed.
This post explains how to setup a Grafana dashboard on Kubernetes using Prometheus as the data analytics tool.
The steps below will guide us on how to configure a working Grafana instance on an existing Kubernetes cluster.
You can follow the posts below if you wish to deploy a kubernetes cluster:
- Install Kubernetes Cluster on Ubuntu using K3s
- Install Kubernetes Cluster on Ubuntu with kubeadm
- How To Install MicroK8s Kubernetes Cluster on CentOS
- Deploy Kubernetes Cluster on CentOS With Ansible and Calico CNI
With your Kubernetes cluster installed and fully functional, proceed to the next configuration steps.
1. Deploy Grafana Service in Kubernetes
Create new project for the deployment:
$ kubectl create namespace monitoring
namespace/monitoring created
Deploy Grafana service into the namespace we just created.
$ kubectl create deployment grafana -n monitoring --image=docker.io/grafana/grafana:latest
deployment.apps/grafana created
This deploys Grafana on the cluster and starts it. After a few seconds, you can check if the image was deployed by using:
kubectl get deployments -n monitoring
The output will be something similar to this:
$ kubectl get deployments -n monitoring
NAME READY UP-TO-DATE AVAILABLE AGE
grafana 1/1 1 1 21s
2. Expose Grafana Service using NodePort
We have to expose grafana service via the NodePort as it is only accessible using ClusterIP currently.
Expose the grafana deployment using NodePort service with the following command:
$ kubectl -n monitoring expose deployment grafana --type="NodePort" --port 3000
service/grafana exposed
This creates a service and exposes port 3000, the default grafana port.
Check if the service has been exposed with the following command:
$ 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 the describe service
command to find which port has been exposed externally.
root@kube-master:~# 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 via that port externally.
3. Access Grafana Web Dashboard
You can now open the Grafana dashboard in the browser using http://<master_IP>:NodePort.
e.g:
http://172.21.200.11:32150
This opens a Grafana login page. The default username and password for Grafana is admin/admin
You will be asked to change the password on your first-time login.
We need to test our Grafana instance to find out if it is working as expected. The simplest method to test that is to use theTestData DB method which provides a sample of the visualized data.
Click ‘Create your first data source’ tab then choose ‘TestData DB’. Click ‘Save and test’ as shown below.
Click ‘Create a new dashboard’ to create a dashboard.
To see a panel with visualized data, click on ‘heat map’ or ‘graph’
4. Using Grafana Dashboards
There are many prebuilt Grafana templates available for various data sources. These templates can be used to create user-friendly dashboards that have the pre-built functionalities which fit your environment.
You can check out the templates from here.
The steps below will guide us on how to monitor our Kubernetes cluster with the Grafana instance we just deployed.
1 Obtain the template ID from grafana public template as shown:
2. Select the Import option from your Grafana dashboard.
3. Enter the dashboard ID obtained from Grafana
4. Click Load button:
5. Click import to import the dashboard. This will redirect to the dashboard with the metrics displayed as in the sample below:
To get more metrics on your kubernetes cluster you need to integrate exporters like Prometheus and Kube State Metrics.
These will be covered in other articles on this site.
Conclusion
Grafana is a very lightweight yet powerful tool when it comes to dashboards. It can be integrated with several monitoring tools and can be very useful for visualization of different workloads in different scenarios. This includes cloud systems, network environments and containers.
More guides: