How To Setup Grafana On Kubernetes
- Last Updated On: November 4, 2019
- By: Bibin Wilson
Grafana is an open-source lightweight dashboard tool. It can be integrated with many data sources like Prometheus, AWS cloud watch, Stackdriver, etc.
In our previous posts, we have seen the following.
This tutorial explains the Grafana setup on a Kubernetes cluster. You can create dashboards on Grafana for all the Kubernetes metrics through prometheus.
Lets get started with the setup.
Step 1: Create file named grafana-datasource-config.yaml
vi grafana-datasource-config.yaml
Copy the following contents.
Note: The following datasource configuration is for prometheus. If you have more data sources, you can add more data sources with different YAMLs under data section.
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-datasources
namespace: monitoring
data:
prometheus.yaml: |-
{
"apiVersion": 1,
"datasources": [
{
"access":"proxy",
"editable": true,
"name": "prometheus",
"orgId": 1,
"type": "prometheus",
"url": "http://prometheus-service.monitoring.svc:8080",
"version": 1
}
]
}
Step 2: Create the configmap using the following command.
kubectl create -f grafana-datasource-config.yaml
Step 3: Create a file named deployment.yaml
vi deployment.yaml
Copy the following contents
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
name: grafana
labels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana:latest
ports:
- name: grafana
containerPort: 3000
resources:
limits:
memory: "2Gi"
cpu: "1000m"
requests:
memory: "1Gi"
cpu: "500m"
volumeMounts:
- mountPath: /var/lib/grafana
name: grafana-storage
- mountPath: /etc/grafana/provisioning/datasources
name: grafana-datasources
readOnly: false
volumes:
- name: grafana-storage
emptyDir: {}
- name: grafana-datasources
configMap:
defaultMode: 420
name: grafana-datasources
Step 4: Create the deployment
kubectl create -f deployment.yaml
Step 5: Create a service file named service.yaml
vi service.yaml
Copy the following contents. This will expose Grafana on NodePort
32000. You can also expose it using ingress or a Loadbalancer based on your requirement.
apiVersion: v1
kind: Service
metadata:
name: grafana
namespace: monitoring
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '3000'
spec:
selector:
app: grafana
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 32000
Step 6: Create the service.
kubectl create -f service.yaml
Now you should be able to access the Grafana dashboard using any node IP on port 32000. Use the following default username and password to log in. Once you log in with default credentials, it will prompt you to change the default password.
User: admin
Pass: admin
Setup Kubernetes Dashbaords
There are many prebuilt Grafana templates available for various data sources. You can check out the templates from here.
Setting up a dashboard from a template is pretty easy. Follow the steps given below to setup a dashboard to monitor kubernetes deployments.
Step 1: Get the template ID from grafana public template. as shown below.
Step 2: Head over to grafana and select the import option.
Step 3: Enter the dashboard ID you got it step 1
Step 4: Grafana will automatically fetch the template from Grafana website. You can change the values as shown in the image below and click import.
You should see the dashboard immediately.
Conclusion
Grafana is a very powerful tool when it comes to dashboards. It is used by many organisations to monitor its workloads. Let us know how you are using Grafana in your organisation. Also let us know if you want to add more information to this article.
Bibin Wilson
Other Interesting Blogs
How to Setup and Configure Docker Swarm Cluster
In this tutorial, you will learn how to set up a four node docker swarm cluster. Prerequisites Minimum two nodes with Docker
Setting Up Azure CLI on Ubuntu Linux
Azure has a great web interface called azure portal for performing all the functions. But if you prefer command line tools over
Docker Networking Tutorial : Connect Containers Across Hosts Using Weave
Weave connects containers across hosts by creating a virtual network. It also has a DNS service which helps in automatic discovery. In
Comments
Hi,
The above setup works for me. But i am trying to keep dashboards in one specific folder in my cluster and then i want to read it from grafana pod.
This is config map i have created:-
And in deployment file I am adding following config:
and
But i am getting following error:
Did you find the solution to this. Facing same issue
Could you possibly correct the following typo? I looked at the configs several times and asked what I was doing wrong, but I hadn’t read enough.
You wrote “Now you should be able to access the Grafana dashboard using any node IP on port 3200.”
It should be “Now you should be able to access the Grafana dashboard using any node IP on port 32000.”. A zero is missing.
Thanks, Michael for letting us know. We have updated the content with the correct port.
Hi, I’m not getting the error “Templating init failed Bad gateway”
Press F12 to see if you get lots of errors, if so, your datasource url in prometheus.yaml is wrong. Change “http://prometheus-service.monitoring.svc:8080” to “http://prometheus-service:8080”, it works for me, hope to help you 🙂
Correct Cheng. http://prometheus-service.monitoring.svc:8080 is added assuming that Prometheus is running in the monitoring namespace.
HI, how do you get the node ip to get the grafana dashboard? Thanks!!
Hi,
how do i get the node ip to view the dashboard? Thanks!!
you can use any one of the nodes IP in the cluster.
Hi, I want to install Grafana with MySQL database rather than default SQLite? Do you know how to do this? I tried a lot but no success.
Update: Yeah! First, we have to create centralized MySQL DB and then I passed env variables like this.
Never done it before! I will consider testing it out!
Please let us know if you are able to setup it up before we do!
Helpful post! Thanks
Statefulset grafana will be helpful as well
Hi , can you please provide what would be a proper datasource-config file for mysql on google cloud sql ?
Could you please provide article to setup ELK on Kubernetes same like this document.
Thanks for the nice article , does the volume is persistence?