问题描述
在 Kubernetes 中运行 Prometheus 时,我正在通过 ConfigMap
推出新配置.ConfigMap 在容器中作为文件公开.
While running Prometheus in Kubernetes, I'm pushing out new config via a ConfigMap
. ConfigMaps are exposed as files in the container.
我希望 Prometheus 在文件更改时自动重新加载其配置.
I would love Prometheus to automatically reload its configuration when the file changes.
这样的东西会起作用吗?
Would something like this work?
inotifywait -q -m -e close_write /etc/prometheus/config.yml |
while read -r filename event; do
curl -X POST http://localhost:9090/-/reload
done
推荐答案
(我花了一些时间来让它完全工作)这适用于一个小的边车容器.配置可能如下所示:
( I took some time to get this fully to work)this works with a small sidecar container. The configuration could look like this:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: prometheus
spec:
replicas: 1
template:
....
spec:
containers:
... (your actual container config goes here) ...
- name: refresh
imagePullPolicy: Always
args:
- /etc/prometheus/config.yml
- http://localhost:9090/-/reload
image: tolleiv/k8s-prometheus-reload
volumeMounts:
- name: config-volume
mountPath: /etc/prometheus
volumes:
- name: config-volume
configMap:
name: prometheus
实际检查是用这个脚本完成的,其中观察到的文件和 URL 作为参数传递:
The actual check is done with this script where the observed file and the URL are passed as parameters:
#!/bin/sh
while true; do
inotifywait "$(readlink -f $1)"
echo "[$(date +%s)] Trigger refresh"
curl -sSL -X POST "$2" > /dev/null
done
一切都可以在Dockerhub上的这个容器中找到
使用 -m
保留单个 inotifywait
不起作用,因为当 ConfigMap
更改时 Kubernetes 会进行符号链接处理.
Keeping a single inotifywait
with the -m
didn't work because of symlink juggling which is done by Kubernetes when the ConfigMap
changes.
这篇关于当底层 ConfigMap 发生变化时,我将如何运行一个简单的容器来触发 Prometheus 重新加载其配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!