<input id="ohw05"></input>
  • <table id="ohw05"><menu id="ohw05"></menu></table>
  • <var id="ohw05"></var>
  • <code id="ohw05"><cite id="ohw05"></cite></code>
    <label id="ohw05"></label>
    <var id="ohw05"></var>
  • Prometheus之Dockerfile編寫、鏡像構建、容器啟動

    從官方鏡像啟動: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/

    posted @ 2022-02-25 21:17  wuenwuen  閱讀(188)  評論(0編輯  收藏  舉報
    国产美女a做受大片观看