Prometheus之Dockerfile編寫、映象構建、容器啟動

wuenwuen發表於2022-02-25

從官方映象啟動:prom/prometheus

拉取映象

$ docker pull prom/prometheus

啟動容器

方式1:

$ docker run -td -p 9090:9090 --name prometheus1 prom/prometheus

方式2:路徑掛載

$ docker run \
    -p 9090:9090 \
    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

$ docker run \
    -p 9090:9090 \
    -v /path/to/config:/etc/prometheus \
    prom/prometheus

官方Dockerfile分析

官方Dockerfile檔案如下

ARG ARCH="amd64"
ARG OS="linux"
FROM quay.io/prometheus/busybox-${OS}-${ARCH}:latest
LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com>"

ARG ARCH="amd64"
ARG OS="linux"
COPY .build/${OS}-${ARCH}/prometheus        /bin/prometheus
COPY .build/${OS}-${ARCH}/promtool          /bin/promtool
COPY documentation/examples/prometheus.yml  /etc/prometheus/prometheus.yml
COPY console_libraries/                     /usr/share/prometheus/console_libraries/
COPY consoles/                              /usr/share/prometheus/consoles/
COPY LICENSE                                /LICENSE
COPY NOTICE                                 /NOTICE
COPY npm_licenses.tar.bz2                   /npm_licenses.tar.bz2

WORKDIR /prometheus
RUN ln -s /usr/share/prometheus/console_libraries /usr/share/prometheus/consoles/ /etc/prometheus/ && \
    chown -R nobody:nobody /etc/prometheus /prometheus

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

從官網Dockerfile檔案可知:

  1. 使用busybox作為基礎映象
  2. 拷貝相關檔案(prometheus、promtool、prometheus.yml等)到指定目錄(/bin、/etc)
  3. 指定工作目錄/prometheus
  4. 容器內埠9090
  5. 指定預設匿名卷為"/prometheus",Prometheus執行產生的資料將寫到宿主機相關目錄
  6. ENTRYPOINT容器啟動入口點為"/bin/prometheus"
  7. CMD指定容器啟動引數為:"--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.path=/prometheus","--web.console.libraries=/usr/share/prometheus/console_libraries", "--web.console.templates=/usr/share/prometheus/consoles"

容器內Prometheus啟動命令為:

/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles

進入容器檢視Prometheus的程式:

[root@docker ~]# docker exec -it prometheus1 sh
/prometheus $
/prometheus $ ps -ef | grep prometheu[s]
    1 nobody    0:53 /bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles

編寫自己的Dockerfile

這裡編寫自己的Dockerfile進行構建,開啟Prometheus程式的配置熱載入和資料庫管理功能。

FROM amd64/busybox:1.35
LABEL maintainer="The Prometheus Authors <wuenwuen>"

ARG ARCH="amd64"
ARG OS="linux"
COPY prometheus-*.${OS}-${ARCH}/prometheus 				/prometheus/bin/
COPY prometheus-*.${OS}-${ARCH}/promtool 				/prometheus/bin/
COPY prometheus-*.${OS}-${ARCH}/prometheus.yml 			/prometheus/etc/prometheus.yml
COPY prometheus-*.${OS}-${ARCH}/console_libraries/*    /prometheus/console_libraries/
COPY prometheus-*.${OS}-${ARCH}/consoles/*             /prometheus/consoles/
COPY prometheus-*.${OS}-${ARCH}/LICENSE               /prometheus/LICENSE
COPY prometheus-*.${OS}-${ARCH}/NOTICE                /prometheus/NOTICE

WORKDIR /prometheus
RUN ln -s /prometheus/bin/prometheus /prometheus/bin/promtool /bin/ && \
    chown -R root:root /prometheus

USER       root
EXPOSE     9090
VOLUME     [ "/prometheus/data", "/prometheus/etc" ]
ENTRYPOINT [ "/bin/prometheus" ]
CMD        [ "--config.file=/prometheus/etc/prometheus.yml", \
             "--storage.tsdb.path=/prometheus/data", \
             "--web.console.libraries=/prometheus/console_libraries", \
             "--web.console.templates=/prometheus/consoles", \
             "--web.enable-lifecycle", \
             "--web.enable-admin-api" ]

解讀:

  1. 使用amd64/busybox:1.35作為基礎映象,將Prometheus的二進位制檔案放入bin目錄,將配置檔案統一放入etc目錄;
  2. 將配置檔案目錄和資料儲存目錄定義為匿名卷;
  3. 啟動方式中,新增兩項配置,開啟配置檔案的熱載入和資料庫管理功能。

在同目錄下新增一個.dockerignore檔案,來遮蔽一些無關構建的檔案:

prometheus-*.linux-amd64.tar.gz
etc

構建映象:

新建目錄,將版本包和Dockerfile檔案放入該目錄

# mkdir /root/prometheus;cd /root/prometheus
# ls
Dockerfile prometheus-2.33.1.linux-amd64.tar.gz

解壓版本包,同時新建.dockerignore檔案,在該檔案下新增與構建無關的檔案或目錄(這表示構建時,不將這些檔案複製到構建上下文環境中)

# tar -zxf prometheus-2.33.1.linux-amd64.tar.gz
# cat .dockerignore
prometheus-*.linux-amd64.tar.gz

執行構建命令docker build

$ docker build -t prometheus:v1 .
Sending build context to Docker daemon  200.8MB
Step 1/18 : FROM amd64/busybox:1.35
 ---> 96b2896db672
Step 2/18 : LABEL maintainer="The Prometheus Authors <wuenwuen>"
 ---> Running in 04e215d3c3e0
Removing intermediate container 04e215d3c3e0
 ---> 88050ed9e09d
Step 3/18 : ARG ARCH="amd64"
 ---> Running in 5ce5d1b12e0f
Removing intermediate container 5ce5d1b12e0f
 ---> 370832a62c71
Step 4/18 : ARG OS="linux"
 ---> Running in d7ea847b2d90
Removing intermediate container d7ea847b2d90
 ---> cf9781cb7722
Step 5/18 : COPY prometheus-*.${OS}-${ARCH}/prometheus                          /prometheus/bin/
 ---> 42b4b03f8332
Step 6/18 : COPY prometheus-*.${OS}-${ARCH}/promtool                            /prometheus/bin/
 ---> 897066bd0ca3
Step 7/18 : COPY prometheus-*.${OS}-${ARCH}/prometheus.yml                      /prometheus/etc/prometheus.yml
 ---> b141fa2c22a6
Step 8/18 : COPY prometheus-*.${OS}-${ARCH}/console_libraries/*    /prometheus/console_libraries/
 ---> 228b62d5e860
Step 9/18 : COPY prometheus-*.${OS}-${ARCH}/consoles/*             /prometheus/consoles/
 ---> 5c9125f39322
Step 10/18 : COPY prometheus-*.${OS}-${ARCH}/LICENSE                /prometheus/LICENSE
 ---> c89f57bbceb6
Step 11/18 : COPY prometheus-*.${OS}-${ARCH}/NOTICE                 /prometheus/NOTICE
 ---> 7e459dfd86fe
Step 12/18 : WORKDIR /prometheus
 ---> Running in 8f09cea3e03e
Removing intermediate container 8f09cea3e03e
 ---> ee1f386d159b
Step 13/18 : RUN ln -s /prometheus/bin/prometheus /prometheus/bin/promtool /bin/ &&     chown -R root:root /prometheus
 ---> Running in 093e9b6b8d14
Removing intermediate container 093e9b6b8d14
 ---> 121df7a93221
Step 14/18 : USER       root
 ---> Running in b4ffed0d491a
Removing intermediate container b4ffed0d491a
 ---> 75e2aada4653
Step 15/18 : EXPOSE     9090
 ---> Running in b09e22947d56
Removing intermediate container b09e22947d56
 ---> c700606bd44d
Step 16/18 : VOLUME     [ "/prometheus/data", "/prometheus/etc" ]
 ---> Running in e18dd4f3af2a
Removing intermediate container e18dd4f3af2a
 ---> 824d398febdd
Step 17/18 : ENTRYPOINT [ "/bin/prometheus" ]
 ---> Running in ff8fb122f2fb
Removing intermediate container ff8fb122f2fb
 ---> 414d2523bb9b
Step 18/18 : CMD        [ "--config.file=/prometheus/etc/prometheus.yml",              "--storage.tsdb.path=/prometheus/data",              "--web.console.libraries=/prometheus/console_libraries",              "--web.console.templates=/prometheus/consoles",              "--web.enable-lifecycle",              "--web.enable-admin-api" ]
 ---> Running in 56a8542d31a9
Removing intermediate container 56a8542d31a9
 ---> 17f5fa014281
Successfully built 17f5fa014281
Successfully tagged prometheus:v1

檢視構建的映象:

$ docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
prometheus        v1        17f5fa014281   6 minutes ago   403MB
amd64/busybox     1.35      96b2896db672   2 weeks ago     1.24MB
prom/prometheus   latest    a3d385fc29f9   2 months ago    201MB
# 可以看到自己構建的映象比官方映象大了一倍

啟動容器:

直接啟動:

$ docker run -td prometheus:v1

$ docker run -td -p 9090:9090 prometheus:v1

直接啟動後,配置檔案和資料儲存目錄將預設使用匿名掛載。

掛載路徑使用docker inspect命令檢視

路徑掛載(推薦):

選擇掛載出配置檔案目錄,資料儲存目錄使用預設的匿名掛載就行了。

由於路徑掛載時,容器內掛載路徑下的檔案會被隱藏,同時Prometheus的啟動又需要指定配置檔案,所以啟動容器前,需要提前將配置檔案放在掛載點路徑下,以避免容器啟動後Prometheus程式無法啟動,導致容器退出。

新建掛載點,並存放配置檔案:

$ tree /root/prometheus/etc
/root/prometheus/etc
├── first_rules.yml
├── prometheus.yml
└── static_config
    └── node_exporter.yml

然後就可以使用構建的映象來啟動容器了,命令如下

# 選擇其中一個即可
$ docker -td -v /root/prometheus/etc:/prometheus/etc prometheus:v1

$ docker run -td -p 9090:9090 --name prometheus-1 -v /root/prometheus/etc:/prometheus/etc prometheus:v1

$ docker run  \
	-td -p 9090:9090 --name prometheus-1 \
    -v /root/prometheus/etc:/prometheus/etc \
    prometheus:v1

檢視容器:

$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED              STATUS              PORTS                                       NAMES
b2ebd62251ac   prometheus:v1   "/bin/prometheus --c…"   About a minute ago   Up About a minute   0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   prometheus-1

$ docker ps --format "table {{.Image}}\t{{.ID}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
IMAGE           CONTAINER ID   PORTS                                       STATUS         NAMES
prometheus:v1   b2ebd62251ac   0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   Up 6 minutes   prometheus-1

訪問Prometheus的UI介面http://192.168.175.130:9090/

相關文章