Import Dashboard JSON to Grafana from a ConfigMap in Kubernetes

Steps to import the JSON of a Grafana dashboard:

  1. Create a ConfigMap including the dashboard JSON
  2. Create a new Grafana dashboardProvider in the Grafana helmRelease values
  3. Map the new dashboardProvider to the new ConfigMap in the Grafana helmRelease values

ConfigMap definition

Create a new ConfigMap. The name of the ConfigMap should match the JSON filename; in the example below, this is graph-services-response-times. The ConfigMap must be in the same namespace as the Grafana helmRelease. The ConfigMap should have the label grafana_dashboard, the label’s value should be "1", and the data of the ConfigMap should define a JSON multi-line string. The value of the multi-line string should be the JSON exported from a Grafana dashboard.

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    grafana_dashboard: "1"
  name: graph-services-response-times
  namespace: monitoring
data:
  graph-services-response-times.json: |-
    {
      "annotations": {
        "list": [
          {
            "builtIn": 1,
            "datasource": {
              "type": "datasource",
              "uid": "grafana"
            },
            ...

You can also include multiple dashboard JSON files in the ConfigMap by adding additional key-value pairs. For example:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    grafana_dashboard: "1"
  name: graph-services-response-times
  namespace: monitoring
data:
  graph-services-response-times.json: |-
    # Dashboard 1 JSON goes here
  graph-services-response-times-2.json: |-
    # Dashboard 2 JSON goes here

New dashboardProvider in the Grafana helmRelease values

Every dashboard imported from a ConfigMap must have an individual dashboardProvider defined for it in the Grafana helmRelease values.

Add a new dashboardProvider in the Grafana helmRelease values under dashboardProviders.dashboardproviders.yaml. The dashboardProvider name must be unique, and must also be appended to the options.path value /var/lib/grafana/dashboards/. The folder in which the dashboard will be available in Grafana should also be defined. This folder does not need to already exist in Grafana, but the value should not be empty; otherwise, the dashboard is saved in the default folder, which is only available to be viewed by Grafana users with the admin role.

 - name: dp1
   orgId: 1
   folder: 'graph-services'
   type: file
   disableDeletion: false
   editable: true
   options:
     path: /var/lib/grafana/dashboards/dp1

Mapping the new dashboardProvider to the new ConfigMap

The new dashboardProvider should now be mapped to the new ConfigMap containing the dashboard JSON definition. In the Grafana helmRelease under dashboardsConfigMaps, add a new key-value pair, with the new dashboardProvider’s name as the key and the new ConfigMap’s name as the value.

dp1: "graph-services-response-times"

Apply the ConfigMap, install the Grafana helmRelease, and your newly imported dashboard should be available in Grafana!