基于docker封装prometheus解决时区问题

发布时间:2020-08-20 16:38:29编辑:admin阅读(3629)

    一、概述

    官方dockerhub上面的prometheus,下载命令如下:

    docker pull prom/prometheus

    发现它的时区为:UTC,我需要更改为CST,也就是中国上海时区。

    发现修改变量TZ=Asia/Shanghai,修改/etc/localtime 文件都无法修改时区,均失败了。

    那么解决办法,就只有自己封装prometheus镜像了。

     

    二、启动prometheus

    环境说明

    操作系统:centos 7.6

    docker版本:19.03.5

    ip地址:192.168.31.229

     

    封装prometheus

    目录结构

    新建目录/opt/myprometheus,目录结构如下:

    ./
    ├── dockerfile
    └── prometheus-2.20.0.linux-amd64.tar.gz


    dockerfile

    FROM centos:7
    ADD prometheus-2.20.0.linux-amd64.tar.gz /
    
    RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
        mv /prometheus-2.20.0.linux-amd64 /prometheus
    
    WORKDIR /prometheus
    ENTRYPOINT [ "/prometheus/prometheus" ]
    CMD        [ "--config.file=/prometheus/prometheus.yml", \
                 "--storage.tsdb.path=/prometheus/data", \
                 "--web.console.libraries=/prometheus/console_libraries", \
                 "--web.enable-lifecycle", \
                 "--web.console.templates=/prometheus/consoles" ]


    prometheus-2.20.0.linux-amd64.tar.gz 是从官方下载的,链接如下:https://prometheus.io/download/

     

    生成镜像

    docker build -t myprometheus:1 .

    启动镜像

    # 创建持久化目录
    mkdir -p /data/prometheus
    
    # 后台启动
    docker run -d \
      --restart=always \
      --name prometheus \
      -p 9090:9090 \
      myprometheus:1
    
    # 等待几秒,拷贝容器文件
    docker cp prometheus:/prometheus/  /data/prometheus
    
    # 删除容器
    docker rm -f prometheus
    
    # 挂载目录启动
    docker run -d \
      --restart=always \
      --name prometheus \
      -p 9090:9090 \
      -v /data/prometheus:/prometheus \
      myprometheus:1

     

    查看时区

    # docker exec -it prometheus date
    Thu Jul 30 17:54:37 CST 2020
    发现时区是正确的。

     

    访问页面

    http://192.168.31.229:9090

    效果如下:

    1.png

     

     

    另外再介绍一下alertmanager修改时区,镜像下载命令为:

    docker pull prom/alertmanager

     

    那么启动命令为:

    mkdir -p /data/alertmanager /data/alertmanager/storage
    
    docker run -d \
      -p 9093:9093 \
      --name alertmanager \
      --restart=always \
      -v /etc/localtime:/etc/localtime \
      -v /data/alertmanager:/etc/alertmanager \
      -v /data/alertmanager/storage:/alertmanager \
      prom/alertmanager

    这里直接将/etc/localtime文件,挂载一下即可。


关键字