专栏: 监控 & 可观测性
难度: 入门
标签: Prometheus 监控 Exporter 时序数据库 运维监控


前言

Prometheus 是云原生监控的事实标准。本文从安装到查询,完整走一遍 Prometheus 的核心链路。


一、核心架构

应用/服务
  ↓ 暴露 /metrics 端点(或通过 Exporter)
Prometheus Server(定时 pull 数据)
  ↓ 存储到本地 TSDB
PromQL 查询  →  Grafana 可视化
  ↓ 触发告警规则
AlertManager(告警路由/去重/静默)
  ↓ 企业微信/钉钉/PagerDuty

二、安装 Prometheus

# Docker方式快速启动
docker run -d \
  --name prometheus \
  -p 9090:9090 \
  -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
  -v prometheus_data:/prometheus \
  prom/prometheus:latest \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.retention.time=30d \
  --web.enable-lifecycle    # 允许热加载配置

三、配置文件详解

# /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s        # 采集间隔
  evaluation_interval: 15s    # 规则评估间隔
  external_labels:
    cluster: 'production'     # 附加到所有指标的标签

# 告警规则文件
rule_files:
  - "rules/*.yml"

# 告警接收者
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

# 采集任务
scrape_configs:
  # 采集Prometheus自身
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # 采集Node Exporter(服务器指标)
  - job_name: 'node'
    static_configs:
      - targets:
          - '10.0.0.1:9100'
          - '10.0.0.2:9100'
        labels:
          env: 'production'

  # 通过服务发现采集K8s
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: 'true'
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)

四、Node Exporter 安装

# 在每台需要监控的服务器上安装
docker run -d \
  --name node-exporter \
  --net="host" \
  --pid="host" \
  -v "/:/host:ro,rslave" \
  prom/node-exporter:latest \
  --path.rootfs=/host

# 验证:访问 http://服务器IP:9100/metrics
curl http://localhost:9100/metrics | grep node_cpu

五、常用 PromQL 查询

# 查询CPU使用率(排除idle)
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# 内存使用率
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

# 磁盘使用率
(1 - node_filesystem_free_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes) * 100

# 网络流入速率(bytes/s)
rate(node_network_receive_bytes_total{device="eth0"}[5m])

# HTTP错误率(5xx比例)
sum(rate(http_requests_total{status=~"5.."}[5m])) /
sum(rate(http_requests_total[5m])) * 100

# P99延迟
histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))

六、配置告警规则

# rules/node-alerts.yml
groups:
  - name: node.rules
    rules:
      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "CPU使用率过高"
          description: "节点 {{ $labels.instance }} CPU使用率 {{ $value | printf \"%.1f\" }}% 超过85%,持续5分钟"
      
      - alert: DiskSpaceLow
        expr: (1 - node_filesystem_free_bytes / node_filesystem_size_bytes) * 100 > 90
        for: 10m
        labels:
          severity: critical
        annotations:
          summary: "磁盘空间不足"
          description: "节点 {{ $labels.instance }} 磁盘 {{ $labels.mountpoint }} 使用率 {{ $value | printf \"%.1f\" }}%"

七、热加载配置

# 不重启Prometheus,热加载配置(需要--web.enable-lifecycle)
curl -X POST http://localhost:9090/-/reload

# 验证配置语法
promtool check config /etc/prometheus/prometheus.yml
promtool check rules /etc/prometheus/rules/*.yml

结语: Prometheus 的采集→存储→查询→告警链路清晰,各组件职责明确。掌握这套体系,是搭建完整监控平台的第一步。

更多推荐