【发布时间】:2021-01-23 12:32:37
【问题描述】:
以 infrastructure-as-code 的方式,我想自动配置 Grafana 实例,该实例预加载了来自 public Grafana Dashboards directory 的某些仪表板。
我知道使用 .json 格式的仪表板预加载 Grafana 的方法,但不是通过从上述门户标识所需仪表板的唯一仪表板 ID 引用。
【问题讨论】:
标签: grafana
以 infrastructure-as-code 的方式,我想自动配置 Grafana 实例,该实例预加载了来自 public Grafana Dashboards directory 的某些仪表板。
我知道使用 .json 格式的仪表板预加载 Grafana 的方法,但不是通过从上述门户标识所需仪表板的唯一仪表板 ID 引用。
【问题讨论】:
标签: grafana
我们以 Dashboard ID 11568 为例:
第 1 步:下载 .json
curl https://grafana.com/api/dashboards/11568/revisions/1/download -o ~/Desktop/dashboard.json
第 2 步:将 .json 嵌入到配置映射中并为其添加标签,以便 grafana 仪表板边车容器自动加载它。
kubectl create configmap mydashboard --from-file=$HOME/Desktop/dashboard.json -n=monitoring --dry-run=client -o yaml | kubectl label -f - --dry-run=client -o yaml --local grafana_dashboard=1 > ~/Desktop/dashboard.yaml
第 3 步:将 configmap 包装在 gitops Custom Resources Flux v2 CD 中作为示例
cp ~/Desktop/dashboard.yaml ~/Desktop/gitrepo/base/dashboard.yaml
temp="""
resources:
- dashboard.yaml
"""
echo "$temp" | tee ~/Desktop/gitrepo/base/kustomization.yaml
temp="""
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
name: gitrepo
spec:
gitImplementation: go-git
ref:
branch: master
url: https://somegitrepo.com/somepath
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: gitops-deployment
spec:
interval: 1m
sourceRef:
kind: GitRepository
name: gitrepo
path: ./base
"""
echo "$temp" | tee -a ~/Desktop/gitrepo/deploy.yaml
上面的伪代码意味着如果你这样做了
kubectl apply -f ~/Desktop/gitrepo/deploy.yaml
flux gitrepository CR 将使用引用它的 kustomization CR 创建,FluxCD 的 Kustomization 控制器将执行以下操作:
cd ~/Desktop/gitrepo/base && kustomize build 。 && kubectl apply -f -
所以它基本上会做一个基于 gitops kustomization 的 configmap 部署,其中嵌入了一个 grafana dashboard.json,并且位于具有正确标签的正确命名空间中,这样 grafana 安装了 prometheus 操作符(带有自动加载仪表板的边车),将“以 infrastructure-as-code 方式”自动加载它
【讨论】:
Grafana 可以下载这些仪表板(当然它需要访问互联网)。示例 bash 脚本:
#!/bin/bash
jq --version >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed. Aborting."; exit 1; }
### Please edit grafana_* variables to match your Grafana setup:
grafana_host="http://localhost:3000"
grafana_cred="admin:admin"
# Keep grafana_folder empty for adding the dashboards in "General" folder
grafana_folder="AWS CloudWatch"
ds=(1516 677 139 674 590 659 758 623 617 551 653 969 650 644 607 593 707 575 1519 581 584 2969 8050 11099 11154 11155 12979 13018 13040 13104);
folderId=$(curl -s -k -u "$grafana_cred" $grafana_host/api/folders | jq -r --arg grafana_folder "$grafana_folder" '.[] | select(.title==$grafana_folder).id')
if [ -z "$folderId" ] ; then echo "Didn't get folderId" ; else echo "Got folderId $folderId" ; fi
for d in "${ds[@]}"; do
echo -n "Processing $d: "
j=$(curl -s -k -u "$grafana_cred" $grafana_host/api/gnet/dashboards/$d | jq .json)
payload="{\"dashboard\":$j,\"overwrite\":true"
if [ ! -z "$folderId" ] ; then payload="${payload}, \"folderId\": $folderId }"; else payload="${payload} }" ; fi
curl -s -k -u "$grafana_cred" -XPOST -H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "$payload" \
$grafana_host/api/dashboards/import; echo ""
done
来源:https://github.com/monitoringartist/grafana-aws-cloudwatch-dashboards
随意修改它为您喜欢的语言和所需的仪表板集。请记住,某些仪表板可能还需要输入变量。
【讨论】: