Prometheus & Grafana 运行 on local env : How to link grafana container to prometheus container in local

Prometheus & Grafana running on local env : How to link grafana container to prometheus container in local

我正在将本地 Ubuntu 机器的 PromtheusGrafana 设置为 docker 容器,

我的步骤是:

  1. 运行宁普罗米修斯 : docker run -t -d -p 9090:9090 prom/prometheus
  2. 运行宁 Grafana : docker run -t -d --name grafana -p 3000:3000 grafana/grafana

正如您在映射的 9090 端口上看到的 prometheus 运行 一样,grafana 运行ning 在 3000

现在在 grafana 中为 prometheus 配置 grafana dashborad 时,我需要指出 prometheus 的 url :

-> 因为它们都在本地容器上 运行ning。

给 grafana 什么地址让它指向 prometheus?

对于 运行 docker 容器,如果需要查找其地址,以下命令可能会有所帮助。

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name>

这将return关联一个IP地址。

为了简单的设置,您可以使用 docker-compose 作为评论。使用 prometheus 和 grafana 的 docker-compose.yaml 文件示例:

docker-compose.yaml

version: "3"
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus.yaml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
  grafana:
    image: grafana/grafana:latest
    volumes:
      - ./grafana-storage:/var/lib/grafana
      - ./grafana/config.ini:/etc/grafana/config.ini
      - ./grafana/provisioning:/etc/grafana/provisioning
      - ./grafana/dashboards:/var/lib/grafana/dashboards
    ports:
      - 3001:3000

prometheus.yaml

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

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

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# 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: 'your-app'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
      - targets: ['your-app:3000']

config.ini

[paths]
provisioning = /etc/grafana/provisioning

[server]
enable_gzip = true

[users]
default_theme = light

您的 Grafana 容器将无法联系(发现)您的 Prometheus 容器,因为当 docker 启动每个容器时,它会在主机系统上创建一个具有唯一名称(如 vethef766ac)的虚拟接口,并隔离容器。

如果您不想使用 docker compose 并且如果您想使用其主机 IP 访问您的 Grafana 容器,则必须 运行 使用 --network 选项在主机网络中使用您的 Grafana 容器。

然后您可以 运行 Grafana:

docker run -t -d --name grafana --network="host" grafana/grafana

Note: --network="host" gives the container full access to local system services such as D-bus and is therefore considered insecure.

您要在 Grafana 中指定的 URL 将是 http://localhost:9090