如何在 docker 上 运行 prometheus - 在本地编写和抓取 django 服务器 运行ning?

How to run prometheus on docker-compose and scrape django server running locally?

我正在尝试设置 prometheus 以使用 django-prometheus 和 Docker compose 监视我的 Django 应用程序。我一直在网上关注一些指南,但与我见过的所有指南不同,我现在想在本地 运行 Django 所以简单地 python manage.py runserver 和 运行 prometheus 与 docker-compose(然后添加 grafana)。我想这样做是为了在本地测试它,稍后我会将它部署到 Kubernetes 但这是另一集。

我的问题是让本地 运行ning django 服务器与 prometheus 运行ning 容器在同一网络中通信,因为我在 prometheus 仪表板上的 /targets 中收到此错误:

Get "http://127.0.0.1:5000/metrics": dial tcp 127.0.0.1:5000: connect: connection refused

这些是我的 docker-compose 文件和 prometheus 配置:

version: '3.6'

services:
  prometheus:
    image: prom/prometheus
    volumes:
    - ./prometheus/:/etc/prometheus/
    command:
    - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
    - 9090:9090
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets:
        - localhost:9090

  - job_name: django-app
    static_configs:
      - targets:
        - localhost:8000
        - 127.0.0.1:8000

如果你想 运行 Django 应用程序在(容器和外部)Docker Compose 然后,当 运行 时,它将绑定到主机的端口之一.

您还需要获取 Docker Compose prometheus 服务才能绑定到主机的网络。

您应该可以使用 prometheus 服务下的 network_mode: host 执行此操作。

然后,prometheus 将能够在它正在使用的主机端口上访问 Django 应用程序,并且 prometheus 将可以作为 localhost:9090 访问(不需要 ports 节)。