在Linux环境中搭建高可用系统监控,核心目标是避免单点故障导致监控服务不可用,确保系统运行状态能够被持续采集和展示。通常我们会选择成熟的开源组件组合实现这一需求,整体方案采用Prometheus负责指标采集存储,keepalived实现主备节点高可用,Grafana提供可视化面板。

环境准备
本次搭建需要两台配置相近的Linux服务器,系统选择CentOS 7或者Ubuntu 20.04均可,两台服务器需要处于同一局域网段,且相互之间网络互通。同时需要准备以下软件包:
- Prometheus 2.40.0及以上版本
- keepalived 2.2.0及以上版本
- Grafana 9.0.0及以上版本
- node_exporter 1.5.0及以上版本,用于采集系统指标
两台服务器分别作为主节点和备节点,假设主节点IP为192.168.0.10,备节点IP为192.168.0.11,虚拟IP(VIP)设置为192.168.0.100,后续监控服务都通过VIP访问。
部署基础监控组件
安装node_exporter
在两台节点上都执行以下操作,首先创建运行用户:
# 创建prometheus用户 useradd -s /sbin/nologin prometheus # 下载node_exporter安装包 wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz # 解压安装包 tar -zxvf node_exporter-1.5.0.linux-amd64.tar.gz -C /usr/local/ mv /usr/local/node_exporter-1.5.0.linux-amd64 /usr/local/node_exporter # 修改目录属主 chown -R prometheus:prometheus /usr/local/node_exporter
然后创建systemd服务文件,方便管理node_exporter服务:
cat > /etc/systemd/system/node_exporter.service << EOF [Unit] Description=Node Exporter After=network.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/node_exporter/node_exporter [Install] WantedBy=multi-user.target EOF
启动并设置开机自启:
systemctl daemon-reload systemctl start node_exporter systemctl enable node_exporter
安装Prometheus
同样在两台节点上执行安装操作:
# 下载Prometheus安装包 wget https://github.com/prometheus/prometheus/releases/download/v2.40.0/prometheus-2.40.0.linux-amd64.tar.gz # 解压安装包 tar -zxvf prometheus-2.40.0.linux-amd64.tar.gz -C /usr/local/ mv /usr/local/prometheus-2.40.0.linux-amd64 /usr/local/prometheus # 修改目录属主 chown -R prometheus:prometheus /usr/local/prometheus
修改Prometheus配置文件,添加node_exporter的采集任务:
# 编辑/usr/local/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node_exporter"
static_configs:
# 这里先填写两台节点的IP,后续高可用配置会自动切换
- targets: ["192.168.0.10:9100", "192.168.0.11:9100"]
创建Prometheus的systemd服务文件:
cat > /etc/systemd/system/prometheus.service << EOF [Unit] Description=Prometheus After=network.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data [Install] WantedBy=multi-user.target EOF
先不要启动Prometheus服务,等待keepalived配置完成后再启动。
安装Grafana
在两台节点上安装Grafana,以CentOS为例:
# 添加Grafana 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 # 安装Grafana yum install grafana -y # 启动并设置开机自启 systemctl start grafana-server systemctl enable grafana-server
Grafana默认端口为3000,后续通过VIP的3000端口访问即可。
配置keepalived实现高可用
keepalived通过VRRP协议实现主备节点的状态检测和VIP漂移,当主节点故障时自动将VIP切换到备节点,保证服务不中断。
安装keepalived
两台节点都执行安装命令:
# CentOS安装 yum install keepalived -y # Ubuntu安装 apt install keepalived -y
主节点配置
编辑主节点的keepalived配置文件/etc/keepalived/keepalived.conf:
global_defs {
router_id node1 # 节点标识,备节点需要修改
}
vrrp_script check_prometheus {
script "/usr/local/bin/check_prometheus.sh" # 检查Prometheus服务状态的脚本
interval 2 # 检查间隔2秒
weight -20 # 检查失败权重减20
}
vrrp_instance VI_1 {
state MASTER # 主节点为MASTER
interface eth0 # 网卡名称,根据实际环境修改
virtual_router_id 51 # 虚拟路由ID,主备节点需要一致
priority 100 # 优先级,主节点高于备节点
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 # 认证密码,主备节点需要一致
}
virtual_ipaddress {
192.168.0.100/24 # 虚拟IP
}
track_script {
check_prometheus # 关联检查脚本
}
}
创建Prometheus服务检查脚本/usr/local/bin/check_prometheus.sh:
#!/bin/bash # 检查Prometheus服务是否运行 if ! systemctl is-active --quiet prometheus; then exit 1 fi # 检查Prometheus端口是否正常监听 if ! netstat -tln | grep -q ":9090"; then exit 1 fi exit 0
给脚本添加执行权限:
chmod +x /usr/local/bin/check_prometheus.sh
备节点配置
备节点的keepalived配置文件修改以下内容:
global_defs {
router_id node2 # 备节点标识
}
vrrp_instance VI_1 {
state BACKUP # 备节点为BACKUP
priority 90 # 优先级低于主节点
# 其余配置和主节点保持一致
}
检查脚本和主节点完全相同,同样需要创建并赋予执行权限。
启动服务
先启动主节点的keepalived和Prometheus:
systemctl start keepalived systemctl start prometheus systemctl enable keepalived systemctl enable prometheus
再启动备节点的keepalived和Prometheus:
systemctl start keepalived systemctl start prometheus systemctl enable keepalived systemctl enable prometheus
此时可以通过ip addr show eth0命令在主节点上看到VIP 192.168.0.100已经绑定到网卡上。
配置Grafana可视化
打开浏览器访问http://192.168.0.100:3000,默认用户名和密码都是admin,首次登录需要修改密码。
添加Prometheus数据源,数据源地址填写http://192.168.0.100:9090,点击保存测试,确认连接正常。
然后导入node_exporter的官方监控面板,面板ID为1860,选择对应的Prometheus数据源,即可看到系统的CPU、内存、磁盘、网络等指标的实时展示。
高可用验证
手动停止主节点的Prometheus服务,模拟主节点故障:
systemctl stop prometheus
此时可以看到主节点的VIP会自动漂移到备节点,通过VIP访问Prometheus和Grafana服务仍然正常,监控数据不会中断。当主节点恢复后,VIP会自动切回主节点,备节点恢复备用状态。
注意事项
- 两台节点的Prometheus数据不会自动同步,如果需要历史数据一致,可以额外配置Prometheus的远程存储,比如对接InfluxDB或者VictoriaMetrics。
- keepalived的检查脚本需要根据实际服务情况调整,避免误判导致VIP频繁切换。
- 防火墙需要开放对应的端口:Prometheus 9090、node_exporter 9100、Grafana 3000、VRRP协议对应的112端口。
- 如果服务器有多块网卡,需要确认keepalived配置的网卡名称和实际使用的网卡一致。
Linux高可用系统监控Prometheuskeepalived修改时间:2026-07-16 02:00:49