How to create grafana configmap for datasources?
Asked Answered
T

2

14

I am trying to create a configmap for a Grafana datasource, using an instance of Grafana from the Kube-Prometheus-Stack helm chart https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack

I am aware for dashboards, you can create a configmap from a json file using the commands listed in this answer: stable/prometheus-operator - adding persistent grafana dashboards

wget https://raw.githubusercontent.com/percona/grafana-dashboards/master/dashboards/MongoDB_Overview.json
kubectl -n monitoring create cm grafana-mongodb-overview --from-file=MongoDB_Overview.json
kubectl -n monitoring label cm grafana-mongodb-overview grafana_dashboard=mongodb-overview

Can something similar be done for grafana datasources? I currently have a datasource.yaml which contains the following lines:

    data:
      datasource-PRF1-Prometheus.yaml: |-
        apiVersion: 1
        datasources:
          - name: Test-Prometheus
            type: prometheus
            url: https://prometheus.url.net/
            access: Server
            isDefault: true
            basicAuth: true
            basicAuthPassword: password
            basicAuthUser: admin

However, I am not able to import a datasource using it, even though it creates a configmap.

Tresa answered 11/11, 2020 at 3:39 Comment(0)
E
15

For you to load the data by grafana server component, you would need to set this in your metadata field grafana_datasource: "1".

For a configmap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-grafana-datasource
  labels:
     grafana_datasource: "1"
     namespace: monitoring
data:
  datasource.yaml: |-
    apiVersion: 1
    datasources:
    - access: proxy
      basicAuth: false
      editable: false
      isDefault: false
      jsonData:
        authType: credentials
        defaultRegion: us-west-2
      name: CloudWatch
      type: cloudwatch

For a secret with the same label

apiVersion: v1
kind: Secret
metadata:
  name: influx-grafana-datasource
  labels:
     grafana_datasource: "1"
     namespace: monitoring
type: Opaque
stringData:
  influxdatasource.yaml: |-
    # config file version
    apiVersion: 1
    datasources:
      - name: influxdb
        type: influxdb
        access: proxy
        database: metrics_db
        user: metrics_read_user
        url: http://influx.example.com:8086/
        jsonData:
          timeInterval: "15s"
        secureJsonData:
          password: yourinfluxpassword
Ethbinium answered 4/2, 2021 at 5:1 Comment(3)
Important - using unique file names across all datasource ConfigMaps. So replace datasource.yaml by something unique and descriptive for your case, I have for example loki-datasource.yamlSheriff
i'm using kube-prometheus-stack on gke, running similar issue when trying to create a custom data source. May I know why would adding grafana_datasource: 1 make a difference.Argeliaargent
The password here isn't very secure... Should maybe use env var grafana.com/docs/grafana/latest/administration/provisioning/…Abshire
P
5

I have a ConfigMap for grafana withe prometheus datasource that scrapes Flink Task Managers. The file (https://github.com/felipegutierrez/explore-flink/blob/master/k8s/grafana-configuration-configmap.yaml) is too big to paste here but the main sections are below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-config
  namespace: kafka
  labels:
    app: flink
data:
  grafana.ini: |+ ...
  dashboards.yml: |+
    apiVersion: 1
 database
    deleteDatasources:
      - name: Prometheus
        orgId: 1
    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        orgId: 1
        url: http://prometheus-service:9090
        password:
        user:
        database:
        basicAuth: false
        basicAuthUser:
        basicAuthPassword:
        withCredentials:
        isDefault: true
        jsonData:
          graphiteVersion: "1.1"
          tlsAuth: false
          tlsAuthWithCACert: false
        secureJsonData:
          tlsCACert: "..."
          tlsClientCert: "..."
          tlsClientKey: "..."
        version: 1
        editable: true
  dashboard.json: |+
    {...}

After having the ConfigMap set you call it inside the grafana pod like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-deployment
  namespace: kafka
spec:
  replicas: 1
  selector:
    matchLabels:
      app: flink
      component: grafana
  template:
    metadata:
      labels:
        app: flink
        component: grafana
    spec:
      volumes:
      - name: grafana-config-volume
        configMap:
          name: grafana-config
          items:
          - key: grafana.ini
            path: grafana.ini
          - key: datasource.yml
            path: provisioning/datasources/datasource.yml
          - key: dashboards.yml
            path: provisioning/dashboards/dashboards.yml
          - key: dashboard.json
            path: dashboard.json
      containers:
      - name: grafana
        image: grafana/grafana
        imagePullPolicy: IfNotPresent # Always
        ports:
        - containerPort: 3000
          name: http
        volumeMounts:
          - name: grafana-config-volume
            mountPath: /etc/grafana/

The full example working is here: https://github.com/felipegutierrez/explore-flink/blob/master/k8s/grafana-deployment.yaml

Parakeet answered 11/11, 2020 at 7:15 Comment(2)
Thanks for your answer! Sorry, I'm still new to Grafana, is there a command I can use to update the pod's yml file via CLI and insert the call to the configmap?Tresa
You can use the same command to create pods kubectl apply -f file.yaml that it also updates the pod if there is some difference on the previous file.yaml. However, i am not sure if it will work for configMaps because the Deployment pod is a different pod. I guess you have to restart the grafsna pod. Something like this kubectl -n service rollout restart deployment <name>Parakeet

© 2022 - 2024 — McMap. All rights reserved.