【问题标题】:How to import custom dashboards to grafana using helm如何使用 helm 将自定义仪表板导入 grafana
【发布时间】:2019-04-24 13:09:56
【问题描述】:

我正在尝试理解 helm,我想知道是否有人可以 ELI5 给我一些东西或帮助我做一些事情。

所以我确实在下面运行:

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

然后我通过以下方式安装了 kube-prometheus:

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

一切正常,但我正在尝试从 json 文件添加一些自定义仪表板,但我很难理解如何做到这一点。

我在关注这个:https://blogcodevalue.wordpress.com/2018/09/16/automate-grafana-dashboard-import-process/

在我的 values.yaml 中我在下面添加了

serverDashboardConfigmaps:
  - example-dashboards

我明白,如果我这样做:

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

这应该会导致 grafana 拾取名为 example-dashboards 的以下配置映射并从 custom-dashboards 文件夹加载 *.json 文件。

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.

现在有两个问题:

如何将上述配置映射添加到此 helm 版本?

custom-dashboards 文件夹在哪里?它是在我的笔记本电脑上然后发送到 grafana 吗?

我需要将所有https://s3-eu-west-1.amazonaws.com/coreos-charts/stable/ 复制到我的笔记本电脑上吗?

很抱歉解释了一切,但我只是想理解这一点。

【问题讨论】:

  • 我曾经也遇到过这种情况。他们使用 grafana-watcher sidecar 在启动时将仪表板和数据源上传到 Grafana。实际上,它的方式很容易出错,我发现它不可能每天使用。我不建议你走那条路。我所做的是使用 Grafana Provisioning:grafana.com/docs/administration/provisioning。所以我只是将仪表板安装到 Grafana 到 /etc/grafana/provisioning/dashboards/ 目录。它的一个缺点是您不能再在 Grafana UI 中保存仪表板 - 只能导出为 JSON 并上传到 Git。
  • 谢谢,但我不认为我可以使用 helm 做到这一点?
  • 为什么不呢,你可以修改 Helm chart 来做。我也这样做了,但是对原始 Helm 图表进行了许多修改。也许我可以在几天后分享...
  • 我认为这暂时超出了我的掌舵技能。
  • 是否可以将仪表板链接为 URL?这个例子似乎表明了它:github.com/grafana/helm-charts/blob/main/charts/grafana/…

标签: kubernetes grafana kubernetes-helm configmap


【解决方案1】:

我部分想通了。我可以从 configmap 加载仪表板。还不是来自单独的 json 文件,但它是一个进步。

对于任何感兴趣的人,我将其放在我的 github 页面上:https://github.com/tretos53/notes/blob/master/Grafana/Readme.MD

【讨论】:

  • 来自未来的人:apiVersion: v1 kind: ConfigMap metadata: name: custom-dashboards data: rpi-dashboard.json: |- {{ .Files.Get "dashboards/rpi-dashboard.json" | indent 4}} 路径与您的掌舵图相关
【解决方案2】:

您可以在此处的 prometheus-operator 图表中找到一个很好的示例:

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

它是一个 ConfigMapList,它从给定目录获取所有 JSON,并将它们存储到 Grafana 读取的 ConfigMaps 中。

{{- $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 }}

请注意,ConfigMap 的大小可能会受到限制: https://stackoverflow.com/a/53015758/4252480

【讨论】:

    【解决方案3】:

    在 2021 年最新版本的kube-prometheus-stack 图表中,根据this answer on github,您应该只需创建一个带有仪表板数据和正确标签的 configmap,它将由 grafana pod 中的 sidecar 进行检查。

    例子:

    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 来自这个舵图值:

    prometheus:
      prometheusSpec:
        serviceMonitorSelector:
          matchLabels:
            prometheus: my-value
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2019-12-31
      • 2018-11-23
      • 2019-11-14
      • 2019-11-13
      • 2021-04-03
      • 2021-06-13
      • 2020-07-14
      • 2020-03-28
      相关资源
      最近更新 更多