成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用 Node Exporter 監控 Linux 主機之一

運維 系統運維
Node Exporter 是用于暴露 *NIX 主機指標的 Exporter,比如采集 CPU、內存、磁盤等信息。采用 Go 編寫,不存在任何第三方依賴,所以只需要下載解壓即可運行。

[[430745]]

Node Exporter 是用于暴露 *NIX 主機指標的 Exporter,比如采集 CPU、內存、磁盤等信息。采用 Go 編寫,不存在任何第三方依賴,所以只需要下載解壓即可運行。

安裝配置

由于 Node Exporter 是一個獨立的二進制文件,可以直接從 Prometheus 下載頁面(https://prometheus.io/download/#node_exporter) 下載解壓運行:

  1. ☸ ➜ wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz 
  2. # 國內加速可以使用下面的命令下載 
  3. # wget https://download.fastgit.org/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz 
  4. ☸ ➜ tar -xvf node_exporter-1.2.2.linux-amd64.tar.gz 
  5. node_exporter-1.2.2.linux-amd64/ 
  6. node_exporter-1.2.2.linux-amd64/LICENSE 
  7. node_exporter-1.2.2.linux-amd64/NOTICE 
  8. node_exporter-1.2.2.linux-amd64/node_exporter 
  9. ☸ ➜ cd node_exporter-1.2.2.linux-amd64 && ls -la 
  10. total 18084 
  11. drwxr-xr-x  2 3434 3434       56 Aug  6 21:50 . 
  12. dr-xr-x---. 5 root root     4096 Oct 14 11:50 .. 
  13. -rw-r--r--  1 3434 3434    11357 Aug  6 21:49 LICENSE 
  14. -rwxr-xr-x  1 3434 3434 18494215 Aug  6 21:45 node_exporter 
  15. -rw-r--r--  1 3434 3434      463 Aug  6 21:49 NOTICE 

直接執行 node_exporter 文件即可運行:

  1. ☸ ➜ ./node_exporter 
  2. level=info ts=2021-10-14T03:52:31.947Z caller=node_exporter.go:182 msg="Starting node_exporter" version="(version=1.2.2, branch=HEAD, revision=26645363b486e12be40af7ce4fc91e731a33104e)" 
  3. level=info ts=2021-10-14T03:52:31.947Z caller=node_exporter.go:183 msg="Build context" build_context="(go=go1.16.7, user=root@b9cb4aa2eb17, date=20210806-13:44:18)" 
  4. ...... 
  5. level=info ts=2021-10-14T03:52:31.948Z caller=node_exporter.go:199 msg="Listening on" address=:9100 
  6. level=info ts=2021-10-14T03:52:31.948Z caller=tls_config.go:191 msg="TLS is disabled." http2=false 

從日志上可以看出 node_exporter 監聽在 9100 端口上,默認的 metrics 接口通過 /metrics 端點暴露,我們可以通過訪問 http://localhost:9100/metrics 來獲取監控指標數據:

  1. ☸ ➜ curl http://localhost:9100/metrics 
  2. ...... 
  3. # HELP node_load1 1m load average. 
  4. # TYPE node_load1 gauge 
  5. node_load1 0.01 
  6. # HELP node_load15 15m load average. 
  7. # TYPE node_load15 gauge 
  8. node_load15 0.05 
  9. # HELP node_load5 5m load average. 
  10. # TYPE node_load5 gauge 
  11. node_load5 0.04 
  12. # HELP node_memory_Active_anon_bytes Memory information field Active_anon_bytes. 
  13. # TYPE node_memory_Active_anon_bytes gauge 
  14. node_memory_Active_anon_bytes 8.4393984e+07 
  15. # HELP node_memory_Active_bytes Memory information field Active_bytes. 
  16. # TYPE node_memory_Active_bytes gauge 
  17. node_memory_Active_bytes 1.8167808e+08 
  18. # HELP node_memory_Active_file_bytes Memory information field Active_file_bytes. 
  19. # TYPE node_memory_Active_file_bytes gauge 
  20. node_memory_Active_file_bytes 9.7284096e+07 
  21. # HELP node_memory_AnonHugePages_bytes Memory information field AnonHugePages_bytes. 
  22. # TYPE node_memory_AnonHugePages_bytes gauge 
  23. node_memory_AnonHugePages_bytes 3.5651584e+07 
  24. # HELP node_memory_AnonPages_bytes Memory information field AnonPages_bytes. 
  25. # TYPE node_memory_AnonPages_bytes gauge 
  26. node_memory_AnonPages_bytes 8.159232e+07 
  27. # HELP node_memory_Bounce_bytes Memory information field Bounce_bytes. 
  28. # TYPE node_memory_Bounce_bytes gauge 
  29. node_memory_Bounce_bytes 0 
  30. ...... 

該 metrics 接口數據就是一個標準的 Prometheus 監控指標格式,我們只需要將該端點配置到 Prometheus 中即可抓取該指標數據。為了了解 node_exporter 可配置的參數,我們可以使用 ./node_exporter -h 來查看幫助信息:

  1. ☸ ➜ ./node_exporter -h 
  2.     --web.listen-address=":9100"  # 監聽的端口,默認是9100 
  3.     --web.telemetry-path="/metrics"  # metrics的路徑,默認為/metrics 
  4.     --web.disable-exporter-metrics  # 是否禁用go、prome默認的metrics 
  5.     --web.max-requests=40     # 最大并行請求數,默認40,設置為0時不限制 
  6.     --log.level="info"        # 日志等級: [debug, info, warn, error, fatal] 
  7.     --log.format=logfmt     # 置日志打印target和格式: [logfmt, json] 
  8.     --version                 # 版本號 
  9.     --collector.{metric-name} # 各個metric對應的參數 
  10.     ...... 

其中最重要的參數就是 --collector.,通過該參數可以啟用我們收集的功能模塊,node_exporter 會默認采集一些模塊,要禁用這些默認啟用的收集器可以通過 --no-collector. 標志來禁用,如果只啟用某些特定的收集器,基于先使用 --collector.disable-defaults 標志禁用所有默認的,然后在通過指定具體的收集器 --collector. 來進行啟用。下圖列出了默認啟用的收集器:

一般來說為了方便管理我們可以使用 docker 容器來運行 node_exporter,但是需要注意的是由于采集的是宿主機的指標信息,所以需要訪問主機系統,如果使用 docker 容器來部署的話需要添加一些額外的參數來允許 node_exporter 訪問宿主機的命名空間,如果直接在宿主機上運行的,我們可以用 systemd 來管理,創建一個如下所示的 service unit 文件:

  1. ☸ ➜ cat /etc/systemd/system/node_exporter.service 
  2. [Unit] 
  3. Description=node exporter service 
  4. Documentation=https://prometheus.io 
  5. After=network.target 
  6.  
  7. [Service] 
  8. Type=simple 
  9. User=root 
  10. Group=root 
  11. ExecStart=/usr/local/bin/node_exporter  # 有特殊需求的可以在后面指定參數配置 
  12. Restart=on-failure 
  13.  
  14. [Install] 
  15. WantedBy=multi-user.target 

然后就可以使用 systemd 來管理 node_exporter 了:

  1. ☸ ➜ cp node_exporter /usr/local/bin/node_exporter 
  2. ☸ ➜ systemctl daemon-reload 
  3. ☸ ➜ systemctl start node_exporter 
  4. ☸ ➜ systemctl status node_exporter 
  5. ● node_exporter.service - node exporter servoce 
  6.    Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled) 
  7.    Active: active (running) since Thu 2021-10-14 15:29:46 CST; 5s ago 
  8.      Docs: https://prometheus.io 
  9.  Main PID: 18679 (node_exporter) 
  10.     Tasks: 5 
  11.    Memory: 6.5M 
  12.    CGroup: /system.slice/node_exporter.service 
  13.            └─18679 /usr/local/bin/node_exporter 
  14.  
  15. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:..._zone 
  16. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...=time 
  17. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...timex 
  18. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...ueues 
  19. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...uname 
  20. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...mstat 
  21. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...r=xfs 
  22. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...r=zfs 
  23. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=node_exporter.go:...:9100 
  24. Oct 14 15:29:46 node1 node_exporter[18679]: level=info ts=2021-10-14T07:29:46.137Z caller=tls_config.go:191...false 
  25. Hint: Some lines were ellipsized, use -l to show in full

這里我們用 systemd 的方式在兩個節點上(node1、node2)分別啟動 node_exporter,啟動完成后我們使用靜態配置的方式在之前的 Prometheus 配置中新增一個 node_exporter 的抓取任務,來采集這兩個節點的監控指標數據,配置文件如下所示:

  1. global
  2.   scrape_interval: 5s 
  3.  
  4. scrape_configs: 
  5.   - job_name: "prometheus" 
  6.     static_configs: 
  7.       - targets: ["localhost:9090"
  8.   - job_name: "demo" 
  9.     scrape_interval: 15s # 會覆蓋global全局的配置 
  10.     scrape_timeout: 10s 
  11.     static_configs: 
  12.       - targets: ["localhost:10000""localhost:10001""localhost:10002"
  13.   - job_name: "node_exporter" # 新增 node_exporter 任務 
  14.     static_configs: 
  15.       - targets: ["node1:9100""node2:9100"] # node1、node2 在 hosts 中做了映射 

上面配置文件最后我們新增了一個名為 node_exporter 的抓取任務,采集的目標使用靜態配置的方式進行配置,然后重新加載 Prometheus,正常在 Prometheus 的 WebUI 的目標頁面就可以看到上面配置的 node_exporter 任務了。

接下來我們來了解一些關于節點監控的常用指標,比如 CPU、內存、IO 監控等。

CPU 監控

對于節點我們首先能想到的就是要先對 CPU 進行監控,因為 CPU 是處理任務的核心,根據 CPU 的狀態可以分析出當前系統的健康狀態。要對節點進行 CPU 監控,需要用到 node_cpu_seconds_total 這個監控指標,在 metrics 接口中該指標內容如下所示:

  1. # HELP node_cpu_seconds_total Seconds the CPUs spent in each mode. 
  2. # TYPE node_cpu_seconds_total counter 
  3. node_cpu_seconds_total{cpu="0",mode="idle"} 13172.76 
  4. node_cpu_seconds_total{cpu="0",mode="iowait"} 0.25 
  5. node_cpu_seconds_total{cpu="0",mode="irq"} 0 
  6. node_cpu_seconds_total{cpu="0",mode="nice"} 0.01 
  7. node_cpu_seconds_total{cpu="0",mode="softirq"} 87.99 
  8. node_cpu_seconds_total{cpu="0",mode="steal"} 0 
  9. node_cpu_seconds_total{cpu="0",mode="system"} 309.38 
  10. node_cpu_seconds_total{cpu="0",mode="user"} 79.93 
  11. node_cpu_seconds_total{cpu="1",mode="idle"} 13168.98 
  12. node_cpu_seconds_total{cpu="1",mode="iowait"} 0.27 
  13. node_cpu_seconds_total{cpu="1",mode="irq"} 0 
  14. node_cpu_seconds_total{cpu="1",mode="nice"} 0 
  15. node_cpu_seconds_total{cpu="1",mode="softirq"} 74.1 
  16. node_cpu_seconds_total{cpu="1",mode="steal"} 0 
  17. node_cpu_seconds_total{cpu="1",mode="system"} 314.71 
  18. node_cpu_seconds_total{cpu="1",mode="user"} 78.83 
  19. node_cpu_seconds_total{cpu="2",mode="idle"} 13182.78 
  20. node_cpu_seconds_total{cpu="2",mode="iowait"} 0.69 
  21. node_cpu_seconds_total{cpu="2",mode="irq"} 0 
  22. node_cpu_seconds_total{cpu="2",mode="nice"} 0 
  23. node_cpu_seconds_total{cpu="2",mode="softirq"} 66.01 
  24. node_cpu_seconds_total{cpu="2",mode="steal"} 0 
  25. node_cpu_seconds_total{cpu="2",mode="system"} 309.09 
  26. node_cpu_seconds_total{cpu="2",mode="user"} 79.44 
  27. node_cpu_seconds_total{cpu="3",mode="idle"} 13185.13 
  28. node_cpu_seconds_total{cpu="3",mode="iowait"} 0.18 
  29. node_cpu_seconds_total{cpu="3",mode="irq"} 0 
  30. node_cpu_seconds_total{cpu="3",mode="nice"} 0 
  31. node_cpu_seconds_total{cpu="3",mode="softirq"} 64.49 
  32. node_cpu_seconds_total{cpu="3",mode="steal"} 0 
  33. node_cpu_seconds_total{cpu="3",mode="system"} 305.86 
  34. node_cpu_seconds_total{cpu="3",mode="user"} 78.17 

從接口中描述可以看出該指標是用來統計 CPU 每種模式下所花費的時間,是一個 Counter 類型的指標,也就是會一直增長,這個數值其實是 CPU 時間片的一個累積值,意思就是從操作系統啟動起來 CPU 開始工作,就開始記錄自己總共使用的時間,然后保存下來,而且這里的累積的 CPU 使用時間還會分成幾個不同的模式,比如用戶態使用時間、空閑時間、中斷時間、內核態使用時間等等,也就是平時我們使用 top 命令查看的 CPU 的相關信息,而我們這里的這個指標會分別對這些模式進行記錄。

接下來我們來對節點的 CPU 進行監控,我們也知道一個一直增長的 CPU 時間對我們意義不大,一般我們更希望監控的是節點的 CPU 使用率,也就是我們使用 top 命令看到的百分比。

要計算 CPU 的使用率,那么就需要搞清楚這個使用率的含義,CPU 使用率是 CPU 除空閑(idle)狀態之外的其他所有 CPU 狀態的時間總和除以總的 CPU 時間得到的結果,理解了這個概念后就可以寫出正確的 promql 查詢語句了。

要計算除空閑狀態之外的 CPU 時間總和,更好的方式是不是直接計算空閑狀態的 CPU 時間使用率,然后用 1 減掉就是我們想要的結果了,所以首先我們先過濾 idle 模式的指標,在 Prometheus 的 WebUI 中輸入 node_cpu_seconds_total{mode="idle"} 進行過濾:

要計算使用率,肯定就需要知道 idle 模式的 CPU 用了多長時間,然后和總的進行對比,由于這是 Counter 指標,我們可以用 increase 函數來獲取變化,使用查詢語句 increase(node_cpu_seconds_total{mode="idle"}[1m]),因為 increase 函數要求輸入一個區間向量,所以這里我們取 1 分鐘內的數據:

我們可以看到查詢結果中有很多不同 cpu 序號的數據,我們當然需要計算所有 CPU 的時間,所以我們將它們聚合起來,我們要查詢的是不同節點的 CPU 使用率,所以就需要根據 instance 標簽進行聚合,使用查詢語句 sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance):

這樣我們就分別拿到不同節點 1 分鐘內的空閑 CPU 使用時間了,然后和總的 CPU (這個時候不需要過濾狀態模式)時間進行比較即可,使用查詢語句 sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(increase(node_cpu_seconds_total[1m])) by (instance):

然后計算 CPU 使用率就非常簡單了,使用 1 減去乘以 100 即可:(1 - sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(increase(node_cpu_seconds_total[1m])) by (instance) ) * 100。這就是能夠想到的最直接的 CPU 使用率查詢方式了,當然前面我們學習的 promql 語法中提到過更多的時候我們會去使用 rate 函數,而不是用 increase 函數進行計算,所以最終的 CPU 使用率的查詢語句為:(1 - sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(increase(node_cpu_seconds_total[1m])) by (instance) ) * 100。

可以和 top 命令的結果進行對比(下圖為 node2 節點),基本上是保持一致的,這就是監控節點 CPU 使用率的方式。

 

責任編輯:姜華 來源: k8s技術圈
相關推薦

2021-10-26 08:08:34

Node ExporLinux 監控

2021-10-28 08:39:22

Node Export自定義 監控

2022-08-16 09:54:48

telegrafexporter監控

2011-03-24 11:03:05

Nagios監控Linux

2014-09-28 10:37:45

LinuxNagiosNRPE

2024-02-21 16:13:36

CNCF開源監控工具Prometheus

2011-03-23 10:17:25

Nagios監控

2021-11-30 07:02:10

虛擬化Linux 中斷

2018-05-04 09:32:32

Linux快速監控rwho

2021-01-08 09:02:19

CentOS7Prometheus監控

2010-06-01 10:32:04

linux Mrtg

2020-11-26 09:10:36

Prometheus

2020-12-14 08:55:00

Node.js服務性框架

2011-01-05 09:34:47

linuxqq問題

2021-07-12 06:52:48

Zabbix監控Linux

2021-03-18 23:04:41

Solidity開發智能

2025-03-05 07:00:00

Grafana可視化Kubernetes

2010-02-23 12:03:37

CentOS系統

2021-12-18 07:42:15

Ebpf 監控 Node.js

2011-03-28 17:35:44

NagiosNRPE監控
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品视频一二三 | 亚洲伊人久久综合 | 国产日韩欧美精品一区二区 | 欧美综合久久 | 久久国产一区二区 | 成人av一区 | 国产精品美女久久久久aⅴ国产馆 | 日韩福利 | 日日噜噜夜夜爽爽狠狠 | av免费电影在线 | 五月激情综合网 | 手机在线观看av | 日韩欧美理论片 | 91在线成人 | 草草视频在线观看 | 久热免费在线 | 欧美一区二区三区大片 | 女人精96xxx免费网站p | www.久久久久久久久久久久 | 久久久久91 | 久久久精品一区 | av网站在线看 | 91亚洲国产成人久久精品网站 | 亚洲欧美在线视频 | 999re5这里只有精品 | 亚洲成人一区二区三区 | 成人福利在线观看 | 日日av| 无吗视频 | 欧美亚洲日本 | 欧美一级在线观看 | 九九九视频在线 | 91精品久久久久久久久 | 亚洲一区二区电影在线观看 | 九九在线视频 | 日本成人中文字幕在线观看 | 欧美一区二区三区在线观看视频 | 热99精品视频 | 欧美日韩综合 | 成人在线不卡 | 精品欧美乱码久久久久久1区2区 |