4
votes

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.

2

2 Answers

6
votes

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
3
votes

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