【问题标题】:How can I monitor a ASP.Net Core app with dockerized Prometheus (and Grafana)?如何使用 dockerized Prometheus(和 Grafana)监控 ASP.Net Core 应用程序?
【发布时间】:2021-06-17 13:23:24
【问题描述】:

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

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

Docker-compose.yml 和 prometheus.yml 列出如下。

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

我做错了什么?欢迎任何cmets!

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: {}

prometheus.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']

【问题讨论】:

    标签: docker docker-compose prometheus docker-for-windows


    【解决方案1】:

    不要在 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']
    

    host.docker.internal 是连接 Docker 主机的特殊地址,因为容器内的localhost 就是容器。

    【讨论】:

    • 谢谢,Anemyte,似乎正是我要找的。不过,我无法让它工作——我在主机上的目标似乎仍然处于脱机状态。 curl、apk 和 apt 未安装在 docker-image 上,并且 ping 显示“权限被拒绝 - 你是 root 吗?”。有什么建议?我有非常基本的 linux 知识,我什至无法检查我正在运行哪个发行版:/
    • @AndersJuul 好吧,你可以在那里添加 curl :) 。使用image: debian:buster 添加另一个服务,然后运行docker-compose run <service_name>。您将进入此新服务的控制台提示符,您可以在其中apt update && apt install curl。让我知道错误是什么,我怀疑它是错误的协议\证书(HTTPS 而不是 HTTP)或防火墙。
    • root@6ac68dcf7ffb:/# curl host.docker.internal:57902 ttp://www.w3.org/TR/html4/strict.dtd" rel="nofollow" target="_blank">w3.org/TR/html4/strict.dtd"> 错误请求

      错误请求 -主机名无效


      HTTP 错误 400。请求主机名无效。

    • root@6ac68dcf7ffb:/# curl host.docker.internal:57902/metrics ttp://www.w3.org/TR/html4/strict.dtd" rel="nofollow" target="_blank">w3.org/TR/html4/strict.dtd"> 错误请求

      错误请求 - 无效主机名


      HTTP 错误 400。请求主机名无效。

    猜你喜欢
    • 2020-07-03
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多