【问题标题】:How to Configure Prometheus running as container to scrape metrics generated by other application as container如何配置作为容器运行的 Prometheus 以抓取其他应用程序生成的指标作为容器
【发布时间】:2019-02-07 04:26:50
【问题描述】:

我正在解决 Docker 中 Prometheus 和 python 应用程序之间的网络问题。如何让 Prometheus 能够在 docker 中抓取 python 应用程序生成的指标,并将其显示在 Prometheus 端。

Docker-compose.yml

version: '3'
services:
  prometheus:
    image: prom/prometheus:v2.0.0
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
    links:
      - web
  web:
    image: python:3.5-alpine
    build: ./test
    ports:
     - "5000:8002"

Dockerfile:

FROM python:3.5-alpine
ADD app1.py /
RUN pip install prometheus_client
CMD ["python", "./app1.py"]
EXPOSE 8002

prometheus.yml

# my global config
global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  external_labels:
      monitor: 'codelab-monitor'
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['docker.for.mac.host.internal:9090']
  - job_name: 'python_app'
    metrics_path: /  
    static_configs:
      - targets: ['docker.for.mac.host.internal:8002']

到目前为止,我可以看到 Prometheus 运行良好,其目标的状态显示为 UP 在“docker.for.mac.host.internal:9090”和“docker.for.mac.host.internal:8002”上

python 应用程序也在运行,我可以在端口上看到输出指标

所以现在一切都应该正常了,Prometheus 可以通过指定端口抓取指标。但是那里没有这样的指标。

【问题讨论】:

  • 您创建了一个名为“web”的链接,您是否尝试在“static_configs.targets”中使用它?像“网络:8002”之类的东西。 IMO 'docker.for.mac.host.internal' 仍然命中同一个容器 - 带有 prometheus 的容器。

标签: python docker containers prometheus


【解决方案1】:

这是我正在使用的 prometheus.yml 文件的相对部分:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    scrape_timeout: 10s

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

    target_groups:
      - targets: ['localhost:9090']

  - job_name: app-server
    target_groups:
      - targets: ['dbserver:5000']

请注意,您还需要通过自己点击各种 URL 来验证是否有要提取的指标。

curl "http://dbserver:5000/metrics"

适用于 Mac 的 Docker。在 dbserver:5000 上运行的应用程序在 Mac 上运行,而不是在 Docker 内部。如果它在 Docker 中,我会通过确保它被暴露并且我可以从 Mac 终端窗口使用 curl 来实现它。

【讨论】:

    猜你喜欢
    • 2019-08-23
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多