我已将 kube-prometheus-stack 的实例(https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack)安装到 Rancher 集群上并使用自定义values.yaml来添加Grafana配置。
values.yaml 的 Grafana 部分:
grafana:
enabled: true
ingress:
enabled: disabled
service:
type: ClusterIP
port: 3000
targetPort: 3000
annotations: {}
labels: {}
portName: service
sidecar:
image:
repository: kiwigrid/k8s-sidecar
tag: 1.1.0
sha: ""
imagePullPolicy: IfNotPresent
skipTlsVerify: true
enableUniqueFilenames: false
dashboards:
enabled: true
SCProvider: true
label: grafana_dashboard
folder: /tmp/dashboards
defaultFolderName: null
searchNamespace: ALL
folderAnnotation: null
provider:
name: sidecarProvider
orgid: 1
folder: ''
type: file
disableDelete: false
allowUiUpdates: true
foldersFromFilesStructure: false
datasources:
enabled: true
label: grafana_datasource
searchNamespace: ALL
在安装之前,我还在 Rancher 中存储了一个名为
grafana-datasource
的配置映射,并具有标签:键:grafana_datasource
,值:1
。此配置映射存储在与 kube-prometheus-stack 应用程序相同的命名空间中。
在这个
grafana-datasource
配置图中,我放置了以下数据:
apiVersion: 1
datasources:
- name: Test-Prometheus
type: prometheus
url: https://prometheus.test.net/
access: proxy
isDefault: false
basicAuth: true
basicAuthUser: admin
basicAuthPassword: password
withCredentials: false
isDefault: false
version: 1
editable: true
我已验证我的数据源存在,并且能够登录并通过 Grafana GUI 手动添加。但是,安装并未获取我的
grafana-datasource
配置映射,仅加载默认的 Grafana 数据源:
如何让我的 grafana 在安装时拾取并应用自定义数据源配置映射?
我复制了您的设置,一切正常。
看来您可能跳过了 configmap 文件名的
.yaml/.yml
扩展名(并且由于您没有提及您使用的名称,这是最可能的原因)。添加如下所示的内容,它应该可以工作。
apiVersion: v1
data:
custom.yaml: |
apiVersion: 1
datasources:
- name: Test-Prometheus
type: prometheus
url: https://prometheus.test.net/
access: proxy
isDefault: false
basicAuth: true
basicAuthUser: admin
basicAuthPassword: password
withCredentials: false
isDefault: false
version: 1
editable: true
kind: ConfigMap
metadata:
creationTimestamp: null
name: grafana-datasource
我通过添加一个作业来重新启动grafana pod中的grafana服务器解决了这个问题。由于我使用 argocd 来自动化部署,因此这是为我管理的
apiVersion: batch/v1
kind: Job
metadata:
name: grafana-reload-job
namespace: monitoring
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "5"
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
spec:
serviceAccountName: monitoring-grafana
containers:
- name: kubectl-exec
image: bitnami/kubectl:latest
command: ["/bin/sh"]
args:
- -c
- |
POD=$(kubectl get pod -n monitoring -l app.kubernetes.io/name=grafana -o jsonpath="{.items[0].metadata.name}")
kubectl exec -it -n monitoring $POD -- sh -c "kill \$(ps aux | grep '[g]rafana server' | awk '{print \$1}')"
restartPolicy: Never