CentOS8.2安裝配置Prometheus和node_exporter
Prometheus是一個開源的監視,查詢和警報工具。該功能豐富的工具最初由Soundcloud於2012年構建,已被多家公司採用,以監控其IT基礎架構並確保所有系統執行順暢。Prometheus允許您查詢和提取時間序列指標,例如通過HTTP協議的CPU和記憶體利用率,並在實時圖形上將其視覺化。您還可以將Prometheus配置為在節點或服務停機時發出警報,並將其與其他第三方監視工具(例如Grafana)整合
prometheus主機 | prometheus 客戶端 | |
---|---|---|
主機名 | prometheusserver | prometheusclient |
IP資訊 | 192.168.6.140 | 192.168.6.160 |
步驟1:建立一個Prometheus使用者和組
首先,我們將為Prometheus建立一個系統使用者。執行以下命令以實現此目的。
[root@prometheusserver ~]# useradd -m -s /bin/false prometheus [root@prometheusserver ~]# id prometheus uid=1005(prometheus) gid=1005(prometheus) groups=1005(prometheus) [root@prometheusserver ~]#
您可能已經注意到,系統使用者沒有/ bin / false選項中指定的登入許可權。
步驟2:Prometheus建立配置目錄
建立Prometheus使用者之後,我們將在/ etc和/ var目錄中建立配置目錄,這些目錄將儲存Prometheus配置檔案和資料。因此,執行以下命令:
[root@prometheusserver ~]# mkdir /etc/prometheus
[root@prometheusserver ~]# mkdir /var/lib/prometheus
在/ var / lib / prometheus上設定所有權
[root@prometheusserver ~]# chown prometheus /var/lib/prometheus/
步驟3:下載Prometheus tar檔案
有了目錄後,我們現在可以下載Prometheus。要獲取最新版本,請轉到“下載”頁面以獲取適用於您的環境的最新版本。在撰寫本文時,最新版本為v 2.23.0。或者,只需執行以下命令
[root@prometheusserver ~]# dnf install wget -y
[root@prometheusserver ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
下載完成後,解壓縮tarball檔案,如下所示
[root@prometheusserver tmp]# tar zxvf prometheus-2.23.0.linux-amd64.tar.gz
這將為您提供一個名為prometheus-2.14.0.linux-amd64的目錄
使用tree命令檢視目錄結構,
提取的目錄包含2個二進位制檔案prometheus和promtool,我們需要將其複製到/usr/local/bin路徑。
因此,導航到提取的目錄並使用以下命令將其複製:
[root@prometheusserver tmp]# cd prometheus-2.23.0.linux-amd64
[root@prometheusserver prometheus-2.23.0.linux-amd64]# cp prometheus /usr/local/bin
對其他二進位制檔案也一樣
[root@prometheusserver prometheus-2.23.0.linux-amd64]# cp promtool /usr/local/bin
步驟4:Prometheus建立配置檔案
首先從配置開始,建立檔案/etc/prometheus/prometheus.ym l並將配置貼上到檔案中
[root@prometheus ~]# vi /etc/prometheus/prometheus.yml
# Global config
global:
scrape_interval:15s #設定間隔為每15秒。預設值為每1分鐘。
Evaluation_interval:15s #每15秒評估一次規則。預設值為每1分鐘。
scrape_timeout:15s #scrape_timeout設定為全域性預設值(10s)。
#包含僅一個要刮擦的端點的刮擦配置: #這裡是Prometheus本身。
scrape_configs:
#作業名稱作為標籤“ job = <job_name>”新增到從此配置中刮取的任何時間序列。
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
這將僅監視您的本地系統(Prometheus Server)。
接下來,如下調整防火牆,以允許通過埠9090到伺服器的外部連線
[root@prometheusserver /]# firewall-cmd --add-port=9090/tcp --permanent
success
[root@prometheusserver /]#firewall-cmd --reload
success
[root@prometheusserver /]#
步驟5:Prometheus Server建立Systemd服務檔案
為了使我們能夠使用systemd將Prometheus作為服務進行管理,我們需要為其建立系統檔案。因此,如圖所示建立檔案並貼上內容,
[root@prometheusserver /]# vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.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/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
為了使更改生效,請重新載入systemctl,
[root@prometheusserver /]# systemctl daemon-reload
現在啟動並啟用Prometheus在啟動時執行
[root@prometheusserver /]# systemctl start prometheus
[root@prometheusserver /]# systemctl enable prometheus
為了確定Prometheus正在執行,請執行以下命令:
[root@prometheusserver /]# systemctl status prometheus
從顯示的輸出中,我們可以清楚地看到Prometheus正常執行,沒有錯誤。另外,您可以使用netstat實用工具檢查服務是否正在偵聽埠9090。
[root@prometheusserver /]# netstat -tunlp
或者
[root@prometheusserver /]# ss -antpl
那很棒!Prometheus按預期在埠9090上執行。現在轉到瀏覽器,瀏覽伺服器的IP,如下所示
http://伺服器-ip:9090
點選“狀態”標籤,然後點選“目標”
您的系統將顯示如下
步驟6:安裝並配置node_exporter
node_exporter是一種實用程式,可收集和大量Linux系統指標,例如CPU,記憶體使用率,檔案系統和網路統計資訊。在本節中,我們將在Prometheus伺服器和遠端CentOS 8 Linux主機上安裝node_exporter,並監視這兩個主機上的系統指標。
在Prometheus節點上,我們將為node_exporter建立一個系統使用者。
[root@prometheusserver tmp]# useradd -m -s /bin/false node_exporter
接下來,前往Prometheus的下載頁面,然後下載node_exporter tarball或使用下面的wget命令從命令列下載下來,
[root@prometheusserver tmp]# wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
下載node_exporter檔案後,繼續進行提取,如下所示
[root@prometheusserver tdops]# tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz
您可以使用tree命令檢查提取的資料夾的內容,如下所示
[root@prometheusserver tdops]# tree node_exporter-1.0.1.linux-amd64
node_exporter-1.0.1.linux-amd64
├── LICENSE
├── node_exporter
└── NOTICE
0 directories, 3 files
接下來,將node_exporter的二進位制檔案複製到 /usr/local/bin路徑
[root@prometheusserver tdops]# cp node_exporter-1.0.1.linux-amd64/node_exporter /usr/local/bin
接下來,設定已複製的node_exporter檔案的檔案許可權,如下所示
[root@prometheusserver tmp]# chown node_exporter:node_exporter /usr/local/bin/node_exporter
接下來,我們需要配置node_exporter以作為服務執行。因此,繼續操作並建立一個systemd服務檔案,如下所示
[root@prometheusserver tmp]# vi /etc/systemd/system/node_exporter.service
然後貼上如下所示的配置並儲存檔案
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
為了使更改生效,請使用以下命令重新載入systemd Manager:
[root@prometheusserver tmp]# systemctl daemon-reload
接下來,啟動並啟用node_exporter服務
[root@prometheusserver tdops]# systemctl start node_exporter
[root@prometheusserver tdops]# systemctl enable node_exporter
或者
# 啟動並使節點匯出器在系統引導時執行。
[root@prometheusserver tdops]# systemctl enable --now node_exporter.service
為了確保該服務正在執行,請執行:
[root@prometheusserver tdops]# systemctl status node_exporter
● node_exporter.service - Prometheus Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disa>
Active: active (running) since Sun 2020-12-13 20:59:44 CST; 32s ago
Main PID: 6419 (node_exporter)
Tasks: 4 (limit: 23840)
Memory: 1.7M
CGroup: /system.slice/node_exporter.service
└─6419 /usr/local/bin/node_exporter
遇到一個小插曲啟動時遇到錯誤
Dec 13 20:43:50 centos8 systemd[1]: Started Prometheus Node Exporter.
Dec 13 20:43:50 centos8 systemd[5595]: node_exporter.service: Failed to execute command: Exec format error
Dec 13 20:43:50 centos8 systemd[5595]: node_exporter.service: Failed at step EXEC spawning /usr/local/bin/node_exporter: Exec format error
Dec 13 20:43:50 centos8 systemd[1]: node_exporter.service: Main process exited, code=exited, status=203/EXEC
Dec 13 20:43:50 centos8 systemd[1]: node_exporter.service: Failed with result 'exit-code'.
Dec 13 20:43:50 centos8 tdops[5610]: tdops pts/1 2020-12-13 19:41 (10.57.237.13) [/tmp] 2020-12-13/20:43:50 systemctl restart node_exporter
[root@prometheusserver tmp]# systemctl status node_exporter
● node_exporter.service - Prometheus Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2020-12-13 20:44:36 CST; 4s ago
Process: 5635 ExecStart=/usr/local/bin/node_exporter (code=exited, status=203/EXEC)
Main PID: 5635 (code=exited, status=203/EXEC)
Dec 13 20:44:36 prometheusserver systemd[1]: Started Prometheus Node Exporter.
Dec 13 20:44:36 prometheusserver systemd[1]: node_exporter.service: Main process exited, code=exited, status=203/EXEC
Dec 13 20:44:36 prometheusserver systemd[1]: node_exporter.service: Failed with result 'exit-code'.
[root@prometheusserver tmp]#
排查了一下發現,下載的軟體包與系統不匹配,
為了確保該服務正在執行,請使用netstat實用程式檢查它是否正在按預設預期在埠9100上偵聽。
[root@prometheusserver tdops]# netstat -pnltu | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 6419/node_exporter
[root@prometheusserver tdops]# ss -antpl | grep 9100
LISTEN 0 16384 *:9100
完善!Node_exporter服務正在按預期執行。
接下來,如圖所示在防火牆中開啟埠9100
[root@prometheusserver tdops]# firewall-cmd --add-port=9100/tcp --permanent
success
[root@prometheusserver tdops]# firewall-cmd --reload
success
同樣,對遠端CentOS 8 Linux系統也重複上述步驟。
最後,您需要將node_exporter目標新增到prometheus.yml檔案。附加以下幾行以為Prometheus伺服器定義node_exporter
[root@prometheusserver tdops]# cat /etc/prometheus/prometheus.yml
# Global config
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
##---新增內容------
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
重新啟動Prometheus服務
[root@prometheusserver tdops]# systemctl restart prometheus
再次轉到瀏覽器,單擊“狀態”標籤,然後單擊“目標”
確保 在Prometheus伺服器的瀏覽器上觀察到名為node_exporter的新端點
要為遠端Linux系統 prometheusclient新增端點,請回到prometheus.yml檔案並在下面新增以下行
–目標:['192.168.6.160:9100']
現在,node_exporter部分應如下所示
-job_name:'node_exporter'static_configs
:
-目標:['localhost:9100']
-目標:['192.168.10.90:9100']
儲存更改並重新啟動Prometheus服務
[ root @ prometheus〜]#systemctl重新啟動prometheus
重新整理瀏覽器並注意為遠端CentOS Linux系統新增的第二個端點
為了確保您從配置的節點接收指標。只需使用curl命令,如下所示:
#curl http:// 192.168.6.160:9100 / metrics
例如,要顯示來自Prometheus伺服器的度量,請執行:
[ root @ prometheus〜]#curl http:// localhost:9100 / metrics
對於遠端CentOS 8主機,我執行了以下命令:
[ root @ prometheus〜]#curl http://192.168.6.160:9100/metrics
也可以通過開啟瀏覽器並瀏覽URL來實現
http://192.168.10.90:9100/metrics
您也可以選擇繪製所需指標的圖形。只需轉到Prometheus伺服器的主頁,然後單擊標記為“在游標處插入度量”的下拉選單。
選擇要繪製圖形的指標,
單擊“執行”按鈕,然後單擊下面的“圖形”選項卡以顯示圖形
這使我們到了本主題的結尾。您已成功安裝並配置了Prometheus,以監視伺服器和遠端主機上的系統指標。在下一個指南中,我們將Prometheus與Grafana整合在一起,以更好地視覺化和分析指標。隨時與我們分享您的反饋,並與朋友分享文章。