Prometheus + Node Exporter 在两个 EC2 实例上
I’m happy to translate the article for you, but I’ll need the full text you’d like translated. Could you please paste the content (or the portion you want translated) here? I’ll keep the source line, formatting, markdown, and any code blocks exactly as they appear.
1️⃣ Architecture Overview (What we are building)
EC2 #1 — TARGET (Ubuntu)
- Purpose: expose system metrics → 目的: 暴露系统指标
- Tool: Node Exporter → 工具: Node Exporter
- Port: 9100 → 端口: 9100
EC2 #2 — MONITOR (Ubuntu)
- Purpose: collect and display metrics → 目的: 收集并展示指标
- Tool: Prometheus → 工具: Prometheus
- Port: 9090 → 端口: 9090
Browser
↓
Prometheus (Ubuntu, :9090)
↓ scrape
Node Exporter (Ubuntu, :9100)
2️⃣ AWS SECURITY GROUP SETUP (LAB MODE)
⚠️ 这在生产环境中不安全 – 仅用于培训和演示。
2.1 创建安全组(两个 EC2 均使用相同步骤)
AWS Console → EC2 → Security Groups → Create security group
| 入站规则 | 协议 | 端口 | 源 |
|---|---|---|---|
| All traffic | All | All | 0.0.0.0/0 |
出站规则 – 保持默认:All traffic → 0.0.0.0/0
将此安全组附加到:
- Monitor EC2
- Target EC2
3️⃣ 目标 EC2(Ubuntu) – 安装 Node Exporter
3.1 连接到目标 EC2
ssh ubuntu@<target-ip>
3.2 下载 Node Exporter
cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
3.3 解压并安装
tar -xvf node_exporter-1.7.0.linux-amd64.tar.gz
cd node_exporter-1.7.0.linux-amd64
sudo mv node_exporter /usr/local/bin/
3.4 启动 Node Exporter(前台演示)
node_exporter
你应该会看到:
Listening on :9100
3.5 验证 Node Exporter
ss -tulnp | grep 9100
测试指标:
curl http://localhost:9100/metrics | head
✅ Node Exporter 已就绪
4️⃣ MONITOR EC2 (Ubuntu) – 安装 Prometheus
4.1 连接到 MONITOR EC2
ssh ubuntu@<monitor-ip>
4.2 下载 Prometheus
cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz
4.3 解压文件
tar -xvf prometheus-2.48.1.linux-amd64.tar.gz
cd prometheus-2.48.1.linux-amd64
4.4 创建目录
sudo mkdir -p /etc/prometheus
sudo mkdir -p /var/lib/prometheus
4.5 安装二进制文件
sudo mv prometheus promtool /usr/local/bin/
prometheus --version
4.6 移动配置文件
sudo mv prometheus.yml /etc/prometheus/
sudo mv consoles console_libraries /etc/prometheus/
验证:
ls /etc/prometheus
预期输出:
prometheus.yml
consoles
console_libraries
5️⃣ 配置 Prometheus (Ubuntu)
5.1 编辑配置
sudo nano /etc/prometheus/prometheus.yml
5.2 用以下内容替换整个文件
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: []
rule_files: []
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
static_configs:
- targets: [":9100"]
保存 (CTRL+O, Enter, CTRL+X)。
5.3 验证配置(非常重要)
promtool check config /etc/prometheus/prometheus.yml
预期输出:
SUCCESS
6️⃣ 启动 Prometheus (Ubuntu)
prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus
查找:
Server is ready to receive web requests.
7️⃣ 访问 Prometheus UI
打开浏览器并访问:
http://<monitor-ip>:9090
然后进入 Status → Targets。
✅ 预期结果
prometheus UP
node UP
这确认了:
- Networking works
- Security group works
- Metrics are being scraped
8️⃣ 现场演示查询(Ubuntu 实验)
转到 Graph 选项卡。
8.1 检查目标
up
8.2 CPU 使用率(%)
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
8.3 内存使用率(%)
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes)
/ node_memory_MemTotal_bytes * 100
8.4 磁盘使用率(%)
100 * (1 - (node_filesystem_avail_bytes{mountpoint="/"}
/ node_filesystem_size_bytes{mountpoint="/"}))
Node Exporter 现在公开系统指标,Prometheus 可以抓取并可视化这些指标。
📦 概述
- Node Exporter 在 TARGET EC2 上运行,并在 9100 端口暴露指标。
- Prometheus 定期抓取这些指标。
- 如果目标 UP,则监控正常工作。
- Security Groups 控制网络访问——它们不是 Linux 级别的防火墙。
10️⃣ 我们有意允许的内容(实验模式)
| 组件 | 允许 |
|---|---|
| SG 入站 | 所有流量 |
| IPv4 | 0.0.0.0/0 |
| 端口 | 9090, 9100 |
| ✅ 易于学习 | ❌ 不适合生产环境 |
Source: …
📊 Grafana 部署与设置(Ubuntu,AWS EC2)
🔹 Grafana 放在哪里?
Grafana 安装在 MONITOR EC2 上,与 Prometheus 一起。
最终架构(非常重要)
TARGET EC2 (Ubuntu)
└── Node Exporter
└── :9100 (/metrics)
MONITOR EC2 (Ubuntu)
├── Prometheus
│ └── :9090 (scrapes node exporter)
└── Grafana
└── :3000 (visualizes Prometheus data)
为什么 Grafana 放在 MONITOR EC2 上
- Grafana 不收集指标——它只负责可视化。
- Prometheus 是数据源。
- 将 Grafana 与 Prometheus 放在同一台机器上可以带来:
- 更简洁的网络配置
- 符合真实生产环境的模式
- 更容易教学
| ✅ 正确 | ❌ 错误 |
|---|---|
| Prometheus + Grafana 在同一台 EC2 上 | Grafana 在目标节点上 |
🧩 STEP‑BY‑STEP:在 Ubuntu 上安装 Grafana(MONITOR EC2)
| 步骤 | 命令 / 操作 |
|---|---|
| 1️⃣ 连接到 MONITOR EC2 | ssh ubuntu@<monitor-ip> |
| 2️⃣ 更新系统 | sudo apt update |
| 3️⃣ 安装所需软件包 | sudo apt install -y apt-transport-https software-properties-common wget |
| 4️⃣ 添加 Grafana GPG 密钥 | wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -预期输出: OK |
| 5️⃣ 添加 Grafana 软件源 | echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list |
| 6️⃣ 安装 Grafana | sudo apt updatesudo apt install -y grafana |
| 7️⃣ 启动并设置 Grafana 开机自启 | sudo systemctl start grafana-serversudo systemctl enable grafana-server |
| 检查状态 | sudo systemctl status grafana-server预期: Active: active (running) |
| 8️⃣ 在安全组中打开 Grafana 端口(实验模式) | 确保入站规则允许所有流量(或至少端口 3000)访问 MONITOR EC2。 |
| 9️⃣ 访问 Grafana UI | 在浏览器中打开 http://<monitor-ip>:3000 |
| 默认登录 | 用户名: admin密码: admin(首次登录后会要求更改密码) |
| 🔗 将 Grafana 连接到 Prometheus | 1. Settings → Data Sources → Add data source → Prometheus。 2. Name: Prometheus。3. URL: http://localhost:9090。4. 点击 Save & Test。 预期: Data source is working。 |
| 📈 导入 Node Exporter 仪表板 | 1. + (Create) → Import。 2. Dashboard ID 1860 → Load。 3. 选择 Prometheus 数据源 → Import。 你将看到 CPU、内存、磁盘、网络和负载均值的图表。 |
🧠 常见问题与解决方案
| 症状 | 检查 / 修复 |
|---|---|
| Grafana 页面无法打开 | - 确认 EC2 安全组已放行端口 3000。 - 确认服务正在运行: sudo systemctl status grafana-server。 |
| Grafana 中没有数据 | - 确认 Prometheus 数据源 URL 正确为 http://localhost:9090。- 在 Grafana 中测试数据源(Save & Test)。 |
| 仪表盘显示为空 | - Prometheus 目标必须为 UP。 - 等待 1–2 分钟以收集并显示指标。 |
监控愉快! 🎉