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. 点击 ConfigurationData SourcesAdd 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 节点 Prometheussystemctl 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)
  • ✅ 任一节点故障不影响整体监控

📚 参考资料

相关推荐
小此方9 小时前
Re:Linux系统篇(四十五)信号篇·三:一文讲透 Linux 信号保存机制:block,pending,handler三张表到底在做什么
linux·运维·服务器
C+-C资深大佬11 小时前
GitHub Actions 自动化运维实战:从零到一构建高效 CI/CD 流水线
运维·自动化·github
2301_7779983417 小时前
Linux信号机制
linux
一叶龙洲20 小时前
win11与Ubuntu之间同步配置、插件
linux·运维·ubuntu
CHANG_THE_WORLD20 小时前
12.总结:深入理解 Linux I/O 多路复用:select、poll、epoll 全解析
linux·运维·服务器
风曦Kisaki21 小时前
# Linux笔记:操作系统优化与资源管理
linux·运维·服务器·笔记
Mr.HeBoYan21 小时前
一次持续三天才出现的丢包故障——深入解析 DPDK Memory Ordering、rte_ring 与 CPU Memory Barrier (下)
linux·网络·算法·架构·dpdk
zhangrelay1 天前
笔记本轻量高品质延寿工具完整分系统清单
运维·笔记·学习
Discipline~Hai1 天前
ARM01-ARM体系架构
linux·c语言·arm开发·架构