Docker:无法使用 VictoriaMetrics "vmagent" 抓取 SonarQube,连接被拒绝

Docker: Can't scrape SonarQube with VictoriaMetrics "vmagent", connection refused

我正在构建 docker-compose.yml 并想使用 VictoriaMetrics 自己的抓取器 (vmagent) 来抓取 SonarQube 通过 /api/prometheus/metrics.

上的插件公开的指标

如果 docker 是 运行,我可以访问 localhost:9000/api/prometheus/metrics 并查看我的所有指标。 但是,vmagent 每 60 秒抛出以下错误(这意味着至少我的 prometheus.yml 配置被正确使用):

vmagent            | 2021-02-03T12:02:51.617Z error   VictoriaMetrics/lib/promscrape/scrapework.go:235
                     error when scraping "http://localhost:9000/api/prometheus/metrics" from job "sonarqube" with labels {instance="localhost:9000",job="sonarqube",monitor="codelab-monitor"}:
                     error when scraping "http://localhost:9000/api/prometheus/metrics": dial tcp4 127.0.0.1:9000: connect: connection refused; try -enableTCP6 command-line flag if you scrape ipv6 addresses

我不是要抓取 IPv6。

我的 docker-compose.yml 看起来像这样:

services:

  sonarqube:
    image: sonarqube:8.2-community
    depends_on:
      - postgresdb
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      [...]
    volumes:
      [...]

  postgresdb:
    [...]

  victoriametrics:
    container_name: victoriametrics
    image: victoriametrics/victoria-metrics
    ports:
      - 8428:8428
      - 8089:8089
      - 8089:8089/udp
      - 2003:2003
      - 2003:2003/udp
      - 4242:4242
    volumes:
      [...]
    command:
      - '--storageDataPath=/storage'
      - '--graphiteListenAddr=:2003'
      - '--opentsdbListenAddr=:4242'
      - '--httpListenAddr=:8428'
      - '--influxListenAddr=:8089'
    networks:
      - sonarnet
    restart: always

  vmagent:
    container_name: vmagent
    image: victoriametrics/vmagent
    depends_on:
      - "victoriametrics"
    ports:
      - 8429:8429
    volumes:
      - vmagentdata:/vmagentdata
      - ./prometheus:/etc/prometheus
    command:
      - '--promscrape.config=/etc/prometheus/prometheus.yml'
      - '--remoteWrite.url=http://victoriametrics:8428/api/v1/write'
    networks:
      - sonarnet
    restart: always

  vmalert:
    [...]

  alertmanager:
    [...]

  grafana:
    [...]

networks:
  sonarnet:
    driver: bridge

volumes:
  [...]

还有我的 prometheus.yml(我假设 vmagent 正在正确读取它,基于非默认的 60 秒的抓取间隔):

global:
  scrape_interval:     60s

  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'sonarqube'
    metrics_path: '/api/prometheus/metrics'
    static_configs:
      - targets: ['localhost:9000']

我怀疑我的 docker networks 配置有问题,但我不知道为什么 vmagent 无法访问 http://localhost:9000/api/prometheus/metrics.

vmagent 容器中 localhost 将意味着 vmagent 本身。尝试使用服务名称 sonarqube:9000 而不是 localhost:9000.

来引用