Grafana + Prometheus 服务器监控

zheng头像
zheng 2026-07-19 06:35
楼主

为什么要做服务器监控?

手里服务器多了以后,经常会遇到这些问题:网站挂了半天才发现;磁盘满了不知道什么时候满的;CPU 突然飙高找不到原因;流量异常没有告警。

Prometheus + Grafana 是目前最流行的开源监控方案:Prometheus 负责采集和存储指标数据;Grafana 负责可视化展示;Node Exporter 负责采集服务器指标(CPU、内存、磁盘、网络等)。这套组合被几乎所有互联网公司使用,搭建也不复杂。

系统架构

服务器1/2/3 → Node Exporter (采集指标)
         ↓
    Prometheus (存储查询)
         ↓
    Grafana (可视化仪表盘)

Docker Compose 一键部署

version: "3"
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: always
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - ./prometheus/data:/prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--storage.tsdb.retention.time=30d"
      - "--web.enable-lifecycle"

  node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    restart: always
    ports:
      - "9100:9100"
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - "--path.procfs=/host/proc"
      - "--path.sysfs=/host/sys"
      - "--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($|/)"

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    restart: always
    ports:
      - "3001:3000"
    volumes:
      - ./grafana/data:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=change_me_please
      - GF_SERVER_ROOT_URL=https://grafana.yourdomain.com

prometheus.yml 配置文件

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node"
    static_configs:
      - targets: ["node-exporter:9100"]
        labels:
          instance: "main-server"

启动:docker-compose up -d

Grafana 初始化配置

1. 添加 Prometheus 数据源

  1. 访问 http://your-ip:3001,用户名 admin
  2. 左侧菜单,Connections,Data Sources,Add data source
  3. 选择 Prometheus,URL 填 http://prometheus:9090
  4. 点击 Save and Test

2. 导入现成仪表盘

推荐导入 Node Exporter Full(ID: 1860):左侧菜单,Dashboards,New,Import,输入 1860,选择数据源,Import。

瞬间就有专业的服务器监控面板:CPU 使用率曲线、内存使用量、磁盘 IO、网络流量、系统负载、运行时间。

3. 其他推荐 Dashboard

Dashboard ID用途
1860Node Exporter Full(服务器全貌)
16098Node Exporter 精简版
12245Docker 容器监控
14282Nginx 监控

监控多台服务器

在其他服务器上部署 Node Exporter:

docker run -d --name node-exporter --restart always   -p 9100:9100   -v /proc:/host/proc:ro   -v /sys:/host/sys:ro   -v /:/rootfs:ro   prom/node-exporter:latest   --path.procfs=/host/proc   --path.sysfs=/host/sys

在主 Prometheus 的 prometheus.yml 中添加新 target,然后热重载:docker exec prometheus kill -HUP 1

配置告警规则

groups:
  - name: server_alerts
    rules:
      - alert: HighCPUUsage
        expr: 100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "CPU 使用率超过 80%"

      - alert: HighMemoryUsage
        expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 90
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "内存使用率超过 90%"

      - alert: DiskAlmostFull
        expr: (1 - (node_filesystem_avail_bytes / node_filesystem_size_bytes)) * 100 > 85
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "磁盘使用率超过 85%"

Nginx 反向代理

server {
    listen 443 ssl http2;
    server_name grafana.yourdomain.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

常见问题

Q: 数据存多久?
A: 上面配置改成了 30 天。建议配合 Grafana Loki(日志)和 Thanos(长期存储)做完整方案。

Q: 资源占用大吗?
A: 三个容器总内存约 300-500MB。建议至少 2GB 内存的服务器。

Q: 和 Uptime Kuma 有什么区别?
A: Uptime Kuma 只管能不能访问(拨测),Prometheus + Grafana 管运行得怎么样(指标监控)。两者互补,可以一起用。

总结

有了这套监控系统,服务器出问题能第一时间知道,不用等用户来反馈网站打不开了。Grafana 的仪表盘看起来也很专业,截图发朋友圈都能装一波运维大神。

自建闲鱼自动发货系统:Docker 部署全流程,24小时自动回复发卡券 Tailscale 异地组网实战

最新回复

暂无回复,来说两句吧

发表评论

站点总访问量:6152