监控与观察我的第一台 Linux 服务器:Prometheus 与 Grafana 初学者指南

发布: (2026年2月3日 GMT+8 17:18)
5 分钟阅读
原文: Dev.to

Source: Dev.to

有没有想过 Linux 服务器内部到底发生了什么?

作为一名 DevOps 工程师(或正在学习的新人),你很快就会从“仅仅执行命令”转向真正 观察 你的基础设施。 本指南将带你一步步构建一个使用 PrometheusGrafana 的简易可观测性栈——“Linux 健康哨兵”。

🔍 监控 vs. 可观测性

概念它告诉你什么
监控系统在工作吗?(例如,“CPU > 80%?”)
可观测性它为什么会这样? – 查看系统发出的数据(“信号”)。

可观测性 是一个 系统,不仅仅是一个工具:

sensors → data pipeline → dashboard

可观测性的四个阶段

  1. 仪器化(传感器) – 发出基础设施/应用信号的工具。
  2. 收集(管道) – 收集器清理、标记并路由数据。
  3. 存储(库) – 指标、日志和追踪存放在优化的数据库中。
  4. 可视化与警报 – 仪表盘 + 警报将数据转化为行动。

🎯 项目目标

可视化 Ubuntu 虚拟机的 实时 CPU、内存和磁盘健康,使用业界标准的 Prometheus + Grafana 堆栈。

🛠️ 技术栈

角色工具
目标 (VM)Node Exporter – “间谍/传感器”,收集硬件统计信息
控制中心 (Laptop)Prometheus – “大脑与存储”(时序数据库)
界面Grafana – 将原始数字转化为精美仪表盘

🚀 步骤实现

步骤 1 – 设置 目标(VirtualBox 虚拟机)

  1. 网络设置(关键)

    • 在 VirtualBox 中:Settings → Network
    • Attached toNAT 更改为 Bridged Adapter
    • 为什么? 让虚拟机在你的家庭网络上获取一个 IP,笔记本电脑才能访问它。
  2. 启动虚拟机 并获取其 IP 地址:

    hostname -I

    假设 IP 为 192.168.1.50

  3. 安装 “间谍” (Node Exporter)

    sudo apt update
    sudo apt install prometheus-node-exporter -y
  4. 验证
    笔记本电脑 的浏览器中打开 http://192.168.1.50:9100/metrics
    你应该会看到一大段文本——说明 exporter 已经在运行。

步骤 2 – 设置 控制中心(你的笔记本电脑)

  1. 安装 Prometheus

    sudo apt update
    sudo apt install prometheus -y
  2. 配置 Prometheus 抓取虚拟机

    sudo nano /etc/prometheus/prometheus.yml

    scrape_configs 列表的末尾添加以下 job:

    - job_name: 'ubuntu_vm'
      static_configs:
        - targets: ['192.168.1.50:9100']

    保存并退出(Ctrl+OEnterCtrl+X)。

  3. 重启 Prometheus

    sudo systemctl restart prometheus

    Prometheus UI 截图

步骤 3 – 使用 Grafana 可视化

  1. 安装 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
  2. 访问 Grafana
    在浏览器中打开 http://localhost:3000
    默认凭证:admin / admin(系统会提示你更改密码)。

  3. 添加 Prometheus 为数据源

    • Connections → Data Sources → Add Data Source
    • 选择 Prometheus
    • URL: http://localhost:9090
    • 点击 Save & Test

    Grafana 数据源配置

  4. 导入 Node Exporter 仪表盘

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

    Grafana 仪表盘导入界面

💡 结果

只需一台 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.
Back to Blog

相关文章

阅读更多 »

当 AI 给你一巴掌

当 AI 给你当头一棒:在 Adama 中调试 Claude 生成的代码。你是否曾让 AI “vibe‑code” 一个复杂功能,却花了数小时调试细微的 bug……