使用 Docker 部署 Prometheus+Grafana 监控平台

10 min read

目录结构

moniter
├── docker-compose.yml
├── grafana
│   └── data
├── prometheus
│   ├── config
│   │   └── prometheus.yml
│   └── data
└── node_exporter
    └── node_exporter

部署 node_exporter

node_exporter 用于采集 VPS 数据并提供给 Prometheus,要先部署它 由于 node_exporter 需要直接与宿主机交互,所以虽然可以部署在容器中,但是官方并不推荐这样做,所以直接部署在宿主机上

wget <https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz>
mv node_exporter-1.0.1.linux-amd64.tar.gz node_exporter.tar.gz
tar zxf node_exporter.tar.gz

有很多方法让它后台运行,为了方便我这里使用 pm2

apt install npm
npm install pm2 -g
pm2 start node_exporter/node_exporter
pm2 save
pm2 startup

部署 Prometheus 和 Grafana

docker-compose.yml

version: "3"
services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
      - ./prometheus/data:/prometheus
    restart: always
    container_name: prometheus
  grafana:
    image: grafana/grafana
    ports:
      - 3000:3000
    volumes:
      - ./grafana/data:/var/lib/grafana
    restart: always
    container_name: grafana

prometheus.yml

global:
  scrape_interval: 5s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets:
          - prometheus:9090
  - job_name: VPS Moniter
    static_configs:
      - targets:
          - 服务器ip:9100
        labels:
          instance: example-host # 名称,用于标记

添加新的监控节点只需要在上面部署 node_exporter,然后在 prometheus.yml 添加一个 targets 就可以了 一切准备就绪,执行 docker-compose up -d,之后打开 ip:3000 就可以看到 grafana 的界面了,默认账号 admin,密码 admin,然后按照下面步骤操作就可以了

在 URL 这一栏填入 http://prometheus:9090