【问题标题】:How to create grafana configmap for datasources?如何为数据源创建 grafana configmap?
【发布时间】:2020-11-11 03:39:35
【问题描述】:

我正在尝试使用 Kube-Prometheus-Stack helm chart https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack 中的 Grafana 实例为 Grafana 数据源创建配置映射

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

【问题讨论】:

    标签: kubernetes prometheus grafana prometheus-operator


    【解决方案1】:

    为了让您通过 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
    

    【讨论】:

    • grafana 大师的另一个大师班。
    • 重要 - 在所有数据源 ConfigMap 中使用唯一的文件名。因此,将datasource.yaml 替换为适合您的情况的独特且具有描述性的内容,例如loki-datasource.yaml
    • 我在 gke 上使用 kube-prometheus-stack,在尝试创建自定义数据源时遇到了类似的问题。我可以知道为什么添加grafana_datasource: 1 会有所不同。
    【解决方案2】:

    我有一个用于 grafana 的 ConfigMap,它带有用于抓取 Flink 任务管理器的 prometheus 数据源。文件 (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 pod 中调用它:

    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

    【讨论】:

    • 感谢您的回答!抱歉,我还是 Grafana 的新手,有没有可以用来通过 CLI 更新 pod 的 yml 文件并将调用插入到 configmap 的命令?
    • 您可以使用相同的命令来创建 Pod kubectl apply -f file.yaml,如果与之前的 file.yaml 有一些不同,它也会更新 Pod。但是,我不确定它是否适用于 configMaps,因为 Deployment pod 是不同的 pod。我想你必须重新启动 grafsna 吊舱。像这样kubectl -n service rollout restart deployment <name>
    猜你喜欢
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2021-09-21
    • 2019-03-29
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多