@Docker Compose 部署 Prometheus

news2025/12/14 22:00:20

文章目录

      • Docker Compose 部署 Prometheus
        • 1. 环境准备
        • 2. 配置文件准备
        • 3. 编写 Docker Compose 文件
        • 4. 启动服务
        • 5. 验证部署
        • 6. 常用操作
        • 7. 生产环境增强建议
        • 8. 扩展监控对象

Docker Compose 部署 Prometheus

1. 环境准备
  • 安装 Docker(版本 ≥ 20.10)和 Docker Compose(版本 ≥ 1.29)
  • 创建项目目录:
    mkdir prometheus && cd prometheus
    
2. 配置文件准备
  • 创建 Prometheus 配置文件
    prometheus.yml(基础配置):

    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
          - targets: ["localhost:9090"]  # 监控自身
    
      # 示例:添加 Node Exporter(需额外部署)
      # - job_name: "node"
      #   static_configs:
      #     - targets: ["node-exporter:9100"]
    
  • 创建告警规则文件(可选)
    alerts.yml

    groups:
    - name: example
      rules:
      - alert: InstanceDown
        expr: up == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Instance {{ $labels.instance }} down"
    

    linux_rules.yml

    groups:
    - name: linux-system-rules
      rules:
    
      # CPU 相关规则
      - alert: HighCpuLoad
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High CPU load on {{ $labels.instance }}"
          description: "CPU usage is {{ $value }}% for last 10 minutes"
    
      # 内存相关规则
      - alert: HighMemoryUsage
        expr: (node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes) / node_memory_MemTotal_bytes * 100 > 5  # 修改测试触发告警
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High memory usage on {{ $labels.instance }}"
          description: "Memory usage is {{ $value }}% for last 10 minutes"
    
      # 交换分区规则
      - alert: HighSwapUsage
        expr: (node_memory_SwapTotal_bytes - node_memory_SwapFree_bytes) / node_memory_SwapTotal_bytes * 100 > 50
        for: 15m
        labels:
          severity: warning
        annotations:
          summary: "High swap usage on {{ $labels.instance }}"
          description: "Swap usage is {{ $value }}% for last 15 minutes"
    
      # 磁盘空间规则
      - alert: LowDiskSpace
        expr: (node_filesystem_avail_bytes{mountpoint!~"^(/run|/var/lib/docker).*",fstype!="tmpfs"} / node_filesystem_size_bytes * 100) < 15
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "Low disk space on {{ $labels.instance }} ({{ $labels.mountpoint }})"
          description: "Only {{ $value }}% free space left on {{ $labels.mountpoint }}"
    
      # 磁盘 I/O 规则
      - alert: HighDiskIoLoad
        expr: rate(node_disk_io_time_seconds_total[1m]) * 100 > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High disk I/O load on {{ $labels.instance }} ({{ $labels.device }})"
          description: "Disk I/O load is {{ $value }}% for last 10 minutes"
    
      # 网络相关规则
      - alert: HighNetworkErrors
        expr: increase(node_network_receive_errs_total[5m]) > 10 or increase(node_network_transmit_errs_total[5m]) > 10
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High network errors on {{ $labels.instance }} ({{ $labels.device }})"
          description: "Network errors detected on interface {{ $labels.device }}"
    
      # 系统负载规则
      - alert: HighSystemLoad
        expr: node_load5 / count by(instance)(node_cpu_seconds_total{mode="system"}) > 1.5
        for: 15m
        labels:
          severity: warning
        annotations:
          summary: "High system load on {{ $labels.instance }}"
          description: "5-minute load average is {{ $value }} (relative to CPU count)"
    
      # 节点宕机规则
      - alert: InstanceDown
        expr: up{job="node"} == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Instance {{ $labels.instance }} down"
          description: "{{ $labels.instance }} has been down for more than 5 minutes"
    
      # 文件描述符规则
      - alert: HighFileDescriptorUsage
        expr: node_filefd_allocated / node_filefd_maximum * 100 > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High file descriptor usage on {{ $labels.instance }}"
          description: "File descriptor usage is {{ $value }}% of maximum"
    
    

    windows_rules.yml

    groups:
    - name: windows-system-rules
      rules:
    
      # CPU 相关规则
      - alert: HighCpuUsageWindows
        expr: 100 - (avg by(instance) (rate(windows_cpu_time_total{mode="idle"}[5m])) * 100) > 85
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High CPU usage on {{ $labels.instance }}"
          description: "CPU usage is {{ $value }}% for last 10 minutes"
    
      # 内存相关规则
      - alert: HighMemoryUsageWindows
        expr: (windows_os_physical_memory_total_bytes - windows_os_physical_memory_free_bytes) / windows_os_physical_memory_total_bytes * 100 > 90
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High memory usage on {{ $labels.instance }}"
          description: "Memory usage is {{ $value }}% for last 10 minutes"
    
      # 磁盘空间规则
      - alert: LowDiskSpaceWindows
        expr: (windows_logical_disk_free_bytes / windows_logical_disk_size_bytes * 100) < 95  # 修改测试触发告警
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "Low disk space on {{ $labels.instance }} ({{ $labels.volume }})"
          description: "Only {{ $value }}% free space left on {{ $labels.volume }}"
    
      # 磁盘 I/O 规则
      - alert: HighDiskIoWindows
        expr: rate(windows_logical_disk_read_seconds_total[5m]) * 100 > 80 or rate(windows_logical_disk_write_seconds_total[5m]) * 100 > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High disk I/O on {{ $labels.instance }} ({{ $labels.volume }})"
          description: "Disk I/O utilization is {{ $value }}% for last 10 minutes"
    
      # 服务状态规则
      - alert: CriticalServiceDown
        expr: windows_service_status{status!="running"} == 1
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "Critical service down on {{ $labels.instance }}"
          description: "Service {{ $labels.service }} is not running"
    
      # 系统启动时间规则
      - alert: SystemRebooted
        expr: time() - windows_system_system_up_time > 300
        for: 0m
        labels:
          severity: info
        annotations:
          summary: "System rebooted on {{ $labels.instance }}"
          description: "System was rebooted, uptime is {{ $value }} seconds"
    
      # 网络连接规则
      - alert: HighNetworkUtilizationWindows
        expr: rate(windows_net_bytes_total[5m]) / windows_net_speed_bits * 8 * 100 > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High network utilization on {{ $labels.instance }} ({{ $labels.interface }})"
          description: "Network utilization is {{ $value }}% for last 10 minutes"
    
      # 进程内存泄漏检测
      - alert: ProcessMemoryLeakWindows
        expr: predict_linear(windows_process_private_bytes[1h], 3600) / 1024 / 1024 / 1024 > 2
        for: 30m
        labels:
          severity: warning
        annotations:
          summary: "Possible memory leak in {{ $labels.process }} on {{ $labels.instance }}"
          description: "Process {{ $labels.process }} is predicted to exceed 2GB memory in 1 hour"
    
      # 系统日志错误规则
      - alert: SystemLogErrorsWindows
        expr: rate(windows_event_log_errors_total[5m]) > 5
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High system log errors on {{ $labels.instance }}"
          description: "{{ $value }} errors per second in system logs"
    
    

    linux_recording_rules.yml

    groups:
    - name: linux-recording-rules
      interval: 1m
      rules:
    
      # CPU Usage (兼容多版本Node Exporter)
      - record: instance:node_cpu_usage:rate5m
        expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle",job=~".*"}[5m])) * 100)
    
      # Memory Usage (排除缓存/缓冲区)
      - record: instance:node_memory_usage:ratio
        expr: >
          (node_memory_MemTotal_bytes - node_memory_MemFree_bytes
          - node_memory_Buffers_bytes - node_memory_Cached_bytes)
          / node_memory_MemTotal_bytes * 100
    
      # Disk Space Usage (过滤无效挂载点)
      - record: instance:node_filesystem_usage:ratio
        expr: >
          (node_filesystem_size_bytes{fstype!~"tmpfs|squashfs",mountpoint!~"/run|/snap"}
          - node_filesystem_avail_bytes{fstype!~"tmpfs|squashfs",mountpoint!~"/run|/snap"})
          / node_filesystem_size_bytes{fstype!~"tmpfs|squashfs",mountpoint!~"/run|/snap"} * 100
    
      # Network Traffic (过滤虚拟接口)
      - record: instance:node_network_receive_mbps:rate5m
        expr: sum by(instance)(rate(node_network_receive_bytes_total{device!~"lo|veth.*"}[5m])) * 8 / 1048576
    
      # System Load (标准化)
      - record: instance:node_load_ratio:rate5m
        expr: node_load5 / count by(instance)(node_cpu_seconds_total{mode="system"})
    
    
3. 编写 Docker Compose 文件

docker-compose.yml

version: '3.8'

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - ./alerts.yml:/etc/prometheus/alerts.yml  # 挂载告警规则
      - prometheus-data:/prometheus  # 数据持久化
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'  # 允许热重载配置
    ports:
      - "9090:9090"
    restart: unless-stopped
    networks:
      - monitor-net

  # 可选:添加 Grafana 可视化
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    volumes:
      - grafana-data:/var/lib/grafana
    ports:
      - "3000:3000"
    restart: unless-stopped
    networks:
      - monitor-net

  # 可选:添加 Node Exporter 监控主机
  # node-exporter:
  #   image: prom/node-exporter:latest
  #   container_name: node-exporter
  #   restart: unless-stopped
  #   network_mode: host  # 需主机模式
  #   pid: host
  #   volumes:
  #     - /:/host:ro,rslave
  #   command:
  #     - '--path.rootfs=/host'

volumes:
  prometheus-data:
  grafana-data:

networks:
  monitor-net:
    driver: bridge
4. 启动服务
docker-compose up -d  # 后台启动
5. 验证部署
  • Prometheus UI:访问 http://<服务器IP>:9090
    • 检查 Targets:Status → Targets
    • 查询指标:Graph → 输入 up 查看状态
  • Grafana UI(如部署):http://<服务器IP>:3000(默认账号 admin/admin)
    • 添加 Prometheus 数据源:http://prometheus:9090
6. 常用操作
  • 重载配置(不重启)
    curl -X POST http://localhost:9090/-/reload
    
  • 查看日志
    docker-compose logs -f prometheus
    
  • 停止服务
    docker-compose down
    
  • 备份数据:备份 prometheus-data 卷(默认位置:/var/lib/docker/volumes/...
7. 生产环境增强建议
  1. 安全加固
    • 设置 Prometheus --web.config.file 启用基础认证
    • 限制 Grafana 登录策略
  2. 持久化优化
    volumes:
      prometheus-data:
        driver_opts:
          type: nfs
          o: addr=<nfs_server>,rw
          device: ":/path/to/nfs"
    
  3. 资源限制
    prometheus:
      deploy:
        resources:
          limits:
            cpus: '2'
            memory: 4G
    
  4. 高可用方案
    • 部署多个 Prometheus 实例 + Thanos
    • 使用 Alertmanager 集群
8. 扩展监控对象

修改 prometheus.yml 添加:

# 监控 Docker 容器
- job_name: "docker"
  static_configs:
    - targets: ["docker-host:9323"]  # 需配置 docker daemon 暴露 metrics

# 监控 MySQL
- job_name: "mysql"
  static_configs:
    - targets: ["mysql-exporter:9104"]  # 需部署 mysqld-exporter

:完整配置参考 Prometheus 官方文档

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2393908.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

openppp2 -- 1.0.0.25225 优化多线接入运营商路由调配

本文涉及到的内容&#xff0c;涉及到上个发行版本相关内容&#xff0c;人们在阅读本文之前&#xff0c;建议应当详细阅读上个版本之中的VBGP技术相关的介绍。 openppp2 -- 1.0.0.25196 版本新增的VBGP技术-CSDN博客 我们知道在现代大型的 Internet 网络服务商&#xff0c;很多…

详细到用手撕transformer下半部分

之前我们讨论了如何实现 Transformer 的核心多头注意力机制&#xff0c;那么这期我们来完整地实现整个 Transformer 的编码器和解码器。 Transformer 架构最初由 Vaswani 等人在 2017 年的论文《Attention Is All You Need》中提出&#xff0c;专为序列到序列&#xff08;seq2s…

【Sqoop基础】Sqoop生态集成:与HDFS、Hive、HBase等组件的协同关系深度解析

目录 1 Sqoop概述与大数据生态定位 2 Sqoop与HDFS的深度集成 2.1 技术实现原理 2.2 详细工作流程 2.3 性能优化实践 3 Sqoop与Hive的高效协同 3.1 集成架构设计 3.2 数据类型映射处理 3.3 案例演示 4 Sqoop与HBase的实时集成 4.1 数据模型转换挑战 4.2 详细集成流程…

MySQL + CloudCanal + Iceberg + StarRocks 构建全栈数据服务

简述 在业务数据快速膨胀的今天&#xff0c;企业对 低成本存储 与 实时查询分析能力 的需求愈发迫切。 本文将带你实战构建一条 MySQL 到 Iceberg 的数据链路&#xff0c;借助 CloudCanal 快速完成数据迁移与同步&#xff0c;并使用 StarRocks 完成数据查询等操作&#xff0c…

截屏精灵:轻松截屏,高效编辑

在移动互联网时代&#xff0c;截图已经成为我们日常使用手机时的一项基本操作。无论是记录重要信息、分享有趣内容&#xff0c;还是进行学习和工作&#xff0c;一款好用的截图工具都能极大地提升我们的效率。截屏精灵就是这样一款功能强大、操作简单的截图工具&#xff0c;它不…

【JavaWeb】基本概念、web服务器、Tomcat、HTTP协议

目录 1. 基本概念1.1 基本概念1.2 web应用程序1.3 静态web1.4 动态web 2. web服务器3. tomcat详解3.1 安装3.2 启动3.3 配置3.3.1 配置启动的端口号3.3.2 配置主机的名称3.3.3 其他常用配置项日志配置数据源配置安全配置 3.4 发布一个网站 4. Http协议4.1 什么是http4.2 http的…

云计算Linux Rocky day02(安装Linux系统、设备表示方式、Linux基本操作)

云计算Linux Rocky day02&#xff08;安装Linux系统、设备表示方式、Linux基本操作&#xff09; 目录 云计算Linux Rocky day02&#xff08;安装Linux系统、设备表示方式、Linux基本操作&#xff09;1、虚拟机VMware安装Rocky2、Linux命令行3、Linux Rocky修改字体大小和背景颜…

在 ODROID-H3+ 上安装 Win11 系统

在 ODROID-H3 上安装 Windows 11 系统。 以下是完整的步骤&#xff0c;包括 BIOS 设置、U 盘制作、安装和驱动处理&#xff0c;全程不保留之前的系统数据。 ✅ 准备工作 1. 准备一个 ≥8GB 的 USB 启动盘 用另一台电脑制作 Windows 11 安装盘。 &#x1f449; 推荐工具&…

使用el-input数字校验,输入汉字之后校验取消不掉

先说说复现方式 本来input是只能输入数字的&#xff0c;然后你不小心输入了汉字&#xff0c;触发校验了&#xff0c;然后这时候&#xff0c;你发现校验取消不掉了 就这样了 咋办啊&#xff0c;你一看校验没错啊&#xff0c;各种number啥的也写了,发现没问题啊 <el-inputv…

Docker容器启动失败的常见原因分析

我们在开发部署的时候&#xff0c;用 Docker 打包环境&#xff0c;理论上是“我装好了你就能跑”。但理想很丰满&#xff0c;现实往往一 docker run 下去就翻车了。 今天来盘点一下我实际工作中经常遇到的 Docker 容器启动失败的常见原因&#xff0c;顺便给点 debug 的小技巧&a…

立志成为一名优秀测试开发工程师(第七天)——unittest框架的学习

目录 unittest框架的学习 一、测试类的编写 创建相关测试类cal.py、CountTest.py 二、常见断言方法 使用unittest单元测试框架编写测试用例CountTest.py 注意&#xff1a;执行的时候光标一定要放在括号后面&#xff0c;鼠标右键运行 三、对测试环境的初始化和清除模块…

论坛系统(4)

用户详情 获取用户信息 实现逻辑 ⽤⼾提交请求&#xff0c;服务器根据是否传⼊Id参数决定返回哪个⽤⼾的详情 1. 不传⽤⼾Id&#xff0c;返回当前登录⽤⼾的详情(从session获取) 2. 传⼊⽤⼾Id&#xff0c;返回指定Id的⽤⼾详情(根据用户id去查) 俩种方式获得用户信息 参…

力扣面试150题--二叉树的层平均值

Day 54 题目描述 思路 初次做法&#xff08;笨&#xff09;&#xff1a;使用两个队列&#xff0c;一个队列存放树的节点&#xff0c;一个队列存放对应节点的高度&#xff0c;使用x存放上一个节点&#xff0c;highb存放上一个节点的高度&#xff0c;sum存放当前层的节点值之和…

【Doris入门】Doris初识:分布式分析型数据库的核心价值与架构解析

目录 1 Doris简介与核心价值 2 Doris架构深度解析 2.1 Frontend&#xff08;FE&#xff09;架构 2.2 Backend&#xff08;BE&#xff09;架构 3 Doris核心概念详解 3.1 数据分布模型 3.2 Tablet与Replica 3.3 数据模型 4 Doris关键技术解析 4.1 存储引擎 4.2 查询执…

数据结构与算法学习笔记(Acwing 提高课)----动态规划·区间DP

数据结构与算法学习笔记----动态规划区间DP author: 明月清了个风 first publish time: 2025.5.26 ps⭐️区间DP的特征在于子结构一般是一个子区间上的问题&#xff0c;涉及到的问题也非常多&#xff0c;如环形区间&#xff0c;记录方案数&#xff0c;高精度&#xff0c;二维…

从0到1搭建AI绘画模型:Stable Diffusion微调全流程避坑指南

从0到1搭建AI绘画模型&#xff1a;Stable Diffusion微调全流程避坑指南 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 从0到1搭建AI绘画模型&#xff1a;Stable Diffusion微调全流程避坑指南摘要引言一、数据集构…

从一到无穷大 #46:探讨时序数据库Deduplicate与Compaction的设计权衡

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。 本作品 (李兆龙 博文, 由 李兆龙 创作)&#xff0c;由 李兆龙 确认&#xff0c;转载请注明版权。 文章目录 引言Compaction AlgorithmsCompact Execution Flow Based On VeloxLocalMergeSource的…

vue3 导出excel

需求&#xff1a;导出自带格式的excel表格 1.自定义二维数组格式 导出 全部代码&#xff1a; <el-button click"exportExcel">导出</el-button> const exportExcel () > {const data [[商品, 单价, 数量, 总价],[A, 100, 1.55, { t: n, f: B2*C2…

day024-网络基础-TCP与UDP、DNS

文章目录 1. 李导推荐书籍2. OSI七层模型2.1 传输层2.2 网络层2.2.1 问&#xff1a;两端处于不同局域网的设备怎么网络通信&#xff1f; 2.3 数据链路层2.4 物理层2.5 图解OSI七层模型 3. 数据传输模式3.1 全双工3.2 半双工3.3 单工 4. TCP 3次握手4.1 抓包 5. TCP 4次挥手5.1 …

专场回顾 | 重新定义交互,智能硬件的未来设计

自2022年起&#xff0c;中国智能硬件行业呈现出蓬勃发展的态势&#xff0c;市场规模不断扩大。一个多月前&#xff0c;“小智AI”在短视频平台的爆火将智能硬件带向了大众视野&#xff0c;也意味着智能硬件已不再仅仅停留在概念和技术层面&#xff0c;而是加速迈向实际落地应用…