如何使用 dockerized Prometheus(和 Grafana)监控 ASP.Net Core 应用程序?

And*_*uul 8 docker docker-compose prometheus docker-for-windows

我希望使用 docker-images / docker-for-windows 在我的开发机器上运行 Prometheus 和 Grafana。

我正在开发系统,ASP.Net core,在 localhost:5001 上运行,指标在 https://localhost:5001/metrics 上显示得很好。

下面列出了 Docker-compose.yml 和 prometheus.yml。

  • 如果我在 docker-compose.yml 中包含 network_mode: host ,我将无法通过 localhost:9090 访问物理机上的 Prometheus
  • 如果我排除 network_mode 并使用 ports: ,我可以通过 localhost:9090 访问物理机上的 Prometheus,但检查 http://localhost:9090/targets 时,它显示 https://localhost:5001/metrics 已关闭。

我究竟做错了什么?有什么意见欢迎留言!

docker-compose.yml:

version: '3.8'
services:
  prometheus:
    image: prom/prometheus
    container_name: gradle_docker-prometheus
    #network_mode: host
    ports:
      - 9090:9090
    volumes:
      - prometheus-storage:/var/lib/prometheus
      - /c/Data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
  grafana:
    image: grafana/grafana
    container_name: gradle_docker-grafana
    ports:
      - "3000:3000"
    volumes:
      - grafana-storage:/opt/grafana/data
    depends_on:
      - prometheus

volumes:
  prometheus-storage: {}
  grafana-storage: {}
Run Code Online (Sandbox Code Playgroud)

普罗米修斯.yml:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

  external_labels:
      monitor: 'my-project'

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 10s
    scheme: http
    static_configs:
         - targets: ['localhost:9090','cadvisor:8080','node-exporter:9100', 'nginx-exporter:9113']
  - job_name: '.Net'
    scrape_interval: 10s
    scheme: https
    static_configs:
         - targets: ['localhost:5001']
Run Code Online (Sandbox Code Playgroud)

ane*_*yte 6

不要在 Windows 上使用主机网络模式,它仅在 Linux 上受支持。您需要的是更改目标地址:

  - job_name: '.Net'
    scrape_interval: 10s
    scheme: https # You may have to change this to 'http'
                  # or you'd have to create a certificate 
                  # with `host.docker.internal`
    static_configs:
         - targets: ['host.docker.internal:5001']
Run Code Online (Sandbox Code Playgroud)

host.docker.internal是连接到 Docker 主机的特殊地址,因为localhost容器内部只有容器本身。

总结一下注释中的内容:将目标更改为 后host.docker.internal,确保您的应用程序允许与该主机连接。跑步

curl http://localhost:5001/ -H "Host: host.docker.internal"
Run Code Online (Sandbox Code Playgroud)

并检查答案。如果您遇到包含以下内容的错误:

请求主机名无效。

然后你必须找出主机过滤器在哪里(它可能是一个包含在localhost其中的数组)并在那里添加新主机(host.docker.internal)。