运行以下命令,使用官方 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
:使用官方镜像。在浏览器中访问:http://localhost:9090
Node Exporter 用于采集系统级别的硬件和操作系统指标,例如 CPU、内存、磁盘和网络使用情况。
使用 Docker 快速启动 Node Exporter:
docker run -d \
--name=node-exporter \
-p 9100:9100 \
prom/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
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
)。如果是 Docker 方式运行 Prometheus,请重启容器:
docker restart prometheus
在 Prometheus Web 界面,进入 Status -> Targets,你应该可以看到 node_exporter
目标状态为 UP
。
PromQL(Prometheus Query Language)是 Prometheus 的查询语言,用于提取和分析时间序列数据。以下是几个基础查询示例:
node_cpu_seconds_total
说明: 这条查询会返回所有 CPU 时间序列的最新值,包括不同的 CPU 核心和模式(user
, system
, idle
等)。
node_cpu_seconds_total{mode="user"}
说明: 这条查询会筛选出 mode="user"
的 CPU 时间序列数据,忽略其他模式。
rate(node_cpu_seconds_total[5m])
说明: rate()
计算每秒的增长率,常用于监控 CPU 使用率、网络流量等数据。
node_memory_MemAvailable_bytes
任务描述:
任务描述:
编写告警规则,监控 CPU 使用率,当 5 分钟平均值超过 80% 时触发告警。
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' # 容器内的挂载路径
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:
在添加或修改规则文件后,使用 Prometheus 自带的验证工具:
# 进入到prometheus容器内,然后运行‘promtool’工具
docker-compose exec prometheus sh
promtool check rules /etc/prometheus/rules/alert_rules.yml
SUCCESS
。