如何将 Prometheus 运行 配置为容器以抓取其他应用程序生成的指标作为容器

How to Configure Prometheus running as container to scrape metrics generated by other application as container

我正在解决 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"

Docker文件:

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 可以通过指定的端口抓取指标。但是那里没有这样的指标。

这是我正在使用的 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"

Docker Mac。 dbserver:5000 上的应用 运行 是 Mac 上的 运行,而不是 Docker 中的应用。如果它在 Docker 内,我通过确保它被暴露并且我可以从 Mac 终端 window.