🗂️ VPS › VPS监控安装  |  更新于:2026年11月9日

VPS监控安装指南:Netdata 或 Prometheus + Grafana

没有监控的VPS就像没有仪表盘的汽车——直到引擎熄火你才知道燃油已空。本文将带你在Ubuntu VPS上一步步安装 Netdata(简单快速)或 Prometheus + Grafana(强大灵活),实现CPU、内存、硬盘、网络的实时监控。

VPS监控安装 Netdata Prometheus Grafana

📋 目录

  1. 为什么需要监控VPS
  2. Netdata vs Prometheus 对比
  3. 在Ubuntu上安装Netdata
  4. 配置Netdata(告警 + 安全)
  5. 安装Prometheus + Node Exporter
  6. 安装Grafana并连接Prometheus
  7. 需要监控的关键指标
  8. 配置告警通知
  9. 保护监控面板安全
  10. 常见问题解答

为什么需要监控VPS

没有监控,你只会在网站已经宕机后才发现问题。VPS常见故障场景:

💡 监控的价值 在宕机前发现问题、在正确时机规划扩容、精确定位性能瓶颈、向利益相关者证明稳定性。

Netdata vs Prometheus 对比

特性NetdataPrometheus + Grafana
安装难度⭐⭐⭐⭐⭐ 极简(1条命令)⭐⭐⭐ 多步骤配置
实时仪表盘✅ 自带UI,即装即用❌ 需导入仪表盘
灵活性中等⭐⭐⭐⭐⭐ 极高
长期数据存储有限(默认24小时)✅ 可自定义保留时间
资源占用内存~50-100 MB内存~300-700 MB
多服务器支持Netdata Cloud(免费)✅ 原生支持
适用场景单台VPS、新手入门集群、大型生产环境
✅ 推荐选择 单台或少量VPS → 选 Netdata。需要自定义仪表盘和复杂告警的多服务器环境 → 选 Prometheus + Grafana。

在Ubuntu上安装Netdata

免费 Ubuntu 20.04/22.04/24.04

Netdata提供一键安装脚本,自动检测操作系统并完成安装:

# 先更新软件包列表
sudo apt update

# 使用一键安装脚本安装Netdata
wget -O /tmp/netdata-kickstart.sh https://get.netdata.cloud/kickstart.sh
sh /tmp/netdata-kickstart.sh --stable-channel --no-updates

验证服务是否正常运行:

sudo systemctl status netdata
# 应显示: active (running)

在浏览器访问 http://您的VPS_IP:19999,实时图表立即呈现。

⚠️ 不要将19999端口对外开放 默认情况下Netdata对所有IP开放19999端口。请立即限制访问权限(见下方安全配置)。

通过软件包管理器安装(备选方案)

# 添加Netdata软件源
curl -s https://packagecloud.io/install/repositories/netdata/netdata/script.deb.sh | sudo bash

# 安装
sudo apt install netdata -y

# 启用并启动
sudo systemctl enable netdata
sudo systemctl start netdata

配置Netdata(告警 + 安全)

限制面板访问权限

编辑 /etc/netdata/netdata.conf:

[web]
    # 仅允许本地访问(通过SSH隧道连接)
    bind to = 127.0.0.1
    # 或指定允许的IP段
    allow connections from = localhost 192.168.1.*
sudo systemctl restart netdata

通过SSH隧道访问面板

# 在本地机器上执行
ssh -L 19999:127.0.0.1:19999 user@您的VPS_IP

# 然后在浏览器中访问
# http://localhost:19999

配置邮件告警

sudo nano /etc/netdata/health_alarm_notify.conf
SEND_EMAIL="YES"
DEFAULT_RECIPIENT_EMAIL="[email protected]"
SENDMAIL="$(which sendmail)"

安装Prometheus + Node Exporter

Prometheus需要安装两个组件:Prometheus本体(指标存储)和Node Exporter(采集系统指标)。

第一步:安装Node Exporter

cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/

# 创建服务用户
sudo useradd -rs /bin/false node_exporter

创建systemd服务文件:

sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
# 验证: curl http://localhost:9100/metrics | head -20

第二步:安装Prometheus

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar xvf prometheus-2.54.1.linux-amd64.tar.gz

sudo useradd -rs /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo cp prometheus-2.54.1.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.54.1.linux-amd64/promtool /usr/local/bin/
sudo cp -r prometheus-2.54.1.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-2.54.1.linux-amd64/console_libraries /etc/prometheus

配置Prometheus:

sudo nano /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

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

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

创建systemd服务:

sudo nano /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/ \
  --storage.tsdb.retention.time=30d \
  --web.listen-address=127.0.0.1:9090

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus

安装Grafana并连接Prometheus

安装Grafana

sudo apt install -y apt-transport-https software-properties-common wget

sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | \
  sudo tee /etc/apt/sources.list.d/grafana.list

sudo apt update
sudo apt install grafana -y

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

访问Grafana界面

使用SSH隧道:ssh -L 3000:127.0.0.1:3000 user@VPS_IP,然后访问 http://localhost:3000

添加Prometheus数据源

  1. 进入 Connections → Data sources → Add data source
  2. 选择 Prometheus
  3. URL填写:http://localhost:9090
  4. 点击 Save & test

导入预制仪表盘

  1. 进入 Dashboards → Import
  2. 输入ID:1860(Node Exporter Full,最受欢迎的仪表盘)
  3. 选择刚创建的Prometheus数据源
  4. 点击 Import

立即获得展示CPU、内存、磁盘和网络指标的精美仪表盘。

需要监控的关键指标

指标正常范围告警阈值
CPU使用率< 70%> 90% 持续5分钟
内存使用率< 80%> 90%(OOM风险)
磁盘使用率< 70%> 85%(规划清理)
磁盘I/O等待视负载而定持续 > 20%
网络流量正常使用基准异常峰值
系统负载< CPU核心数> 2倍CPU核心数
Swap使用率0-20%> 50%(内存不足)

配置告警通知

Netdata告警

Netdata在 /etc/netdata/health.d/ 中内置了大量告警规则,可自定义阈值:

# /etc/netdata/health.d/cpu.conf
alarm: cpu_usage
on: system.cpu
lookup: average -1m percentage of user,system
units: %
warn: $this > 80
crit: $this > 95
info: CPU使用率过高

Grafana告警

  1. 打开面板 → Edit → Alert
  2. 设置条件(如CPU > 90% 持续5分钟)
  3. 添加通知渠道(邮件、Slack、Telegram等)
  4. 点击 Save
✅ 推荐告警配置 CPU > 90% / 内存 > 90% / 磁盘 > 85% / 服务宕机 / 异常网络流量

保护监控面板安全

监控面板包含服务器内部信息,绝对不能对外网开放。

方式一:SSH隧道(推荐用于单台VPS)

# 访问Netdata
ssh -L 19999:127.0.0.1:19999 user@VPS_IP &
# 访问Grafana
ssh -L 3000:127.0.0.1:3000 user@VPS_IP &

方式二:Nginx反向代理 + 基本认证

sudo apt install -y nginx apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd admin
# /etc/nginx/sites-available/netdata
server {
    listen 80;
    server_name monitor.yourdomain.com;

    auth_basic "Monitoring";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        proxy_pass http://127.0.0.1:19999;
        proxy_set_header Host $host;
    }
}

方式三:UFW防火墙封锁端口

sudo ufw deny 19999
sudo ufw deny 9090
sudo ufw deny 3000
sudo ufw allow from 您的IP地址 to any port 19999

🎯 总结:选择适合您的监控工具

常见问题解答

Netdata和Prometheus有什么区别?
Netdata专注于实时监控,自带UI界面,安装极为简单;Prometheus专注于长期指标存储,灵活性强,但需要单独安装Grafana进行可视化。
Netdata会占用多少VPS内存?
Netdata大约使用50-100 MB内存,CPU占用极低(1-3%),适合1 GB内存以上的各类VPS。
Netdata可以设置告警通知吗?
可以,Netdata内置告警功能,支持邮件、Slack、Telegram、PagerDuty等多种通知方式,在/etc/netdata/health.d/中配置,或使用免费的Netdata Cloud。
Prometheus + Grafana需要多少内存?
Prometheus根据指标数量使用200-500 MB内存,Grafana约需100-200 MB。建议选择至少2 GB内存的VPS来运行这套监控系统。
Netdata和Prometheus分别使用哪些端口?
Netdata使用19999端口,Prometheus使用9090端口,Node Exporter使用9100端口,Grafana使用3000端口。建议通过防火墙限制,不要对外网开放。
可以同时使用Netdata和Prometheus吗?
可以,Netdata通过内置Exporter将指标发送到Prometheus,实现Netdata负责实时监控、Prometheus负责长期数据存储的组合方案,各取所长。

相关文章: VPS UFW防火墙配置 • 创建systemd服务 • Grafana + Prometheus监控VPS