{"id":864,"date":"2022-01-29T01:00:00","date_gmt":"2022-01-29T01:00:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=864"},"modified":"2022-01-29T01:00:00","modified_gmt":"2022-01-29T01:00:00","slug":"setup-grafana-kubernetes","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=864","title":{"rendered":"How To Setup Grafana On Kubernetes &#8211; Beginners Guide"},"content":{"rendered":"<p>Grafana is an open-source lightweight dashboard tool. It can be integrated with many data sources like Prometheus, <a href=\"https:\/\/devopscube.com\/how-to-setup-and-push-serverapplication-logs-to-aws-cloudwatch\/\" rel=\"noreferrer noopener\">AWS cloud watch<\/a>, Stackdriver, etc. Running Grafana on Kubernetes<\/p>\n<p>In our previous posts, we have looked at the following.<\/p>\n<ol>\n<li><a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">Setup Prometheus on Kubernetes<\/a><\/li>\n<li><a href=\"https:\/\/devopscube.com\/setup-kube-state-metrics\/\" rel=\"noreferrer noopener\">Setup Kube State Metrics<\/a><\/li>\n<li><a href=\"https:\/\/devopscube.com\/alert-manager-kubernetes-guide\/\" rel=\"noreferrer noopener\">Setup alert manager on Kubernetes<\/a><\/li>\n<\/ol>\n<p>This tutorial explains how to run Grafana on <a href=\"https:\/\/devopscube.com\/setup-kubernetes-cluster-kubeadm\/\" rel=\"noreferrer noopener\">Kubernetes cluster<\/a>. Using Grafana you can simplify Kubernetes monitoring dashboards from Prometheus metrics.<\/p>\n<h2 id=\"grafana-kubernetes-manifests\">Grafana Kubernetes Manifests<\/h2>\n<p>All the Kubernetes manifests (YAML files) used in this tutorial are <a href=\"https:\/\/github.com\/bibinwilson\/kubernetes-grafana?ref=devopscube.com\" rel=\"noreferrer noopener\">hosted on Github<\/a> as well. You can clone it and use it for the setup.<\/p>\n<pre><code>git clone https:\/\/github.com\/bibinwilson\/kubernetes-grafana.git<\/code><\/pre>\n<h2 id=\"deploy-grafana-on-kubernetes\">Deploy Grafana On Kubernetes<\/h2>\n<p>Let&#8217;s look at the Grafana setup in detail.<\/p>\n<p><strong>Step 1:<\/strong> Create a file named <code>grafana-datasource-config.yaml<\/code><\/p>\n<pre><code>vi grafana-datasource-config.yaml<\/code><\/pre>\n<p>Copy the following contents.<\/p>\n<blockquote><p><strong>Note:<\/strong> The following data source configuration is for Prometheus. If you have more data sources, you can add more data sources with different YAMLs under the data section.<\/p><\/blockquote>\n<pre><code>apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: grafana-datasources\n  namespace: monitoring\ndata:\n  prometheus.yaml: |-\n    {\n        \"apiVersion\": 1,\n        \"datasources\": [\n            {\n               \"access\":\"proxy\",\n                \"editable\": true,\n                \"name\": \"prometheus\",\n                \"orgId\": 1,\n                \"type\": \"prometheus\",\n                \"url\": \"http:\/\/prometheus-service.monitoring.svc:8080\",\n                \"version\": 1\n            }\n        ]\n    }<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Create the configmap using the following command.<\/p>\n<pre><code>kubectl create -f grafana-datasource-config.yaml<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Create a file named <code>deployment.yaml<\/code><\/p>\n<pre><code>vi deployment.yaml<\/code><\/pre>\n<p>Copy the following contents on the file.<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: grafana\n  namespace: monitoring\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: grafana\n  template:\n    metadata:\n      name: grafana\n      labels:\n        app: grafana\n    spec:\n      containers:\n      - name: grafana\n        image: grafana\/grafana:latest\n        ports:\n        - name: grafana\n          containerPort: 3000\n        resources:\n          limits:\n            memory: \"1Gi\"\n            cpu: \"1000m\"\n          requests: \n            memory: 500M\n            cpu: \"500m\"\n        volumeMounts:\n          - mountPath: \/var\/lib\/grafana\n            name: grafana-storage\n          - mountPath: \/etc\/grafana\/provisioning\/datasources\n            name: grafana-datasources\n            readOnly: false\n      volumes:\n        - name: grafana-storage\n          emptyDir: {}\n        - name: grafana-datasources\n          configMap:\n              defaultMode: 420\n              name: grafana-datasources<\/code><\/pre>\n<blockquote><p><strong>Note:<\/strong> This Grafana deployment <strong>does not use a persistent volume<\/strong>. If you restart the pod all changes will be gone. Use a persistent volume if you are deploying Grafana for your project requirements. It will persist all the configs and data that Grafana uses.<\/p><\/blockquote>\n<p><strong>Step 4:<\/strong> Create the deployment<\/p>\n<pre><code>kubectl create -f deployment.yaml<\/code><\/pre>\n<p><strong>Step 5: <\/strong>Create a service file named <code>service.yaml<\/code><\/p>\n<pre><code>vi service.yaml<\/code><\/pre>\n<p>Copy the following contents. This will expose Grafana on <code>NodePort<\/code> 32000. You can also expose it <a href=\"https:\/\/devopscube.com\/setup-ingress-kubernetes-nginx-controller\/\" rel=\"noreferrer noopener\">using ingress <\/a>or a Loadbalancer based on your requirement.<\/p>\n<pre><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: grafana\n  namespace: monitoring\n  annotations:\n      prometheus.io\/scrape: 'true'\n      prometheus.io\/port:   '3000'\nspec:\n  selector: \n    app: grafana\n  type: NodePort  \n  ports:\n    - port: 3000\n      targetPort: 3000\n      nodePort: 32000<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Create the service.<\/p>\n<pre><code>kubectl create -f service.yaml<\/code><\/pre>\n<p>Now you should be able to access the Grafana dashboard <strong>using any node IP<\/strong> on port <code>32000<\/code>. Make sure the port is allowed in the firewall to be accessed from your workstation.<\/p>\n<pre><code>http:\/\/&lt;your-node-ip&gt;:32000<\/code><\/pre>\n<p>You can also use port forwarding using the following command.<\/p>\n<pre><code>kubectl port-forward -n monitoring &lt;grafana-pod-name&gt; 3000 &amp;<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code>vagrant@dcubelab:~$ kubectl get po -n monitoring\nNAME                       READY   STATUS    RESTARTS   AGE\ngrafana-64c89f57f7-kjqrb   1\/1     Running   0          10m\nvagrant@dcubelab:~$ kubectl port-forward -n monitoring grafana-64c89f57f7-kjqrb 3000 &amp;<\/code><\/pre>\n<p>You will be able to access Grafana a from <code>http:\/\/localhost:3000<\/code><\/p>\n<p>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.<\/p>\n<pre><code>User: admin\nPass: admin<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/grafana-min-1.png\" class=\"kg-image\" alt=\"Grafana dashboard on Kubernetes\" loading=\"lazy\" width=\"1030\" height=\"686\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/grafana-min-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/grafana-min-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/grafana-min-1.png 1030w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"create-kubernetes-dashboards-on-grafana\">Create Kubernetes Dashboards on Grafana<\/h2>\n<p><strong>Creating a Kubernetes dashboard<\/strong> from the Grafana template is pretty easy. There are many prebuilt Grafana templates available for Kubernetes. You can easily have prebuilt dashboards for ingress controllers, volumes, API servers, Prometheus metrics, and much more.<\/p>\n<p>To know more, see <a href=\"https:\/\/grafana.com\/grafana\/dashboards\/?search=kubernetes&#038;ref=devopscube.com\" rel=\"noreferrer noopener\">Grafana templates for Kubernetes monitoring<\/a><\/p>\n<p>Follow the steps given below to set up a Grafana dashboard to monitor kubernetes deployments.<\/p>\n<p><strong>Step 1:<\/strong> Get the template ID from <a href=\"https:\/\/grafana.com\/grafana\/dashboards\/8588?ref=devopscube.com\" rel=\"noreferrer noopener\">grafana public template.<\/a> as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-323.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"576\" height=\"265\"><\/figure>\n<p><strong>Step 2: <\/strong>Head over to the Grafana dashbaord and select the import option.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-1-54.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"587\" height=\"220\"><\/figure>\n<p><strong>Step 3:<\/strong> Enter the dashboard ID you got in step 1<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-2-58.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"593\" height=\"286\"><\/figure>\n<p><strong>Step 4:<\/strong> Grafana will automatically fetch the template from the Grafana website. You can change the values as shown in the image below and click import.<\/p>\n<blockquote><p><strong>Note<\/strong>: If you are behind the corporate firewall and cannot download the template using id, you can download the template JSON and paste the JSON in the text box to import it.<\/p><\/blockquote>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-3-59.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"606\" height=\"278\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-3-59.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-3-59.png 606w\"><\/figure>\n<p>You should see the dashboard immediately.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-4-51.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"1349\" height=\"427\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-4-51.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-4-51.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-4-51.png 1349w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>When Grafana is used with Prometheus, it uses PromQL to query metrics from Prometheus. You can use the same PromQL Prometheus queries to <strong>build custom dashboards on Grafana<\/strong>.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Grafana is a very powerful tool when it comes to Kubernetes monitoring dashboards.<\/p>\n<p>It is used by many organizations to monitor their Kubernetes workloads. With the wide range of pre-built templates, you can get started with the templates pretty quickly. What more can you ask for right?<\/p>\n<p>Let me know how you are using Grafana in your organization.<\/p>\n<p>Also, let me know if you want to add more information to this article.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-grafana-kubernetes\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Setup Grafana On Kubernetes &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-grafana-kubernetes\/<\/p>\n","protected":false},"author":1,"featured_media":865,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=864"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/865"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}