Prometheus 和 node_exporter 設定指南

15 分鐘閱讀 - 2026年5月29日

hero section cover
目錄
  • Prometheus 與 node_exporter 伺服器監控設定
  • 安裝 node_exporter
  • 將 node_exporter 作為 systemd 服務執行
  • 設定 Prometheus 以擷取 node_exporter 數據
  • 確保您的監控系統安全
  • 監控最佳實踐與後續步驟
  • 疑難排解
分享

安裝 Prometheus 和 node_exporter、設定 scrape 目標、設定 systemd 服務並保護您的監控堆疊。針對 Linux 逐步進行。

Prometheus 與 node_exporter 伺服器監控設定

Prometheus 負責擷取並儲存時間序列指標。node_exporter 則會公開系統層級的資料(如 CPU、記憶體及磁碟使用率),供 Prometheus 收集。本指南涵蓋兩者的安裝、設定擷取目標、將 node_exporter 作為 systemd 服務執行,以及鎖定存取權限。

安裝 node_exporter

從官方 GitHub 發行頁面下載最新穩定版本。截至 2026 年 5 月,最新版本為 1.11.1。請將 amd64 替換為 arm64

wget https://github.com/prometheus/node_exporter/releases/download/v1.11.1/node_exporter-1.11.1.linux-amd64.tar.gz

請將 SHA256 校驗和與發行版頁面上的值進行比對,確認無誤後解壓縮並安裝:

tar -xzvf node_exporter-1.11.1.linux-amd64.tar.gz
sudo mv node_exporter-1.11.1.linux-amd64/node_exporter /usr/local/bin/

建立一個專用的系統使用者,該使用者不設家目錄且無登入殼層:

sudo useradd --no-create-home --shell /bin/false node_exporter
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

執行 /usr/local/bin/node_exporter ,然後檢查輸出:

curl http://localhost:9100/metrics

您應會看到以 node_,並包含諸如 node_cpu_seconds_totalnode_memory_MemAvailable_bytes等指標。預設情況下,node_exporter 會公開約 500 個時間序列。

將 node_exporter 作為 systemd 服務執行

在終端機上執行 node_exporter 雖適合用於測試,但當您關閉工作階段時,服務便會停止。請在 /etc/systemd/system/node_exporter.service:

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
 
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5
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

使用 sudo systemctl status node_exporter。輸出應顯示 active (running).

若 Prometheus 運行於同一主機上,請透過修改 ExecStart

ExecStart=/usr/local/bin/node_exporter --web.listen-address="127.0.0.1:9100"

設定 Prometheus 以擷取 node_exporter 數據

開啟 /etc/prometheus/prometheus.yml 並在 scrape_configs:

scrape_configs:
  - job_name: 'node_exporter'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:9100']
        labels:
          env: 'production'

job_name 用於在查詢和儀表板中識別來源。 targets 指向 node_exporter 監聽的主機與埠號。標籤如 env 可協助您日後篩選指標。

重新啟動前請驗證設定:

promtool check config /etc/prometheus/prometheus.yml

若驗證通過,請在不造成服務中斷的情況下重新載入 Prometheus:

sudo systemctl reload prometheus

開啟 http://<your-prometheus-ip>:9090,前往「狀態 > 目標 (Status > Targets)」,確認 node_exporter 工作顯示為綠色的「已上線 (UP)」狀態。在「表達式瀏覽器 (Expression Browser)」中執行類似 node_cpu_seconds_total ,以確認資料正在傳輸。

確保您的監控系統安全

切勿將 9090 或 9100 埠對外公開。在 Ubuntu/Debian 系統上,請將 node_exporter 的存取權限限制為 Prometheus 伺服器的 IP 位址:

sudo ufw allow from <prometheus-ip> to any port 9100

在採用 firewalld 的 CentOS/RHEL 系統上:

firewall-cmd --permanent --add-port=9100/tcp

針對 Prometheus 網頁介面,請將其置於 Nginx 等反向代理伺服器後方,並啟用基本驗證與 TLS。若需從多個地點存取系統卻不希望直接暴露端口,Tailscale 等網狀 VPN 亦是另一種選擇。

監控最佳實踐與後續步驟

請使用 node_memory_MemAvailable_bytes 取代 MemFree 來設定記憶體警示。 MemAvailable 將緩衝區與快取納入計算,以更精確地呈現實際可用空間。

使用 --no-collector.<name> 標誌來減少干擾。

針對磁碟空間警示, predict_linear PromQL 函數可讓您根據當前趨勢預測卷何時會滿。設定 7 天的預測窗口,可在緩慢的記憶體洩漏演變成服務中斷前及時發現。

若要監控多台伺服器,請在每台機器上安裝 node_exporter,並將其 IP 地址加入 targets 清單中 prometheus.yml。若環境規模較大,請改用基於檔案的服務發現機制,而非硬編碼 IP 位址。

搭配 Grafana 可建立視覺化儀表板。「Node Exporter Full」儀表板(ID 1860)是理想的起點。Alertmanager 會將關鍵警示轉發至 Slack、電子郵件或 PagerDuty

FDC 的專用伺服器與 VPS 方案預設即支援 Prometheus 和 node_exporter。請參閱 FDC 的專用伺服器選項

疑難排解

問題可能原因檢查指令
服務無法啟動二進位檔路徑或權限錯誤journalctl -u node_exporter -xe
無法取得指標防火牆封鎖 9100 埠,或綁定位址錯誤ss -lntp | grep 9100
Prometheus 中的目標已下線網路問題,或 prometheus.yml 中的目標 IP 錯誤curl -I http://<target-ip>:9100/metrics
缺少特定指標收集器預設為停用狀態node_exporter --help

博客

本周特色

更多文章
Prometheus 和 node_exporter 設定指南

Prometheus 和 node_exporter 設定指南

安裝 Prometheus 和 node_exporter、設定 scrape 目標、設定 systemd 服務並保護您的監控堆疊。針對 Linux 逐步進行。

15 分鐘閱讀 - 2026年5月29日

Linux 封包處理的 XDP 與 eBPF

14 分鐘閱讀 - 2026年5月27日

更多文章