Prometheus容器reload config

Prometheus容器reload config

按照之前的文章在kubernetes环境下部署好prometheus之后,监控进程正常运行。现在问题来了:prometheus的配置内容是configmap配置的,更新configmap之后如何让prometheus进程重新加载配置内容呢?

1、ConfigMap的热更新

在kubernetes集群内,当ConfigMap以volume形式挂载到pod内时,更新ConfigMap,kubernetes会自动同步被挂载到pod内的文件内容。但是注意,下面这种挂载方式是不会自动同步文件内容的:

          volumeMounts:
            - mountPath: /etc/prometheus/prometheus.yml
              name: prometheus-conf-volume
              subPath: prometheus.yml
      volumes:
        - name: prometheus-conf-volume
          configMap:
            name: prometheus-conf

正确的写法:

          volumeMounts:
            - mountPath: /etc/prometheus
              name: prometheus-conf-volume
      volumes:
        - name: prometheus-conf-volume
          configMap:
            name: prometheus-conf

ConfigMap被修改之后,不会立刻生效,大概会30秒内完成文件内容同步~~

2、Peometheus加载配置文件

根据官网的描述,Prometheus提供两种重新加载配置文件的方法:

  • 向prometheus进行发信号
/prometheus # ps -ef | grep prometheus
    1 root       0:14 /bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.enable-lifecycle --web.console.templates=/usr/share/prometheus/consoles
/prometheus # kill -HUP 1

这种方法首先需要进入容器内部,找到peometheus进行号,然后执行kill -HUP;

如果需要自动执行,可以编写一个shell脚本加入到镜像中,定时监测文件是否改变;

  • 向prometheus发送HTTP请求
LiondeMacBook-Pro:~ lion$ curl -XPOST http://prometheus.chenlei.com/-/reload

/-/reload只接收POST请求,并且需要在启动prometheus进程时,指定 --web.enable-lifecycle ,默认是没有指定的,所以需要修改镜像;

3、Dockerfile

分享一个Dockerfile,这个是根据官方提供的Dockerfile修改的,默认的peometheus是以nobody用户启动,在挂载volumn时不太方便,同事增加了 --web.enable-lifecycle

FROM        quay.io/prometheus/busybox:latest
LABEL maintainer "The Prometheus Authors <[email protected]>, Custom by <[email protected]>"

COPY prometheus                             /bin/prometheus
COPY promtool                               /bin/promtool
COPY prometheus.yml                         /etc/prometheus/prometheus.yml
COPY console_libraries/                     /usr/share/prometheus/console_libraries/
COPY consoles/                              /usr/share/prometheus/consoles/

RUN ln -s /usr/share/prometheus/console_libraries /usr/share/prometheus/consoles/ /etc/prometheus/
RUN mkdir -p /prometheus 

EXPOSE     9090
VOLUME     [ "/prometheus" ]
WORKDIR    /prometheus
ENTRYPOINT [ "/bin/prometheus" ]
CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \
             "--storage.tsdb.path=/prometheus", \
             "--web.console.libraries=/usr/share/prometheus/console_libraries", \
             "--web.enable-lifecycle", \
             "--web.console.templates=/usr/share/prometheus/consoles" ]

制作镜像前,下载GitHub上编译好的Release,解压,将Dockerfile放入解压路径执行:

root@master-1:~/kubernetes/prometheus/prometheus-2.2.1.linux-amd64# docker build -t 192.168.101.88:5000/prom/prometheus-c:v2.2.2 .

4、补充:自动识别静态目标

Prometheus提供file_sd_config用来自动识别静态目标,参考~~~

猜你喜欢

转载自blog.csdn.net/chenleiking/article/details/80182086
今日推荐