DEV Community

Cover image for How To provisioning DataSources in Grafana with Kubernetes
Der Sascha
Der Sascha

Posted on • Originally published at blog.bajonczak.com

How To provisioning DataSources in Grafana with Kubernetes

In my last post about Grafana, I described to you how I provisioned a configured dashboard in Grafana. This was very nice to use, but unfortunately, every dashboard requires a datasource to display data. You are able to provision this datasource too in your grafana setup.

What is a DataSource?

The datasource is, yes.... the source for your data. This can be everything you need. There are several datasources available to select so I will concentrate on a single datasource to keep this simple. An application Insights (aka Azure Monitor).

Requirements

You will need at first the necessary data from the azure monitor to use the telemetry data from this service. So according to this, you have two choices. I will use here the simplest one that uses an App registration. Because when you use the service principal you must provide this principal to your service, in Azure it's not a complex problem but when you host it on your own it will get very difficult :).
So first of all, let's assume you registered an Application in your Azure Ad Tenant. Next, you have the following data after this process

  • Tenant-ID (Guid)
  • Client-ID (Guid)

Client Secret
Also, You must fetch the subscription Id, in which the Azure monitor is running.

The DataSource Configuration

Before deep diving into the configuration, we will define the data source in our yaml. This is a no-brainer just use this

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-grafana-datasources
data:
  ds.yaml: |-
    {
        "apiVersion": 1,
        "datasources": [
            {
               "access":"proxy",
               "uid":"azure",
                "editable": true,
                "name": "Azure Monitor",
                "orgId": 1,
                "type": "grafana-azure-monitor-datasource",
                "jsonData":{
                  "azureAuthType": "clientsecret",
                  "cloudName":"azuremonitor",
                  "tenantId": "your tenant id",
                  "clientId": "your client id",
                  "subscriptionId": " your subscription id"
                },
                "secureJsonData":
                  {
                    "clientSecret":"your client secret"
                  },
                "version": 1
            },
            {
              "uid":"loki",
              "name": "Loki",
              "type": "loki",
              "url": "http://loki.loki:3100"
            }
        ]
    }
   {{ end }}
Enter fullscreen mode Exit fullscreen mode

You see that you must insert your gathered data into this JSON. Please pay attention to the uid Property. This value, which you will later use in your dashboard as a data source reference, I will show you later an example for this.
So you see also that this data source definition will be stored as configmap-"file" (called ds.yml) in Kubernetes. So after applying it with kubectl -f {yourfile.yml} it will be available within your Kubernetes.

Let Grafana know about your datasources

Provisioning the file itself is not the only task, you must put it in a specific location so that Garfana recognizes this datasource definition. For this, you can use the volume mount points that I described above. As you saw in my post about provisioning the dashboard you will recognize that I provisioning the datasources in a separate mount point. Let's look at a snippet from the complete example:


........
     volumeMounts:
            .......
          - mountPath: /etc/grafana/provisioning/datasources
            name: grafana-datasources
            readOnly: false
          - mountPath: /etc/grafana/provisioning/dashboards
            name: grafana-dashboard-definition
        ........
      volumes:
            ......

        - name: grafana-datasources
          configMap:
              defaultMode: 420
              name: my-grafana-datasources
        - name: grafana-dashboard
          configMap:
              defaultMode: 420
              name: {{ .Release.Name }}-grafana-dashboard
.....
Enter fullscreen mode Exit fullscreen mode

Here you see that I will mount two volumes one for the dashboard definitions and one for the datasources. I will focus on the datasource now. The volume grafana-datasources will reference our datarouce configmap my-grafana-datasources declared above.
Next, we tell Kubernetes, at which location the file will be mounted, for this, you will use the volumeMounts entry that will reference the grafana-datasources volume and mount it at the location /etc/grafana/provisioning/datasources.
So when you start the grafana instance now you will notice in your datasource administration the Azure Monitor data source you are created

How to use your data source in your Dashboard

Remember the uid for your dashboard defined above? Now it's time to use it. Because the uid will define a unique id for your datasource. This uid you now can use in your dashboard definition for each panel. Just search in your definition for datasource, when you already exported a datasource that uses azure monitor data, then you can search for grafana-azure-monitor-datasource. Next, you will see the uid property, in this, you will set the uid from your datasource definition. This can then look like this:

That's all. Now you have not only a provisioned dashboard, you also have to tighten it to a functional datasource too.

Conclusion

In this article you saw, how simple it is to provision a datasource. So I think this will get more and more involved in the DevOps process that we (and of course you) will use. I hope that this article helps you to understand how Grafana can be used in the Kubernetes world and how you can design fancy dashboards without manually configuring the datasource later.

Top comments (0)