【问题标题】:How would I run a simple container the triggers Prometheus to reload its config when the underlying ConfigMap changes?当底层 ConfigMap 发生变化时,我将如何运行一个简单的容器来触发 Prometheus 重新加载其配置?
【发布时间】:2017-06-18 22:28:44
【问题描述】:

在 Kubernetes 中运行 Prometheus 时,我正在通过 ConfigMap 推出新配置。 ConfigMap 在容器中作为文件公开。

我希望 Prometheus 在文件更改时自动重新加载其配置。

这样的东西有用吗?

inotifywait -q -m -e close_write /etc/prometheus/config.yml |
while read -r filename event; do
  curl -X POST http://localhost:9090/-/reload
done

【问题讨论】:

  • 我已经更新了我的答案 - 实际上我发现拥有一个可供所有人使用的完全有效的解决方案非常有趣。如果这对你也有用的话会很酷

标签: kubernetes prometheus


【解决方案1】:

(编辑:我花了一些时间让它完全发挥作用) 这适用于小型边车容器。配置可能如下所示:

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 作为参数传递:

#!/bin/sh
while true; do
   inotifywait "$(readlink -f $1)"
   echo "[$(date +%s)] Trigger refresh"
   curl -sSL -X POST "$2" > /dev/null
done

一切都可以在this container on Dockerhub找到

inotifywait-m 保持一致不起作用,因为当ConfigMap 更改时,Kubernetes 会进行符号链接处理。

【讨论】:

  • 哇,这是一个非常完整的答案!不幸的是,我看到它通过普罗米修斯日志每秒触发大约 100 次重新加载。
  • 也许如果它在卷曲之前对文件进行了 md5 比较...
  • 奇怪...很高兴知道它也适用于您
  • 我已经纠正了它确实有效。我无法重现我的失控刷新模式。谢谢!
  • 容器现在不见了。请在此处发布 Dockerfile 内容
【解决方案2】:

另一种选择是使用livenessProbe 命令并在配置更改时触发Pod 的重启。

可能是这样的:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus:latest
        ports:
        - containerPort: 9090
        volumeMounts:
          - name: config-volume
            mountPath: /etc/prometheus
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - "test -z $(find /etc/prometheus -mmin -2)"
          initialDelaySeconds: 300
          periodSeconds: 10
      volumes:
        - name: config-volume
          configMap:
            name: prometheus

一个缺点可能是这样你会丢失缓存在内存中的数据,但它很简单,不需要边车容器。

【讨论】:

  • 您的方法重新启动容器,我想重新加载配置而无需重新启动
猜你喜欢
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 2021-06-25
  • 2018-04-28
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多