Prometheus 用于数据收集和监控,Grafana 用于数据可视化。

使用 docker compose 安装 Prometheus + Grafana。

docker-compose.yaml

version: '3'
services:
    # influxdb
    influxdb:
        image: influxdb:2.7.1
        container_name: influxdb
        restart: always
        ports:
            - 8006:8086
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - /mnt/storage/docker/data/influxdb/ect:/etc/influxdb2
            - /mnt/storage/docker/data/influxdb/lib:/var/lib/influxdb2
    # 服务监控
    # 监控主机的系统指标(如 CPU、内存、磁盘使用率等)
    # 默认,Grafana Web 界面使用端口 3000 访问,而 Prometheus 使用端口 9090 ,Node Exporter 使用端口 9100
    # 1.1 数据节点
    nodeexporter:
        image: prom/node-exporter:latest
        container_name: nodeexporter
        restart: always
        ports:
            - 8010:9100
    # 1.2 轮询数据
    prometheus:
        user: root
        image: prom/prometheus:latest
        container_name: prometheus
        restart: always
        command:
            - "--config.file=/etc/prometheus/prometheus.yml"
        ports:
            - 8011:9090
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - /mnt/storage/docker/data/prometheus/etc:/etc/prometheus
            - /mnt/storage/docker/data/prometheus/prom_data:/prometheus
    # 1.3 显示图表
    grafana:
        user: root
        image: grafana/grafana:latest
        container_name: grafana
        restart: always
        ports:
            - 8012:3000
        depends_on:
            - prometheus
        environment:
            - GF_SECURITY_ADMIN_USER=admin
            - GF_SECURITY_ADMIN_PASSWORD=admin
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - /mnt/storage/docker/data/grafana/lib:/var/lib/grafana
            - /mnt/storage/docker/data/grafana/datasources:/etc/grafana/provisioning/datasources
  • Prometheus 挂载配置文件及数据目录,需要对数据目录进行创建及赋予权限:mkdir -p /mnt/storage/docker/data/prometheus/prom_data && chown 65534 /mnt/storage/docker/data/prometheus/prom_data 关于持久化权限的讨论
  • Grafana 挂载数据目录 /mnt/storage/docker/data/grafana/lib, 需要赋予权限:mkdir -p /mnt/storage/docker/data/grafana/lib && chown -R 472:472 /mnt/storage/docker/data/grafana/lib
  • Prometheus 在配置文件 /mnt/storage/docker/data/prometheus/etc/prometheus.yml 定义 Node Exporter 的抓取目标

/mnt/storage/docker/data/prometheus/etc/prometheus.yml

# Global config
global:
  scrape_interval: 15s 
  scrape_timeout: 10s
  evaluation_interval: 15s

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:

# Scrape configuration
scrape_configs:
- job_name: prometheus
  static_configs:
    - targets: ["localhost:8011"]

- job_name: 'node-exporter'
  scrape_interval: 15s
  scrape_timeout: 10s
  static_configs:
    - targets: ["192.168.0.114:8010"]