监控与观察我的第一台 Linux 服务器:Prometheus 与 Grafana 初学者指南
Source: Dev.to
有没有想过 Linux 服务器内部到底发生了什么?
作为一名 DevOps 工程师(或正在学习的新人),你很快就会从“仅仅执行命令”转向真正 观察 你的基础设施。 本指南将带你一步步构建一个使用 Prometheus 和 Grafana 的简易可观测性栈——“Linux 健康哨兵”。
🔍 监控 vs. 可观测性
| 概念 | 它告诉你什么 |
|---|---|
| 监控 | 系统在工作吗?(例如,“CPU > 80%?”) |
| 可观测性 | 它为什么会这样? – 查看系统发出的数据(“信号”)。 |
可观测性 是一个 系统,不仅仅是一个工具:
sensors → data pipeline → dashboard
可观测性的四个阶段
- 仪器化(传感器) – 发出基础设施/应用信号的工具。
- 收集(管道) – 收集器清理、标记并路由数据。
- 存储(库) – 指标、日志和追踪存放在优化的数据库中。
- 可视化与警报 – 仪表盘 + 警报将数据转化为行动。
🎯 项目目标
可视化 Ubuntu 虚拟机的 实时 CPU、内存和磁盘健康,使用业界标准的 Prometheus + Grafana 堆栈。
🛠️ 技术栈
| 角色 | 工具 |
|---|---|
| 目标 (VM) | Node Exporter – “间谍/传感器”,收集硬件统计信息 |
| 控制中心 (Laptop) | Prometheus – “大脑与存储”(时序数据库) |
| 界面 | Grafana – 将原始数字转化为精美仪表盘 |
🚀 步骤实现
步骤 1 – 设置 目标(VirtualBox 虚拟机)
-
网络设置(关键)
- 在 VirtualBox 中:Settings → Network
- 将 Attached to 从 NAT 更改为 Bridged Adapter。
- 为什么? 让虚拟机在你的家庭网络上获取一个 IP,笔记本电脑才能访问它。
-
启动虚拟机 并获取其 IP 地址:
hostname -I假设 IP 为
192.168.1.50。 -
安装 “间谍” (Node Exporter)
sudo apt update sudo apt install prometheus-node-exporter -y -
验证
在 笔记本电脑 的浏览器中打开http://192.168.1.50:9100/metrics。
你应该会看到一大段文本——说明 exporter 已经在运行。
步骤 2 – 设置 控制中心(你的笔记本电脑)
-
安装 Prometheus
sudo apt update sudo apt install prometheus -y -
配置 Prometheus 抓取虚拟机
sudo nano /etc/prometheus/prometheus.yml在
scrape_configs列表的末尾添加以下 job:- job_name: 'ubuntu_vm' static_configs: - targets: ['192.168.1.50:9100']保存并退出(
Ctrl+O、Enter、Ctrl+X)。 -
重启 Prometheus
sudo systemctl restart prometheus
步骤 3 – 使用 Grafana 可视化
-
安装 Grafana
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/grafana.gpg > /dev/null sudo apt update && sudo apt install grafana -y sudo systemctl enable --now grafana-server -
访问 Grafana
在浏览器中打开http://localhost:3000。
默认凭证:admin / admin(系统会提示你更改密码)。 -
添加 Prometheus 为数据源
- Connections → Data Sources → Add Data Source
- 选择 Prometheus
- URL:
http://localhost:9090 - 点击 Save & Test

-
导入 Node Exporter 仪表盘
- 点击右上角的 + → Import
- 仪表盘 ID:1860(官方 Node Exporter 仪表盘)
- 选择刚才添加的 Prometheus 数据源并点击 Import

💡 结果
只需一台 Ubuntu 虚拟机、Prometheus 和 Grafana,你现在就拥有了一个 真实的可观测性管道:
- CPU、内存和磁盘指标 由 Node Exporter 收集。
- Prometheus 抓取并存储这些指标。
- Grafana 实时可视化这些数据,随时用于告警或深入分析。
你已经从“运行命令”转变为 看到 底层发生的情况——这正是生产环境监控的样子! 🎉
# Observability Foundations
Works in professional environments.
This small project is the foundation of modern DevOps observability. From here, you can explore:
- **Alerting with Alertmanager**
- **Container monitoring**
- **Kubernetes observability**
- **Logs and distributed tracing**
Observability isn’t just about dashboards; it’s about understanding your systems deeply.