Skip to content

Docker-based monitoring solution with Prometheus, Grafana, and Node Exporter for system metrics, application monitoring, and real-time visualization.

License

Notifications You must be signed in to change notification settings

zenithyangg/prometheus-grafana-docker-monitoring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker 监控系统

基于 Docker 的完整监控解决方案,使用 Prometheus + Grafana + Node Exporter 实现系统监控、应用监控和可视化展示。

✨ 功能特点

  • 系统指标监控(CPU、内存、磁盘、网络)
  • Nginx 性能监控
  • 多服务器监控支持
  • 实时数据可视化
  • 自定义告警规则
  • 支持远程服务器监控

🚀 系统架构

graph TD
    subgraph 监控目标
        Node_Exporter[Node Exporter]
        Nginx_Service[Nginx 服务]
    end

    subgraph 采集与存储
        Nginx_Exporter[Nginx Exporter]
        Prometheus[Prometheus]
    end

    subgraph 可视化与告警
        Grafana[Grafana]
    end

    Node_Exporter --> Prometheus
    Nginx_Service --> Nginx_Exporter
    Nginx_Exporter --> Prometheus
    Prometheus --> Grafana
Loading

⚡ 快速开始

1. 环境要求

  • Docker
  • Docker Compose
  • 至少 2GB 内存
  • 至少 10GB 磁盘空间

2. 安装步骤

  1. 克隆项目到本地:

    git clone [项目地址]
    cd [项目目录]
  2. 启动所有服务:

    docker-compose up -d
  3. 访问监控服务:

    • Grafana: http://localhost:3000 (默认用户名/密码: admin/pass)
    • Prometheus: http://localhost:9090
    • Nginx: http://localhost:8080

3. 配置说明

Prometheus 配置 (prometheus.yaml)

编辑 prometheus.yaml 文件,添加您的监控目标:

global:
  scrape_interval: 15s # Server 抓取频率
  evaluation_interval: 15s
  external_labels:
    monitor: "my-monitor"

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

  - job_name: "nginx_exporter"
    static_configs:
      - targets: ["nginx-prometheus-exporter:9113"]

  - job_name: "node-exporter"
    static_configs:
      - targets:
        - "node-exporter:9100" # 本地 node-exporter
        - "your-remote-server-ip:9100" # 您的远程服务器 IP

Grafana 配置

  1. 添加数据源

    • 类型:Prometheus
    • URL:http://prometheus:9090
    • Access:Server (default)
  2. 导入仪表板

    • 仪表板 ID:11704 (推荐:Node Exporter for Prometheus Dashboard)
    • 数据源:选择您刚刚添加的 Prometheus 数据源
    • 变量配置(如果仪表板需要):
      • job: label_values(up, job)
      • nodename: label_values(node_uname_info{job="$job"}, nodename)
      • instance: label_values(node_uname_info{job="$job", nodename="$nodename"}, instance)

告警配置

Grafana 允许您为监控指标设置告警规则,并通过各种通知渠道接收告警信息。

1. 配置告警通知渠道

在设置具体的告警规则之前,您需要配置告警通知渠道,例如 Discord。

  1. 在 Grafana UI 中,导航到 "Alerting" -> "Notification channels"。
  2. 点击 "New channel" 创建一个新的通知渠道,或编辑现有渠道。
  3. 以 Discord 为例,进行如下配置:
    • Name: discord (推荐使用简洁明了的名称)
    • Type: Discord
    • Webhook URL: https://discord.com/api/webhooks/your_webhook_id/your_webhook_token (⚠️ 请务必替换为您实际的 Discord Webhook URL。)
    • Optional Discord settings: (可选配置,点击展开可设置 Username, Avatar URL, Message, Embeds 等,用于自定义 Discord 消息的外观和内容。)
    • Notification settings: (可选配置,点击展开可设置 Include alerts in message, Send on all alert resolutions, Disable resolve message, Send reminder, Upload image 等,用于控制通知行为。)
  4. 点击 "Send Test" 按钮验证通知是否能正常发送。
  5. 点击 "Save" 保存您的通知渠道配置。
2. 配置告警规则

配置好通知渠道后,您可以为重要的系统指标创建告警规则。

  1. 创建告警规则:

    • 在 Grafana UI 中,导航到 "Alerting" -> "Alert rules",然后点击 "New alert rule"。
    • 或者,在仪表板中找到您希望设置告警的面板,点击面板标题,选择 "Edit",然后切换到 "Alert" 标签页,点击 "Create alert"。
  2. 设置告警条件与阈值: 对于每个告警规则,您需要定义 PromQL 查询、阈值和触发条件。

    • CPU 使用率告警

      • 告警条件: 当 CPU 使用率持续超过 80% 时触发告警。
      • PromQL 表达式:
        100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        
      • 告警级别: 可以设置为 WarningCritical
      • 配置间隔: 设置评估间隔 (例如:Every 1m) 和持续时间 (例如:for 5m),确保指标持续一段时间达到阈值才触发。
    • 内存使用率告警

      • 告警条件: 当内存使用率持续超过 90% 时触发告警。
      • PromQL 表达式:
        (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 90
        
      • 告警级别: WarningCritical
      • 配置间隔: 设置评估间隔和持续时间。
    • 磁盘使用率告警

      • 告警条件: 当根分区 / 的磁盘使用率持续超过 85% 时触发告警。
      • PromQL 表达式:
        (node_filesystem_size_bytes{mountpoint="/"} - node_filesystem_free_bytes{mountpoint="/"}) / node_filesystem_size_bytes{mountpoint="/"} * 100 > 85
        
      • 告警级别: WarningCritical
      • 配置间隔: 设置评估间隔和持续时间。
    • 系统负载告警

      • 告警条件: 当 1 分钟平均系统负载 (node_load1) 持续超过 CPU 核心数时触发告警。
      • PromQL 表达式:
        node_load1 > count(node_cpu_seconds_total{mode="idle"})
        
      • 告警级别: WarningCritical
      • 配置间隔: 设置评估间隔和持续时间。
  3. 关联通知渠道: 在每个告警规则的配置中,选择您在上述步骤 "1. 配置告警通知渠道" 中设置的通知渠道 (例如 discord),以便在告警触发时接收通知。

  4. 保存告警规则: 配置完成后,点击 "Save" 保存您的告警规则。

📊 监控指标

系统指标

以下是您可以在 Prometheus 或 Grafana 中观察的关键系统指标:

  • CPU 使用率
    100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
    
  • 内存使用情况
    node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes
    
  • 磁盘使用情况
    node_filesystem_size_bytes{mountpoint="/"} - node_filesystem_free_bytes{mountpoint="/"}
    
  • 网络流量
    rate(node_network_receive_bytes_total[5m])
    rate(node_network_transmit_bytes_total[5m])
    
  • 系统负载
    node_load1
    node_load5
    node_load15
    

Nginx 指标

以下是您可以在 Prometheus 或 Grafana 中观察的关键 Nginx 指标:

  • 请求数
    nginx_http_requests_total
    
  • 响应时间
    nginx_http_request_duration_seconds
    
  • 错误率
    nginx_http_requests_total{status=~"5.."}
    
  • 连接数
    nginx_http_connections
    

🧪 测试指标变化

要测试 Node Exporter 的指标变化,可以在目标服务器上运行以下命令:

  1. CPU 负载测试

    stress --cpu 4 --timeout 60  # 模拟 4 个 CPU 核心 100% 负载,持续 60 秒
  2. 内存使用测试

    stress --vm 2 --vm-bytes 1G --timeout 60  # 分配 2GB 内存,持续 60 秒

    注意:如果服务器内存资源有限,stress 命令可能会因内存不足(OOM Killer)而被终止。此时可以尝试分配更小的内存量,例如 --vm-bytes 512M--vm-bytes 256M

  3. 磁盘 I/O 测试

    dd if=/dev/zero of=/tmp/testfile bs=1G count=1  # 写入 1GB 文件
  4. 网络测试 (需要安装 iperf3):

    • 在目标服务器上启动 iperf3 服务器:
      iperf3 -s
    • 在另一台机器上进行网络测试:
      iperf3 -c [目标服务器IP]
  5. 文件系统测试

    for i in {1..1000}; do
        dd if=/dev/zero of=/tmp/test$i bs=1M count=10
    done
  6. 进程测试

    for i in {1..100}; do
        sleep 1 &
    done

观察指标

在 Prometheus UI (http://localhost:9090) 或 Grafana 中观察以下指标的变化:

  1. CPU 相关
    • node_cpu_seconds_total
    • node_load1, node_load5, node_load15
  2. 内存相关
    • node_memory_MemTotal_bytes, node_memory_MemAvailable_bytes
    • node_memory_Buffers_bytes, node_memory_Cached_bytes
  3. 磁盘相关
    • node_disk_read_bytes_total, node_disk_written_bytes_total
    • node_disk_io_time_seconds_total
  4. 网络相关
    • node_network_receive_bytes_total, node_network_transmit_bytes_total
  5. 系统相关
    • node_procs_running, node_procs_blocked
    • node_context_switches_total

📂 目录结构

.
├── docker-compose.yaml    # Docker 编排配置
├── prometheus.yaml        # Prometheus 配置
├── nginx/                 # Nginx 配置目录
│   ├── Dockerfile
│   └── status.conf
├── prometheus_data/       # Prometheus 数据存储
└── grafana_data/          # Grafana 数据存储

🧙‍♂️ Docker Compose 配置

version: '3.8'
services:
  nginx:
    build: ./nginx/
    container_name: nginx
    ports:
      - 8080:8080
    networks:
      - monitoring

  nginx-prometheus-exporter:
    image: nginx/nginx-prometheus-exporter:0.10
    container_name: nginx-prometheus-exporter
    command: -nginx.scrape-uri http://nginx:8080/stub_status
    ports:
      - 9113:9113
    depends_on:
      - nginx
    networks:
      - monitoring

  prometheus:
    image: prom/prometheus:v2.35.0
    container_name: prometheus
    volumes:
      - ./prometheus.yaml:/etc/prometheus/prometheus.yaml
      - ./prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yaml'
    ports:
      - '9090:9090'
    networks:
      - monitoring

  renderer:
    image: grafana/grafana-image-renderer:3.4.2
    platform: linux/amd64
    environment:
      BROWSER_TZ: Asia/Taipei
    ports:
      - '8081:8081'
    networks:
      - monitoring

  grafana:
    image: grafana/grafana:8.2.5
    container_name: grafana
    volumes:
      - ./grafana_data:/var/lib/grafana
    environment:
      GF_SECURITY_ADMIN_PASSWORD: pass
      GF_RENDERING_SERVER_URL: http://renderer:8081/render
      GF_RENDERING_CALLBACK_URL: http://grafana:3000/
      GF_LOG_FILTERS: rendering:debug
    depends_on:
      - prometheus
      - renderer
    ports:
      - '3000:3000'
    networks:
      - monitoring

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

networks:
  monitoring:
    driver: bridge

📋 常用命令

# 启动所有服务
docker-compose up -d

# 停止所有服务
docker-compose down

# 查看所有服务日志 (实时)
docker-compose logs -f

# 重启所有服务
docker-compose restart

# 查看服务状态
docker-compose ps

# 查看特定服务日志 (例如:grafana)
docker-compose logs -f grafana
docker-compose logs -f node-exporter
docker-compose logs -f prometheus

💻 添加远程服务器监控

  1. 在远程服务器上安装 Node Exporter

    # 下载 Node Exporter
    wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
    
    # 解压
    tar xvfz node_exporter-1.6.1.linux-amd64.tar.gz
    
    # 移动到系统目录
    sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
    
    # 创建系统服务文件
    sudo nano /etc/systemd/system/node_exporter.service
  2. 创建服务文件 (/etc/systemd/system/node_exporter.service)

    [Unit]
    Description=Node Exporter
    After=network-online.target
    
    [Service]
    User=node_exporter
    Group=node_exporter
    Type=simple
    ExecStart=/usr/local/bin/node_exporter
    
    [Install]
    WantedBy=multi-user.target
  3. 启动服务

    sudo systemctl daemon-reload
    sudo systemctl start node_exporter
    sudo systemctl enable node_exporter
  4. prometheus.yaml 中添加远程服务器目标

      - job_name: 'node-exporter'
        static_configs:
          - targets: ['remote-server-ip:9100']

�� 常见问题

  1. Grafana 无法连接 Prometheus

    • 检查 Prometheus 服务是否正常运行
    • 确认数据源 URL 配置正确
    • 检查网络连接
  2. Node Exporter 无法采集数据

    • 检查 Node Exporter 服务状态
    • 确认端口是否开放
    • 检查防火墙设置
  3. 仪表板无数据

    • 检查时间范围设置
    • 确认变量选择正确
    • 验证查询语句
    • 检查数据源连接
  4. 容器无法启动

    • 检查端口占用
    • 确认目录权限
    • 查看容器日志

🔧 维护建议

  1. 定期备份数据

    • 备份 Prometheus 数据
    • 备份 Grafana 配置
    • 备份告警规则
  2. 监控磁盘使用情况

    • 设置磁盘告警
    • 定期清理旧数据
    • 配置数据保留策略
  3. 及时更新组件版本

    • 关注安全更新
    • 测试新版本兼容性
    • 制定更新计划
  4. 定期检查告警规则

    • 验证告警有效性
    • 调整告警阈值
    • 优化告警通知

🤝 贡献指南

  1. Fork 项目
  2. 创建特性分支
  3. 提交更改
  4. 发起 Pull Request

�� 许可证

MIT License

About

Docker-based monitoring solution with Prometheus, Grafana, and Node Exporter for system metrics, application monitoring, and real-time visualization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published