一、环境准备(CentOS 7.9)
1.1 基础依赖与用户
安装基础工具
1
| yum install -y wget tar vim firewalld
|
关闭防火墙 / 开放端口(生产建议开放指定端口):
1
| systemctl stop firewalld && systemctl disable firewalld
|
创建专用用户(安全,不登录):
1 2 3 4
| groupadd prometheus useradd -r -g prometheus -M -s /sbin/nologin prometheus groupadd alertmanager useradd -r -g alertmanager -M -s /sbin/nologin alertmanager
|
1.2 端口清单(必须开放)
- Prometheus Server:9090
- Node Exporter:9100
- Alertmanager:9093
- Grafana:3000
二、部署 Prometheus Server
2.1 下载、解压、目录规划
版本(当前稳定版,可替换):
1 2
| VERSION=2.53.0 ARCH=linux-amd64
|
上传 Prometheus 并解压:
1
| tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz
|
移动到标准目录:
1
| mv prometheus-2.53.0.linux-amd64 /usr/local/prometheus
|
创建数据目录并授权:
1 2
| mkdir -p /data/prometheus chown -R prometheus:prometheus /usr/local/prometheus /data/prometheus
|
2.2 核心配置 prometheus.yml
1
| vim /usr/local/prometheus/prometheus.yml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| global: scrape_interval: 15s evaluation_interval: 15s
alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"]
rule_files: - "rules/*.yml"
scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"]
- job_name: "node_exporter" static_configs: - targets: - "localhost:9100" - "192.168.1.52:9100"
|
2.3 创建告警规则目录
1 2
| mkdir -p /usr/local/prometheus/rules vim /usr/local/prometheus/rules/node_rules.yml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| groups: - name: node_alerts rules: - alert: NodeHighCPUUsage expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100) > 80 for: 2m labels: severity: warning annotations: summary: "Instance {{ $labels.instance }} CPU usage high" description: "CPU usage is above 80% for 2 minutes (current: {{ $value }}%)" - alert: NodeHighMemoryUsage expr: 100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100) > 85 for: 2m labels: severity: critical annotations: summary: "Instance {{ $labels.instance }} Memory usage high" description: "Memory usage is above 85% (current: {{ $value }}%)"
|
2.4 配置 systemd 服务
1
| vim /etc/systemd/system/prometheus.service
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network-online.target
[Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path=/data/prometheus \ --storage.tsdb.retention.time=15d \ --web.console.templates=/usr/local/prometheus/consoles \ --web.console.libraries=/usr/local/prometheus/console_libraries Restart=on-failure LimitNOFILE=65535
[Install] WantedBy=multi-user.target
|
2.5 启动并验证
1 2 3 4
| systemctl daemon-reload systemctl start prometheus systemctl enable prometheus systemctl status prometheus
|
访问验证:http://服务器IP:9090 → Status → Targets 看 prometheus 是否 UP
三、部署 Node Exporter(采集主机指标)
所有被监控 Linux 节点都需要安装。
3.1 安装
1 2 3 4 5 6
| VERSION=1.8.2 ARCH=linux-amd64 tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz mv node_exporter-1.8.2.linux-amd64 /usr/local/node_exporter useradd -r -M -s /sbin/nologin node_exporter chown -R node_exporter:node_exporter /usr/local/node_exporter
|
其余节点要放通 9100 端口,否则采集不到数据指标。
3.2 systemd 服务
1
| vim /etc/systemd/system/node_exporter.service
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=Node Exporter After=network.target
[Service] User=node_exporter Group=node_exporter Type=simple ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure
[Install] WantedBy=multi-user.target
|
3.3 启动
1 2 3
| systemctl daemon-reload systemctl start node_exporter systemctl enable node_exporter
|
验证:
1
| curl http://localhost:9100/metrics
|
四、部署 Alertmanager(告警分发)
4.1 安装
1 2 3 4 5 6
| VERSION=0.27.0 ARCH=linux-amd64 tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz mv alertmanager-0.27.0.linux-amd64 /usr/local/alertmanager mkdir -p /data/alertmanager chown -R alertmanager:alertmanager /usr/local/alertmanager /data/alertmanager
|
4.2 配置 alertmanager.yml(邮件 / 钉钉示例)
1
| vim /usr/local/alertmanager/alertmanager.yml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| global: resolve_timeout: 5m
route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1h receiver: 'email'
receivers: - name: 'email' email_configs: - to: '你的接收邮箱' from: '你的发件邮箱' smarthost: 'smtp.xxx.com:587' auth_username: '发件邮箱账号' auth_password: '邮箱授权码' send_resolved: true
inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'instance']
|
4.3 systemd 服务
1
| vim /etc/systemd/system/alertmanager.service
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| [Unit] Description=Alertmanager After=network.target
[Service] User=alertmanager Group=alertmanager Type=simple ExecStart=/usr/local/alertmanager/alertmanager \ --config.file=/usr/local/alertmanager/alertmanager.yml \ --storage.path=/data/alertmanager Restart=on-failure
[Install] WantedBy=multi-user.target
|
4.4 启动
1 2 3
| systemctl daemon-reload systemctl start alertmanager systemctl enable alertmanager
|
验证:http://服务器IP:9093
五、部署 Grafana(可视化)
5.1 安装(官方 YUM 源)
1
| rpm -ivh grafana-enterprise-10.1.0-1.x86_64.rpm
|
5.2 启动
1 2
| systemctl start grafana-server systemctl enable grafana-server
|
访问:http://服务器IP:3000,默认账号密码 admin/admin
六、登录 Grafana
浏览器打开:http://你的服务器IP:3000
第一次登录会让你改密码,你可以直接点 Skip 跳过,也可以设置新密码。
七、配置 Prometheus 数据源(超详细)
- 点击左侧菜单的「Connections」图标(插头样式),在最左侧导航栏的中上部。
- 在展开的子菜单里,点击 Data sources(数据源)
- 点击页面右上角的 Add data source(添加数据源)
- 在搜索框里输入 Prometheus,在结果里点选它
- 进入配置页后,只需要改一个地方:在 Connection 下的 URL 输入框,填:
http://localhost:9090
- 其他所有选项保持默认不动!
- 拉到页面最底部,点击 Save & test,出现绿色提示:Data source is working 就说明配置成功了 ✅
八、导入主机监控面板(ID: 1860)
- 点击页面右上角的「+」号(在搜索框右边)
- 在弹出的菜单里,点击 Import dashboard
- 在 Import via grafana.com 输入框中,填入面板 ID:1860
- 点击右边的 Load 按钮
- 页面下方会出现 Prometheus 数据源的下拉菜单,选择你刚才配置好的数据源
- 点击右下角的 Import,面板自动导入完成
九、完整验证清单(按顺序检查)
1. Prometheus 验证
浏览器打开:http://你的服务器IP:9090,点击顶部菜单的 Status → Targets,检查 prometheus 和 node_exporter 两个任务的状态,都要是 UP 状态。
2. Node Exporter 验证
在服务器上执行命令:
1
| curl http://localhost:9100/metrics
|
如果能返回一长串指标数据,说明 Node Exporter 正常运行。
3. Alertmanager 验证
浏览器打开:http://你的服务器IP:9093,能正常打开 Web 界面,说明服务正常。
4. Grafana 面板验证
回到 Grafana,打开刚导入的 1860 面板:
- 顶部的 instance 下拉框能看到所有主机
- 面板里的 CPU、内存、磁盘、网络等图表都能正常显示数据
如果面板里显示 No data,先去 Prometheus 的 Targets 页面确认 node_exporter 是 UP 状态,再回到 Grafana 刷新页面即可。
十、Docker Compose 快速部署(测试环境推荐)
一键启动全套监控服务,新建 docker-compose.yml 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| version: '3.8' services: prometheus: image: prom/prometheus:v2.53.0 container_name: prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml - ./rules:/etc/prometheus/rules - prometheus_data:/prometheus restart: always
node-exporter: image: prom/node-exporter:v1.8.2 container_name: node-exporter ports: - "9100:9100" volumes: - /proc:/host/proc:ro - /sys:/host/sys:ro - /:/rootfs:ro command: - '--path.procfs=/host/proc' - '--path.sysfs=/host/sys' - '--path.rootfs=/rootfs' restart: always
alertmanager: image: prom/alertmanager:v0.27.0 container_name: alertmanager ports: - "9093:9093" volumes: - ./alertmanager.yml:/etc/alertmanager/alertmanager.yml - alertmanager_data:/alertmanager restart: always
grafana: image: grafana/grafana:11.0.0 container_name: grafana ports: - "3000:3000" volumes: - grafana_data:/var/lib/grafana restart: always
volumes: prometheus_data: alertmanager_data: grafana_data:
|
启动命令:
十一、告警验证:手动触发 + 模拟异常
测试邮件告警(手动触发告警)
方式一:访问 Alertmanager 界面 http://你的主服务器IP:9093,点击页面右上角的 Status → Silences,创建静默规则测试通知。
方式二:使用 amtool 发送测试告警
1 2 3
| /usr/local/alertmanager/amtool \ --alertmanager.url=http://localhost:9093 \ alert add TestAlert instance=localhost:9100 severity=critical summary="测试告警" description="测试邮件发送"
|
前提确认:其余主机已正常接入
- 其余主机上已经安装并运行了 node_exporter(端口 9100 开放)
- Prometheus 的 prometheus.yml 中,node_exporter 任务已经添加该主机 IP,且状态为 UP
- 验证地址:http://主服务器IP:9090/status/targets,查看 node_exporter 对应主机状态
模拟异常:制造 CPU 高负载
使用 stress 工具压测 CPU,模拟高负载场景。
安装 stress 工具:
1 2 3 4 5 6
| yum install -y epel-release yum install -y stress
apt update && apt install -y stress
|
执行压测命令,打满 CPU(持续 300 秒):
1
| stress --cpu $(nproc) --timeout 300
|
确认告警触发与通知送达
查看 Prometheus 告警状态
- 访问 http://主服务器IP:9090/alerts
- 查看 CPU 负载告警,状态从 Pending 变为 Firing
- 核对告警内的主机 IP / 实例信息
查看 Alertmanager 告警接收
- 访问 http://主服务器IP:9093
- 告警列表中找到对应告警,状态为 Active
- 核对 instance 字段为测试主机
检查通知渠道
查看绑定的邮箱 / 钉钉,确认收到告警消息;压测结束、负载恢复后,会收到 Resolved 恢复通知。