1. 程式人生 > 實用技巧 >基於docker封裝prometheus解決時區問題

基於docker封裝prometheus解決時區問題

一、概述

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

效果如下:

另外再介紹一下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檔案,掛載一下即可。