How to import custom dashboards to grafana using helm
Asked Answered
A

5

17

I'm trying to understand helm and I wonder if someone could ELI5 to me something or help me with something.

So i did run below:

helm repo add coreos https://s3-eu-west-1.amazonaws.com/coreos-charts/stable/

Then I installed kube-prometheus by using below:

helm install coreos/kube-prometheus --name kube-prometheus -f values.yaml --namespace monitoringtest

Everything works fine but I'm trying to add some custom dashboards from json files and I'm struggling to understand how to do it.

I was following this: https://blogcodevalue.wordpress.com/2018/09/16/automate-grafana-dashboard-import-process/

In my values.yaml I added below

serverDashboardConfigmaps:
  - example-dashboards

I understand that if I do:

helm upgrade --install kube-prometheus -f values.yaml --namespace monitoringtest coreos/kube-prometheus

That should cause grafana to pickup a below configmap called example-dashboards and load *.json files from custom-dashboards folder.

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-dashboards
data:
{{ (.Files.Glob "custom-dashboards/*.json").AsConfig | indent 2 }}

# Or
# 
# data:
#   custom-dashboard.json: |-
# {{ (.Files.Get "custom.json") | indent 4 }}
#
# The filename (and consequently the key under data) must be in the format `xxx-dashboard.json` or `xxx-datasource.json`
# for them to be picked up.

Now two questions:

How do I add above configmap to this helm release?

Where is this custom-dashboards folder located? Is it on my laptop and then is send to grafana?

Do I need to copy all of https://s3-eu-west-1.amazonaws.com/coreos-charts/stable/ onto my laptop?

Sorry for explaining everything but I'm just trying to understand this.

Azote answered 24/4, 2019 at 13:9 Comment(5)
I also faced that once upon a time. They use grafana-watcher sidecar which uploads dashboards and datasources into Grafana on startup. Actually, the way it does it is very prone to errors and i found it impossible to use on daily basis. I don't recommend you to go that route. What I do is use Grafana Provisioning: grafana.com/docs/administration/provisioning. So I simply mount dashboards into Grafana to /etc/grafana/provisioning/dashboards/ directory. One downside of it is that you cannot anymore save dashboards in Grafana UI - only export as JSON and upload to Git.Irrational
Thanks but i don't think I can do that using helm?Azote
Why not, you can modify Helm chart to do it. I also did it, but there were many modifications to original Helm chart. May be I can share some days later...Irrational
I think this is for now beyond my helm skills.Azote
is it possible to link dashboards as URL? This example seems to show it: github.com/grafana/helm-charts/blob/main/charts/grafana/…Tadich
H
6

In the latest version of kube-prometheus-stack chart in 2021, According to this answer on github, You should just create a configmap with dashboard data and right labels and it will be checked by sidecar in grafana pod.

Example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-dashboards-custom-1
  namespace: monitoring
  labels:
     grafana_dashboard: "1"
     prometheus: my-value
     release: prometheus

data:
  app-status.json: |-
    {
    "annotations": {
        "list": [
        {

prometheus: my-value comes from this helm chart value:

prometheus:
  prometheusSpec:
    serviceMonitorSelector:
      matchLabels:
        prometheus: my-value
Hickey answered 24/11, 2021 at 12:12 Comment(0)
F
6

You can find a good example of how to do this in the charts for prometheus-operator here:

https://github.com/helm/charts/tree/master/stable/prometheus-operator/templates/grafana

It is a ConfigMapList that gets all JSONs from a given directory and stores them into ConfigMaps which are read by Grafana.

{{- $files := .Files.Glob "dashboards/*.json" }}
{{- if $files }}
apiVersion: v1
kind: ConfigMapList
items:
{{- range $path, $fileContents := $files }}
{{- $dashboardName := regexReplaceAll "(^.*/)(.*)\\.json$" $path "${2}" }}
- apiVersion: v1
  kind: ConfigMap
  metadata:
    name: {{ printf "%s-%s" (include "prometheus-operator.fullname" $) $dashboardName | trunc 63 | trimSuffix "-" }}
    namespace: {{ template "prometheus-operator.namespace" . }}
    labels:
      {{- if $.Values.grafana.sidecar.dashboards.label }}
      {{ $.Values.grafana.sidecar.dashboards.label }}: "1"
      {{- end }}
      app: {{ template "prometheus-operator.name" $ }}-grafana
{{ include "prometheus-operator.labels" $ | indent 6 }}
  data:
    {{ $dashboardName }}.json: {{ $.Files.Get $path | toJson }}
{{- end }}
{{- end }}

Mind that the size of a ConfigMap might be limited: https://mcmap.net/q/491858/-kubernetes-configmap-size-limitation

Feinleib answered 3/7, 2020 at 13:53 Comment(0)
H
6

In the latest version of kube-prometheus-stack chart in 2021, According to this answer on github, You should just create a configmap with dashboard data and right labels and it will be checked by sidecar in grafana pod.

Example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-dashboards-custom-1
  namespace: monitoring
  labels:
     grafana_dashboard: "1"
     prometheus: my-value
     release: prometheus

data:
  app-status.json: |-
    {
    "annotations": {
        "list": [
        {

prometheus: my-value comes from this helm chart value:

prometheus:
  prometheusSpec:
    serviceMonitorSelector:
      matchLabels:
        prometheus: my-value
Hickey answered 24/11, 2021 at 12:12 Comment(0)
A
5

I partially figured it out. I can load dashboards from a configmap. Not from separate json files yet but it'a progress.

For anyone interested I put this on my github page: https://github.com/tretos53/notes/blob/master/Grafana/Readme.MD

Azote answered 25/4, 2019 at 15:3 Comment(1)
For people from the future: apiVersion: v1 kind: ConfigMap metadata: name: custom-dashboards data: rpi-dashboard.json: |- {{ .Files.Get "dashboards/rpi-dashboard.json" | indent 4}} with the path being relative to your helm chartMatos
S
2

The current way (2023) is easy. The helm chart comes from artifacthub.io.

First put the dashboard json in a subdirectory. Here it's in dashboards/node-exporter.json for the standard node-exporter dash.

Then uncomment dashboardprovider in values.yaml

dashboardProviders:
  dashboardproviders.yaml:
    apiVersion: 1
    providers:
    - name: 'default'
      orgId: 1
      folder: ''
      type: file
      disableDeletion: false
      editable: true
      options:
        path: /var/lib/grafana/dashboards/default

Then add the file to "dashboards: default:"

dashboards:
  default:
    node-exporter-dashboard:
      file: dashboards/node-exporter.json

Then helm install or helm upgrade and grafana-dashboards-default ConfigMap pops up which can be selected in the interface. The configmap contains the full json in node-exporter-dashboard.json.

info found here: https://community.grafana.com/t/how-to-upload-dashboards-as-json-files-to-kubernetes-via-helm/59731

Sopher answered 28/7, 2023 at 11:50 Comment(2)
Is there a link to docs for how to do this?Reamonn
think i found it here. community.grafana.com/t/…Sopher
L
0

There are different ways to do so. It also depends on the Grafana/Prometheus Stack Chart version. The info is here: https://github.com/grafana/helm-charts/tree/main/charts/grafana#import-dashboards (Grafana chart can override kube-prometheus-stack values since the latter is dependent on Grafana)

The one that definitely worked for me is Sidecar for dashboards. If you want to import dashboards for all Grafana organizations as well, you need to use dashboardProviders: {} Some details: https://grafana.com/tutorials/provision-dashboards-and-data-sources/

Laurellaurella answered 20/4, 2022 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.