如何为数据源创建grafana configmap?

时间:2020-11-11 03:39:35

标签: kubernetes prometheus grafana prometheus-operator

我正在尝试使用来自Kube-Prometheus-Stack掌舵图https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack

的Grafana实例为Grafana数据源创建配置图。

我知道仪表板,您可以使用以下答案中列出的命令从json文件创建configmap: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

是否可以对grafana数据源执行类似的操作?我目前有一个datasource.yaml,其中包含以下几行:

    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

但是,即使它创建了configmap,我也无法使用它导入数据源。

2 个答案:

答案 0 :(得分:4)

要通过 grafana 服务器组件加载数据,您需要在元数据字段 grafana_datasource: "1" 中进行设置。

对于配置映射:

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

对于具有相同标签的秘密

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

答案 1 :(得分:1)

我有一个ConfigMap for grafana,带有普罗米修斯数据源,该数据源会刮擦Flink任务管理器。文件(https://github.com/felipegutierrez/explore-flink/blob/master/k8s/grafana-configuration-configmap.yaml)太大,无法在此处粘贴,但下面是主要部分。

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: |+
    {...}

设置好ConfigMap后,您可以像这样在grafana窗格中调用它:

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/

完整的示例工作在这里:https://github.com/felipegutierrez/explore-flink/blob/master/k8s/grafana-deployment.yaml