获取错误 "Get http://localhost:9443/metrics: dial tcp 127.0.0.1:9443: connect: connection refused"

Getting error "Get http://localhost:9443/metrics: dial tcp 127.0.0.1:9443: connect: connection refused"

我正在尝试使用我的 Hyperledger fabric v1.4 网络配置 Prometheus 和 Grafana 以分析对等点和链代码 mertics。在遵循此 documentation 之后,我已将对等容器的端口 9443 映射到主机的端口 9443。我还将同行 core.ymlmetrics 部分下的 provider 条目更改为 prometheus。我已经按照以下方式在 docker-compose.yml 中配置了 prometheus 和 grafana。

  prometheus:
    image: prom/prometheus:v2.6.1
    container_name: prometheus
    volumes:
    - ./prometheus/:/etc/prometheus/
    - prometheus_data:/prometheus
    command:
    - '--config.file=/etc/prometheus/prometheus.yml'
    - '--storage.tsdb.path=/prometheus'
    - '--web.console.libraries=/etc/prometheus/console_libraries'
    - '--web.console.templates=/etc/prometheus/consoles'
    - '--storage.tsdb.retention=200h'
    - '--web.enable-lifecycle'
    restart: unless-stopped
    ports:
    - 9090:9090
    networks:
    - basic
    labels:
    org.label-schema.group: "monitoring"

  grafana:
    image: grafana/grafana:5.4.3
    container_name: grafana
    volumes:
    - grafana_data:/var/lib/grafana
    - ./grafana/datasources:/etc/grafana/datasources
    - ./grafana/dashboards:/etc/grafana/dashboards
    - ./grafana/setup.sh:/setup.sh
    entrypoint: /setup.sh
    environment:
    - GF_SECURITY_ADMIN_USER={ADMIN_USER}
    - GF_SECURITY_ADMIN_PASSWORD={ADMIN_PASS}
    - GF_USERS_ALLOW_SIGN_UP=false
    restart: unless-stopped
    ports:
    - 3000:3000
    networks:
    - basic
    labels:
    org.label-schema.group: "monitoring"

当我在我的远程 centos 机器上 curl 0.0.0.0:9443/metrics 时,我得到了所有指标列表。但是,当我 运行 Prometheus 使用上述配置时,它会抛出错误 Get http://localhost:9443/metrics: dial tcp 127.0.0.1:9443: connect: connection refused。这就是我的 prometheus.yml 的样子。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'peer_metrics'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9443']

甚至,当我在浏览器中访问端点 http://localhost:9443/metrics 时,我会获得所有指标。我在这里做错了什么。为什么 Prometheus 指标显示在其界面上而不是同行的?

您的 prometheus 容器不在主机网络上 运行。它在自己的桥上 运行(由 docker-compose 创建的桥)。因此,peer 的 scrape 配置应该指向 peer 容器的 IP。

推荐的解决方法:

  • 运行 prometheus和grafana与fabric网络在同一个网络。 在 docker-compose for prometheus 堆栈中,您可以像这样引用它:
networks:
  default:
    external:
      name: <your-hyperledger-network>

(使用docker network ls查找网络名称)

然后你可以在你的抓取配置中使用http://<peer_container_name>:9443

由于目标不在 运行 prometheus 容器内,因此无法通过本地主机访问它们。您需要通过主机私有 IP 或将 localhost 替换为 docker.for.mac.localhosthost.docker.internal.

来访问它们

问题:在 Prometheus 上,您添加了一个用于抓取的服务,但在 http://localhost:9090/targets 上,端点状态是 Down 出现错误:

Get http://localhost:9091/metrics: dial tcp 127.0.0.1:9091: connect: connection refused

解决方案:在prometheus.yml你需要验证

  1. 抓取指向正确端点的详细信息。
  2. yml 缩进正确。
  3. 使用 curl -v http://<serviceip>:<port>/metrics 应该会在您的终端中以明文形式提示指标。

注意:如果您指向另一个 docker 容器中的某些服务,则您的本地主机可能不会表示为本地主机,而是表示为 servicename(服务名称docker ps 中显示的)或 docker.host.internal(运行 docker 容器的内部 ip)。

对于此示例:我将使用 2 个 dockers 容器 prometheus 和“myService”。

sudo docker ps

CONTAINER ID        IMAGE                     CREATED                        PORTS                    NAMES
abc123        prom/prometheus:latest        2 hours ago               0.0.0.0:9090->9090/tcp         prometheus
def456        myService/myService:latest         2 hours ago               0.0.0.0:9091->9091/tcp         myService

然后编辑文件 prometheus.yml(并重新运行 prometheus)

- job_name: myService
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  static_configs:
    - targets: // Presenting you 3 options
      - localhost:9091 // simple localhost 
      - docker.host.internal:9091 // the localhost of agent that runs the docker container
      - myService:9091 // docker container name (worked in my case)
      
        

好吧,我记得我通过下载 windows 的 Prometheus 节点导出器解决了这个问题。

看看这个 link https://medium.com/@facundofarias/setting-up-a-prometheus-exporter-on-windows-b3e45f1235a5

注意
此解决方案不适用于 docker swarm。它用于独立容器(多容器),旨在 overlay 网络上 运行。

我们在使用 overlay 网络时遇到同样的错误,这里是解决方案(静态而非动态)

此配置不起作用:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

  external_labels:
    monitor: 'promswarm'

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
      - targets: [ 'localhost:9100' ]

即使 http://docker.for.mac.localhost:9100/ 可用,这个也没有,但 prometheus 找不到 node-exporter。所以下面的也不起作用:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

  external_labels:
    monitor: 'promswarm'

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']


  - job_name: 'node'
    static_configs:
      - targets: [ 'docker.for.mac.localhost:9100'  ]

但只需使用其容器 ID,我们就可以通过其端口号访问该服务。

docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                       NAMES
a58264faa1a4   prom/prometheus          "/bin/prometheus --c…"   5 minutes ago    Up 5 minutes    0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   unruffled_solomon
62310f56f64a   grafana/grafana:latest   "/run.sh"                42 minutes ago   Up 42 minutes   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp   wonderful_goldberg
7f1da9796af3   prom/node-exporter       "/bin/node_exporter …"   48 minutes ago   Up 48 minutes   0.0.0.0:9100->9100/tcp, :::9100->9100/tcp   intelligent_panini

所以我们有 7f1da9796af3 prom/node-exporter ID,我们可以将 yml 文件更新为:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

  external_labels:
    monitor: 'promswarm'

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']


  - job_name: 'node'
    static_configs:
      - targets: [ '7f1da9796af3:9100'  ]

不工作

工作


更新

我本人对这种硬编码的解决方案并不满意,因此在进行了一些其他搜索后发现了一种使用 --network-alias NAME 的更可靠的方法,该方法位于 overlay 网络中,即容器将可以通过该名称进行路由。所以 yml 看起来像这样:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']


  - job_name: 'node'
    static_configs:
      - targets: [ 'node_exporter:9100' ]

其中名字node_exporter是用run子命令创建的别名。例如

docker run --rm  -d  -v "/:/host:ro,rslave" --network cloud --network-alias node_exporter --pid host -p 9100:9100   prom/node-exporter  --path.rootfs=/host

简而言之,它在覆盖 cloud 网络上说您可以使用 node_exporter:<PORT>.

访问节点导出器

如果您指向另一个 docker 容器中的某些服务,您的本地主机可能不会表示为本地主机,而是表示为服务名称( docker [=34 中显示的服务名称=] ) 或 运行 docker 容器 .

的内部 ip

prometheus.yml

  • job_name: "node-exporter"

    static_configs:

    • targets: ["nodeexporter:9100"] // docker 容器名称