Prometheus之Dockerfile编写、镜像构建、容器启动

虚幻大学 xuhss 607℃ 0评论

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475
目录* 从官方镜像启动:prom/prometheus

从官方镜像启动: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 "

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 "

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

61dcb659b90b14f5f787dc6311e350ee - Prometheus之Dockerfile编写、镜像构建、容器启动

转载请注明:xuhss » Prometheus之Dockerfile编写、镜像构建、容器启动

喜欢 (0)

您必须 登录 才能发表评论!