Kubernetes 当底层ConfigMap发生变化时,我如何运行一个简单的容器触发器Prometheus来重新加载其配置?

Kubernetes 当底层ConfigMap发生变化时,我如何运行一个简单的容器触发器Prometheus来重新加载其配置?,kubernetes,prometheus,Kubernetes,Prometheus,在库伯内特斯运行普罗米修斯时,我通过ConfigMap推出了新的配置。ConfigMaps在容器中作为文件公开 我希望普罗米修斯在文件更改时自动重新加载其配置 你喜欢这个工作吗 inotifywait -q -m -e close_write /etc/prometheus/config.yml | while read -r filename event; do curl -X POST http://localhost:9090/-/reload done (编辑:我花了一些时间让它完

在库伯内特斯运行普罗米修斯时,我通过
ConfigMap
推出了新的配置。ConfigMaps在容器中作为文件公开

我希望普罗米修斯在文件更改时自动重新加载其配置

你喜欢这个工作吗

inotifywait -q -m -e close_write /etc/prometheus/config.yml |
while read -r filename event; do
  curl -X POST http://localhost:9090/-/reload
done
(编辑:我花了一些时间让它完全发挥作用) 这适用于一个小型侧车容器。配置可能如下所示:

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
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
使用此脚本完成实际检查,其中观察到的文件和URL作为参数传递:

#!/bin/sh
while true; do
   inotifywait "$(readlink -f $1)"
   echo "[$(date +%s)] Trigger refresh"
   curl -sSL -X POST "$2" > /dev/null
done
每样东西都可以在里面找到

使用
-m
保持一个
inotifywait
无效,因为当
ConfigMap
发生变化时,Kubernetes会进行符号链接杂耍。

(编辑:我花了一些时间使其完全起作用) 这适用于一个小型侧车容器。配置可能如下所示:

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
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
使用此脚本完成实际检查,其中观察到的文件和URL作为参数传递:

#!/bin/sh
while true; do
   inotifywait "$(readlink -f $1)"
   echo "[$(date +%s)] Trigger refresh"
   curl -sSL -X POST "$2" > /dev/null
done
每样东西都可以在里面找到


使用
-m
保持单个
inotifywait
不起作用,因为当
ConfigMap
更改时,Kubernetes会执行符号链接杂耍。另一个选项是使用
livenssprobe
命令,并在配置更改时触发
Pod
的重新启动

可能是这样的:

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
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

一个缺点可能是这样会丢失缓存在内存中的数据,但它是直接的,不需要侧车容器。

另一个选项是使用
livenssprobe
命令,在配置更改时只触发
Pod
的重新启动

可能是这样的:

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
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

缺点可能是这样会丢失缓存在内存中的数据,但它是直接的,不需要侧车容器。

我已经更新了我的答案-我发现有一个可以供所有人使用的完全工作的解决方案非常有趣。如果这对你有用的话,那就太好了。我已经更新了我的答案——事实上,我发现有一个可以供所有人使用的完全有效的解决方案是非常有趣的。如果这对你有用,那就太好了。哇,这是一个非常完整的答案!不幸的是,我看到它通过普罗米修斯日志每秒触发大约100次重新加载。也许如果它在卷曲之前对文件进行md5比较…奇怪。。。很高兴知道一旦它对你起作用了,它确实起作用了。我无法复制我的失控刷新模式。谢谢集装箱现在不见了。请把Dockerfile的内容贴在这里哇,这是一个非常完整的答案!不幸的是,我看到它通过普罗米修斯日志每秒触发大约100次重新加载。也许如果它在卷曲之前对文件进行md5比较…奇怪。。。很高兴知道一旦它对你起作用了,它确实起作用了。我无法复制我的失控刷新模式。谢谢集装箱现在不见了。请在此发布Dockerfile内容您的方法重新启动容器,我希望在不重新启动的情况下重新加载配置您的方法重新启动容器,我希望在不重新启动的情况下重新加载配置