文章轉(zhuǎn)載于:https://devopscube.com/monitor-linux-servers-prometheus-node-exporter/

Node Exporter是收集所有 Linux 服務(wù)器相關(guān)指標和統(tǒng)計數(shù)據(jù)以進行監(jiān)視的最佳方法。
使用Prometheus Node Exporter監(jiān)控Linux服務(wù)器
在本指南中,您將學習如何在 Linux 服務(wù)器上設(shè)置Prometheus Node Exporter,以便將所有Node級別指標導(dǎo)出到Prometheus服務(wù)器
準備條件
- Prometheus Node Exporter需要Prometheus服務(wù)器保持啟動和運行。如果您想配置Prometheus服務(wù),請參考 Linux Prometheus 配置指南。
英文文檔,也可參照我的簡書中文文章 - 在服務(wù)器防火墻中打開端口9100,因為Prometheus通過該端口的讀取相關(guān)監(jiān)控指標數(shù)據(jù)。
安裝Prometheus Node Exporter
步驟1:下載最新的 node exporter安裝包,您應(yīng)該檢查確認Prometheus的版本,并更新此命令以獲取該軟件包。
cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
可以在https://prometheus.io/download/獲得最新版本下載地址或者下載,如圖所示:

步驟2:解壓tar壓縮包
tar -xvf node_exporter-1.6.0.linux-amd64.tar.gz
步驟3:將解壓后的文件移到 /usr/local/bin
sudo mv node_exporter-1.6.0.linux-amd64/node_exporter /usr/local/bin/
創(chuàng)建自定義Node Exporter服務(wù)
步驟1:創(chuàng)建一個node_exporter用戶,來運行Node Exporter服務(wù)。
sudo useradd -rs /bin/false node_exporter
步驟2:在 systemd 下創(chuàng)建Node Exporter服務(wù)文件。
sudo vim /etc/systemd/system/node_exporter.service
步驟3:在服務(wù)文件中添加以下服務(wù)文件內(nèi)容并保存。
[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
步驟4:重新加載系統(tǒng)守護進程并啟動Node Exporter服務(wù)。
sudo systemctl daemon-reload
sudo systemctl start node_exporter
步驟5:檢查Node Exporter狀態(tài),確保其處于正常運行狀態(tài)。
sudo systemctl status node_exporter
步驟6:開啟服務(wù)器開機自啟動Node Exporter服務(wù)。
sudo systemctl enable node_exporter
現(xiàn)在,Node Exporter可以通過 9100 端口導(dǎo)出監(jiān)控指標數(shù)據(jù)了。
您可以通過訪問您的服務(wù)器URL/metrics查看所有服務(wù)器指標,如所示
http://<server-IP>:9100/metrics
配置Prometheus
現(xiàn)在我們已經(jīng)在服務(wù)器上安裝并運行了Node Exporter服務(wù),我們必須在Prometheus服務(wù)器配置中將該服務(wù)器添加為目標。
注意:該配置應(yīng)在 Prometheus 服務(wù)器上完成。
步驟1:登錄Prometheus服務(wù)器并打開prometheus.yml文件。
sudo vim /etc/prometheus/prometheus.yml
步驟2:在 scrape config 部分下添加Node Exporter服務(wù),如下所示。將 localhost 改為您設(shè)置節(jié)點輸出器的服務(wù)器 IP。為便于識別,任務(wù)名稱可以是您的服務(wù)器主機名或 IP。
- job_name: 'node_exporter_metrics'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
步驟3:重啟prometheus服務(wù),使配置更改生效。
sudo systemctl restart prometheus
現(xiàn)在,如果您在prometheus web UI(http://<prometheus-IP>:9090/targets)中檢查目標,您將能夠看到如下所示的狀態(tài)。
sudo systemctl status prometheus
用戶界面,如下圖所示:

此外,您還可以使用 Prometheus 表達式瀏覽器查詢Node Exporter相關(guān)指標數(shù)據(jù)。以下是一些關(guān)鍵node指標數(shù)據(jù),您可以用來查找其統(tǒng)計數(shù)據(jù)。
node_memory_MemFree_bytes
node_cpu_seconds_total
node_filesystem_avail_bytes
rate(node_cpu_seconds_total{mode="system"}[1m])
rate(node_network_receive_bytes_total[1m])
使用prometheus查詢標簽來查詢可用的指標,如下圖所示:

接下來只需要安裝配置好Grafana就可以將相關(guān)指標數(shù)據(jù)可視化展示了。