Prometheus+Alertmanager+node_exporter+grafana高可用

Prometheus 高可用架构搭建实战(3 节点集群)

markdown 复制代码
## 📌 前言

在生产环境中,Prometheus 作为核心监控系统,一旦单点故障将导致监控中断。本文通过 3 台虚拟机搭建一套高可用的 Prometheus 监控架构,实现监控数据的冗余采集和告警的高可用。

**环境信息**:

| 角色 | 主机名 | IP 地址 |
|------|--------|---------|
| 主节点 | prometheus-01 | 192.168.228.131 |
| 从节点 | prometheus-02 | 192.168.228.132 |
| 从节点 | prometheus-03 | 192.168.228.133 |

---

## 📦 一、环境准备

### 1.1 设置主机名(三台均需执行)

```bash
# 131 节点
hostnamectl set-hostname prometheus-01

# 132 节点
hostnamectl set-hostname prometheus-02

# 133 节点
hostnamectl set-hostname prometheus-03

1.2 配置 hosts 解析(三台均需执行)

bash 复制代码
cat >> /etc/hosts << EOF
192.168.228.131 prometheus-01
192.168.228.132 prometheus-02
192.168.228.133 prometheus-03
EOF

1.3 关闭防火墙和 SELinux(三台均需执行)

bash 复制代码
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

1.4 时间同步(三台均需执行)

bash 复制代码
yum install -y chrony
systemctl start chronyd
systemctl enable chronyd
chronyc sources

🚀 二、部署 Prometheus(三台均需安装)

2.1 下载并解压

bash 复制代码
cd /opt
wget https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
tar -xzf prometheus-2.52.0.linux-amd64.tar.gz
mv prometheus-2.52.0.linux-amd64 /usr/local/prometheus

2.2 创建 Prometheus 用户

bash 复制代码
useradd -M -s /sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/prometheus

2.3 配置 Prometheus(各节点需修改 replica 标签)

在 131 节点 ,编辑 /usr/local/prometheus/prometheus.yml:

yaml 复制代码
global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    replica: 'prometheus-A'  # 132 节点改为 prometheus-B,133 节点改为 prometheus-C

alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - '192.168.228.131:9093'
            - '192.168.228.132:9093'
            - '192.168.228.133:9093'

rule_files:
  - "rules/*.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets:
          - '192.168.228.131:9100'
          - '192.168.228.132:9100'
          - '192.168.228.133:9100'

⚠️ 注意 :三台 Prometheus 的 scrape_configs 完全相同,仅 external_labels.replica 不同,用于区分数据来源。

2.4 创建 systemd 服务文件

bash 复制代码
cat > /etc/systemd/system/prometheus.service << 'EOF'
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/usr/local/prometheus/data \
  --web.listen-address=:9090 \
  --web.enable-lifecycle

[Install]
WantedBy=multi-user.target
EOF

2.5 启动 Prometheus(三台均需执行)

bash 复制代码
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus

验证:访问 http://192.168.228.131:9090 查看是否正常。


📊 三、部署 Node Exporter(三台均需安装)

3.1 下载并解压

bash 复制代码
cd /opt
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.0/node_exporter-1.8.0.linux-amd64.tar.gz
tar -xzf node_exporter-1.8.0.linux-amd64.tar.gz
mv node_exporter-1.8.0.linux-amd64 /usr/local/node_exporter

3.2 创建 systemd 服务文件

bash 复制代码
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 \
  --web.listen-address=:9100 \
  --collector.systemd

[Install]
WantedBy=multi-user.target
EOF

3.3 启动 Node Exporter(三台均需执行)

bash 复制代码
chown -R prometheus:prometheus /usr/local/node_exporter
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
systemctl status node_exporter

🚨 四、部署 Alertmanager 集群(三台均需安装)

4.1 下载并解压

bash 复制代码
cd /opt
wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar -xzf alertmanager-0.27.0.linux-amd64.tar.gz
mv alertmanager-0.27.0.linux-amd64 /usr/local/alertmanager
chown -R prometheus:prometheus /usr/local/alertmanager

4.2 配置 Alertmanager

编辑 /usr/local/alertmanager/alertmanager.yml(三台配置相同):

yaml 复制代码
global:
  resolve_timeout: 5m

route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  receiver: 'default-receiver'

receivers:
  - name: 'default-receiver'
    webhook_configs:
      - url: 'http://your-webhook-url'  # 替换为实际告警接收地址

4.3 创建 systemd 服务文件(各节点不同)

131 节点:

bash 复制代码
cat > /etc/systemd/system/alertmanager.service << 'EOF'
[Unit]
Description=Alertmanager Server
After=network.target

[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/alertmanager/alertmanager \
  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data \
  --web.listen-address=:9093 \
  --cluster.listen-address=192.168.228.131:9094 \
  --cluster.advertise-address=192.168.228.131:9094

[Install]
WantedBy=multi-user.target
EOF

132 节点:

bash 复制代码
cat > /etc/systemd/system/alertmanager.service << 'EOF'
[Unit]
Description=Alertmanager Server
After=network.target

[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/alertmanager/alertmanager \
  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data \
  --web.listen-address=:9093 \
  --cluster.listen-address=192.168.228.132:9094 \
  --cluster.advertise-address=192.168.228.132:9094 \
  --cluster.peer=192.168.228.131:9094

[Install]
WantedBy=multi-user.target
EOF

133 节点:

bash 复制代码
cat > /etc/systemd/system/alertmanager.service << 'EOF'
[Unit]
Description=Alertmanager Server
After=network.target

[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/alertmanager/alertmanager \
  --config.file=/usr/local/alertmanager/alertmanager.yml \
  --storage.path=/usr/local/alertmanager/data \
  --web.listen-address=:9093 \
  --cluster.listen-address=192.168.228.133:9094 \
  --cluster.advertise-address=192.168.228.133:9094 \
  --cluster.peer=192.168.228.131:9094

[Install]
WantedBy=multi-user.target
EOF

4.4 启动 Alertmanager(三台均需执行)

bash 复制代码
systemctl daemon-reload
systemctl start alertmanager
systemctl enable alertmanager
systemctl status alertmanager

验证集群状态:访问任意节点 http://<IP>:9093/#/status,查看 Cluster 状态是否显示所有节点。


📈 五、部署 Grafana(在 131 主节点安装)

5.1 安装 Grafana

bash 复制代码
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 -y grafana

5.2 启动 Grafana

bash 复制代码
systemctl daemon-reload
systemctl start grafana-server
systemctl enable grafana-server
systemctl status grafana-server

5.3 配置 Grafana 数据源

  1. 访问 http://192.168.228.131:3000,默认账号密码为 admin/admin
  2. 点击 Configuration → Data Sources → Add data source
  3. 选择 Prometheus
  4. URL 填写 http://192.168.228.131:9090(可配置多个 Prometheus 实现数据源高可用)
  5. 点击 Save & Test

🔄 六、架构验证

6.1 验证 Prometheus 采集

访问 http://192.168.228.131:9090/targets,确认所有节点状态为 UP。

6.2 验证 Alertmanager 集群

访问 http://192.168.228.131:9093/#/status,查看 Cluster 状态:

复制代码
Cluster Status: Ready
Members:
  - prometheus-01 (192.168.228.131:9094)
  - prometheus-02 (192.168.228.132:9094)
  - prometheus-03 (192.168.228.133:9094)

6.3 验证高可用效果

  • 停止 131 节点 Prometheus :systemctl stop prometheus
  • 此时 132 和 133 节点的 Prometheus 仍在采集数据
  • 访问 132 或 133 的 :9090 确认服务正常

📁 七、目录结构汇总

复制代码
/usr/local/prometheus/
├── prometheus
├── prometheus.yml          # 主配置文件
├── rules/                   # 告警规则目录
└── data/                    # TSDB 数据存储

/usr/local/node_exporter/
└── node_exporter

/usr/local/alertmanager/
├── alertmanager
├── alertmanager.yml        # 配置文件
└── data/                    # 数据存储

/etc/systemd/system/
├── prometheus.service
├── node_exporter.service
└── alertmanager.service

🛠️ 八、常用运维命令

bash 复制代码
# 查看服务状态
systemctl status prometheus node_exporter alertmanager grafana-server

# 重启服务
systemctl restart prometheus

# 查看日志
journalctl -u prometheus -f
journalctl -u alertmanager -f

# 热加载 Prometheus 配置(无需重启)
curl -X POST http://localhost:9090/-/reload

🎯 九、总结

组件 部署节点 端口 作用
Prometheus 131/132/133 9090 指标采集与存储
Node Exporter 131/132/133 9100 主机指标采集
Alertmanager 131/132/133 9093/9094 告警聚合与集群
Grafana 131 3000 数据可视化

通过三节点部署,实现了:

  • ✅ Prometheus 多副本冗余
  • ✅ Alertmanager 集群高可用
  • ✅ 监控数据可视化(Grafana)
  • ✅ 任一节点故障不影响整体监控

📚 参考资料

相关推荐
未济3 天前
linux 配置环境变量
linux
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.3 天前
进程间通信
linux
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-053 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz3 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK3 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid