当前位置:网站首页 >  教程

十分钟搭建零成本自动化运维监控平台实操手册

时间:2026年06月11日 23:20:40 来源:易频IT社区

环境准备与基础依赖

本指南基于CentOS 7/8系统编写,使用Prometheus作为监控核心,Grafana作为可视化面板,Alertmanager处理告警,Node Exporter采集数据。全栈开源,无商业授权费用。

首先执行系统基础更新与必要的网络端口开放。监控服务需要用到9090(Prometheus)、9100(Node Exporter)、3000(Grafana)、9093(Alertmanager)端口。

 更新系统基础包
yum update -y
关闭防火墙或开放端口(生产环境建议仅开放必要端口)
systemctl stop firewalld
systemctl disable firewalld
临时关闭SELinux(避免权限问题导致服务启动失败)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

第一步:安装Node Exporter数据采集器

Node Exporter负责采集服务器的CPU、内存、磁盘等核心指标。我们需要在被监控的服务器上安装此组件。

1. 下载并安装Node Exporter

 创建用户目录
useradd -rs /bin/false node_exporter
下载最新版二进制包(以v1.6.1为例,实际可替换为最新版本)
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
解压并移动
tar -xvf node_exporter-1.6.1.linux-amd64.tar.gz
mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
chown node_exporter:node_exporter /usr/local/bin/node_exporter

2. 配置Systemd服务管理

创建服务文件以实现开机自启和守护进程管理。

cat > /etc/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOF

3. 启动服务

systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
验证服务状态,确保输出为Active: active (running)
systemctl status node_exporter

第二步:部署Prometheus监控服务

Prometheus是整个监控系统的时序数据库核心,负责拉取和存储数据。

1. 安装Prometheus

 创建用户
useradd -rs /bin/false prometheus
创建必要目录
mkdir -p /etc/prometheus /var/lib/prometheus
chown prometheus:prometheus /etc/prometheus /var/lib/prometheus
下载安装包
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar -xvf prometheus-2.45.0.linux-amd64.tar.gz
mv prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/
mv prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/
chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

2. 配置prometheus.yml

这是Prometheus的核心配置文件,定义了抓取目标和间隔。请直接复制以下完整配置,将localhost替换为你实际的服务器IP(如果监控本机则保持不变)。

cat > /etc/prometheus/prometheus.yml <<'EOF'
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']
EOF

3. 配置Systemd服务

cat > /etc/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
EOF

4. 启动Prometheus

systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus

第三步:安装Grafana可视化面板

Grafana将枯燥的数据转化为直观的图表。

十分钟搭建零成本自动化运维监控平台实操手册

1. 使用YUM源安装

cat > /etc/yum.repos.d/grafana.repo <<'EOF'
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
安装并启动
yum install grafana -y
systemctl enable grafana-server
systemctl start grafana-server

2. 配置数据源与导入仪表盘

访问 http://服务器IP:3000。默认账号密码均为 admin,首次登录会要求修改密码。

操作路径如下:

  • 添加数据源: 点击左侧齿轮图标 (Configuration) -> Data sources -> Add data source -> 选择 Prometheus
  • 在URL处输入 http://localhost:9090,点击底部的 Save & Test,显示绿色Data source is working即成功。
  • 导入监控面板: 点击左侧 + 号 -> Import -> 在Grafana.com Dashboard ID or URL输入框中输入 8919(这是经典的Node Exporter全览面板)。
  • 点击Load,在Prometheus下拉框选择刚才添加的数据源,点击Import。

第四步:配置钉钉告警(低成本通知)

邮件告警常被拦截,钉钉通知是国内运维首选。首先在钉钉群设置中添加“自定义机器人”,获取Webhook地址(安全设置请勾选“自定义关键词”,输入“告警”二字)。

1. 安装Alertmanager

wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
tar -xvf alertmanager-0.25.0.linux-amd64.tar.gz
mv alertmanager-0.25.0.linux-amd64/alertmanager /usr/local/bin/
mv alertmanager-0.25.0.linux-amd64/amtool /usr/local/bin/

2. 配置Alertmanager连接钉钉

创建配置文件,将YOUR_DINGTALK_WEBHOOK_URL替换为你的真实Webhook地址。

mkdir -p /etc/alertmanager
cat > /etc/alertmanager/alertmanager.yml <<'EOF'
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'dingtalk.webhook'
receivers:
- name: 'dingtalk.webhook'
webhook_configs:
- url: 'YOUR_DINGTALK_WEBHOOK_URL'
send_resolved: true
EOF

3. 配置Systemd并启动

cat > /etc/systemd/system/alertmanager.service <<'EOF'
[Unit]
Description=Alertmanager
After=network-online.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/alertmanager \
--config.file=/etc/alertmanager/alertmanager.yml \
--storage.path=/var/lib/prometheus/alertmanager
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable alertmanager
systemctl start alertmanager

第五步:定义告警规则

没有规则,监控系统只是个花瓶。我们需要定义CPU过高、磁盘剩余不足等规则。

1. 创建规则文件

mkdir -p /etc/prometheus/rules
cat > /etc/prometheus/rules/node_alerts.yml <<'EOF'
groups:
- name: node_alerts
rules:
- alert: HighCPUUsage
expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[2m]))  100) > 80
for: 5m
labels:
severity: warning
annotations:
summary: "实例 {{ $labels.instance }} CPU使用率过高"
description: "CPU使用率超过80% (当前值: {{ $value }}%)"
- alert: HighDiskUsage
expr: (1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"}))  100 > 85
for: 5m
labels:
severity: critical
annotations:
summary: "实例 {{ $labels.instance }} 磁盘空间不足"
description: "挂载点 {{ $labels.mountpoint }} 使用率超过85% (当前值: {{ $value }}%)"
EOF

2. 使规则生效

修改Prometheus配置文件加载规则,并重启服务。由于我们在第二步已经配置了rule_files,这里只需重启Prometheus。

chown -R prometheus:prometheus /etc/prometheus
systemctl restart prometheus

至此,一套包含数据采集、存储、可视化展示、钉钉即时告警的完整低成本运维监控系统已搭建完成。当服务器CPU持续5分钟超过80%或磁盘超过85%时,钉钉群将收到包含具体数值的告警消息。

相关推荐

最新

热门

推荐

精选

标签

易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。

Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图