Jinx

一只奶牛猫

Docker安装Prometheus和grafana

使用docker安装prometheus和grafana后

当我使用prometheus时,服务正常,我可以从 http://localhost:9090 访问UI,所有目标都在运行。

我在grafana上Add new connection时,出现下述错误

 Error reading Prometheus: Post "http://localhost:9090/api/v1/query": dial tcp 127.0.0.1:9090: connect: connection refused

he prometheus.yml is:

global:
  scrape_interval: 15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: "codelab-monitor"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "histogram"

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ["localhost:1010"]

从grafana-github的issue发现问题

Grafana and Prometheus are both deployed using Docker? The localhost of one container is not the localhost of another container, even if you published the port to the host – you can’t reach the Prometheus container or the host using localhost from the Grafana container. You need to use the IP address of the Prometheus container, or the hostname if you are using Docker Compose.If you need further help, please post your Docker configs (docker-compose.yml or docker run commands).

解决办法:

如果是用docker启动这两个容器可以尝试使用http://host.docker.internal:9090 去添加数据源

也可以 prometheus:9090,prometheus为容器的名称