搭建 Prometheus 环境

时间:2025-05-04 11:25:49
  • Prometheus 实战教程 —— 阶段 1

第 1 步:搭建 Prometheus 环境

方法 1:使用 Docker 快速搭建(推荐)

1.1 启动 Prometheus 容器

运行以下命令,使用官方 Docker 镜像启动 Prometheus:

docker run -d \
  --name=prometheus \
  -p 9090:9090 \
  -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus

解释:

  • -p 9090:9090:将 Prometheus 的默认端口 9090 映射到本地。
  • -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml:挂载自定义的配置文件。
  • prom/prometheus:使用官方镜像。
1.2 访问 Prometheus Web 界面

在浏览器中访问:http://localhost:9090


第 2 步:配置 Node Exporter 采集系统指标

Node Exporter 用于采集系统级别的硬件和操作系统指标,例如 CPU、内存、磁盘和网络使用情况。

2.1 安装 Node Exporter

使用 Docker 快速启动 Node Exporter:

docker run -d \
  --name=node-exporter \
  -p 9100:9100 \
  prom/node-exporter

2.2 验证 Node Exporter 是否工作

在浏览器中访问:http://localhost:9100/metrics 你会看到类似以下内容:

# HELP node_cpu_seconds_total Seconds the CPUs spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="user"} 12345.678
node_cpu_seconds_total{cpu="0",mode="system"} 543.21

第 3 步:配置 Prometheus 采集 Node Exporter 数据

3.1 修改 Prometheus 配置文件 (prometheus.yml)

创建或修改 prometheus.yml,添加 scrape_configs 部分,让 Prometheus 采集 Node Exporter 的数据。

global:
  scrape_interval: 15s   # 每 15 秒采集一次数据
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']

解释:

  • scrape_interval:设置 Prometheus 采集数据的频率,默认 15 秒。
  • job_name:定义任务名称为 node_exporter
  • targets:设置 Node Exporter 的地址(localhost:9100)。

3.2 重新启动 Prometheus

如果是 Docker 方式运行 Prometheus,请重启容器:

docker restart prometheus

3.3 验证配置

在 Prometheus Web 界面,进入 Status -> Targets,你应该可以看到 node_exporter 目标状态为 UP


第 4 步:使用 PromQL 查询数据

PromQL(Prometheus Query Language)是 Prometheus 的查询语言,用于提取和分析时间序列数据。以下是几个基础查询示例:

4.1 查询最新的 CPU 使用数据

node_cpu_seconds_total

说明: 这条查询会返回所有 CPU 时间序列的最新值,包括不同的 CPU 核心和模式(user, system, idle 等)。


4.2 查询某个模式下的 CPU 使用数据

node_cpu_seconds_total{mode="user"}

说明: 这条查询会筛选出 mode="user" 的 CPU 时间序列数据,忽略其他模式。


4.3 计算 5 分钟内的 CPU 使用率

rate(node_cpu_seconds_total[5m])

说明: rate() 计算每秒的增长率,常用于监控 CPU 使用率、网络流量等数据。


4.4 查询最近 1 小时的内存使用数据

node_memory_MemAvailable_bytes

第 5 步:实战任务 1——搭建监控并查询 CPU 使用情况

任务描述:

  1. 搭建 Prometheus 和 Node Exporter 采集主机的 CPU 和内存使用情况。
  2. 使用 PromQL 查询最近 5 分钟的 CPU 使用率,并分析内存的可用空间。

第 6 步:实战任务 2——编写简单的告警规则

任务描述:
编写告警规则,监控 CPU 使用率,当 5 分钟平均值超过 80% 时触发告警。

6.1 告警规则配置 (prometheus.yml)

prometheus.yml 文件中添加如下告警规则:

rule_files:
  - 'alert.rules.yml'  # 引用外部告警规则文件

创建 alert.rules.yml 文件:

groups:
  - name: cpu_alerts
    rules:
      - alert: HighCPUUsage
        expr: rate(node_cpu_seconds_total{mode="user"}[5m]) > 0.8
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: "CPU 使用率高"
          description: "CPU 使用率在过去 5 分钟内持续超过 80%"

eg:

cat prometheus/prometheus.yml
global:
  scrape_interval: 15s   # 每 15 秒抓取一次数据
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

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

  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']

  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

  - job_name: 'pushgateway'
    static_configs:
      - targets: ['pushgateway:9091']
rule_files:
  - '/etc/prometheus/alert_rules.yml'   # 容器内的挂载路径

**6.2 加载告警规则文件

docker方式

docker run -d \
  --name=prometheus \
  -p 9090:9090 \
  -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
  -v /path/to/alert.rules.yml:/etc/prometheus/alert.rules.yml \
  harbor.fq.com/prometheus/prometheus:v3.1.0

docker-compose 修改docker-compose.yaml配置文件 ,添加挂载路径 “- /opt/monitoring/rules/alert_rules.yml:/etc/prometheus/alert_rules.yml:ro”

cat docker-compose.yaml
#version: '3.8'

services:
  prometheus:
    image: harbor.fq.com/prometheus/prometheus:v3.1.0
    container_name: prometheus
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - /opt/monitoring/rules/alert_rules.yml:/etc/prometheus/alert_rules.yml:ro
      - prometheus-data:/prometheus       # 持久化存储
    ports:
      - "9090:9090"
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
    restart: always

  alertmanager:
    image: harbor.fq.com/prometheus/alertmanager:v0.27.0
    container_name: alertmanager
    volumes:
      - ./alertmanager/alertmanager.yml:/etc/alertmanager/config.yml:ro
    ports:
      - "9093:9093"
    restart: always

  grafana:
    image: harbor.fq.com/prometheus/grafana:9.5.3
    container_name: grafana
    volumes:
      - grafana-data:/var/lib/grafana      # Grafana 持久化存储
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    restart: always

  node-exporter:
    image: harbor.fq.com/prometheus/node-exporter:v1.8.2
    container_name: node-exporter
    ports:
      - "9100:9100"
    restart: always

  cadvisor:
    image: harbor.fq.com/prometheus/cadvisor:v0.33.0
    container_name: cadvisor
    ports:
      - "8080:8080"
    restart: always

  pushgateway:
    image: harbor.fq.com/prometheus/pushgateway:1.11.0
    container_name: pushgateway
    ports:
      - "9091:9091"
    restart: always

volumes:
  prometheus-data:
  grafana-data:

6.3 验证告警规则

在添加或修改规则文件后,使用 Prometheus 自带的验证工具:

# 进入到prometheus容器内,然后运行‘promtool’工具
 docker-compose exec  prometheus sh


promtool check rules /etc/prometheus/rules/alert_rules.yml