Docker 部署 Prometheus

Docker Prometheus DevOps About 2,412 words

搜索镜像

docker search prom/prometheus

拉取镜像

版本号可以去官网查看:https://hub.docker.com/r/prom/prometheus

docker pull prom/prometheus:v2.32.1

配置文件

名称prometheus.yml

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

创建容器

  • --restart=always:容器退出后(kill后)自动重启。
  • -p 9090:9090:冒号前的9090是宿主机端口,冒号后的9090是容器端口。
  • $PWD/prometheus/prometheus.yml:映射配置文件位置,注意需要提前创建好配置文件。
  • $PWD/prometheus/data:/prometheus:映射数据存储位置。
  • /etc/localtime:/etc/localtime:ro:容器内部的时间格式化保持和宿主机一致。
  • --storage.tsdb.retention.time=100d:保留100天数据。
  • --config.file:指定配置文件,路径是容器中的路径,不是宿主机中的路径。
  • --web.enable-lifecycle:热加载配置文件。修改prometheus.yml后无需重启,但需要手动发起POST请求http://127.0.0.1:9090/-/reload接口。
docker run -d --restart=always \
-u root \
--name prometheus \
-p 9090:9090 \
-v $PWD/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v $PWD/prometheus/data:/prometheus \
-v /etc/localtime:/etc/localtime:ro \
prom/prometheus:v2.32.1 \
--storage.tsdb.retention.time=100d \
--config.file=/etc/prometheus/prometheus.yml

进入容器

注意:prometheus用的是sh,不是bash

docker exec -it prometheus sh

可能的错误

在指定了保存天数--storage.tsdb.retention.time参数后,必须指定--config.file配置文件路径。否则会报以下异常。

level=error msg="Error loading config (--config.file=prometheus.yml)" err="open prometheus.yml: no such file or directory"

Docker容器中的prometheus服务默认是以nobody用户启动的,如果出现权限问题,可以在创建容器时指定root用户,或更改文件权限。

err="open /prometheus/queries.active: permission denied"

出现iptables错误,需要重启docker服务:service docker restart

docker0: iptables: No chain/target/match by that name

链接

控制台:http://localhost:9090

二进制文件下载地址:https://prometheus.io/download/

Views: 3,558 · Posted: 2022-02-15

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh