1. 程式人生 > 其它 >prometheus二進位制部署與Grafana圖形化介面的使用

prometheus二進位制部署與Grafana圖形化介面的使用

一、Prometheus簡介

為什麼使用peometheus?

容器在k8s環境中可以任意橫向擴容與縮容,那麼就需要監控服務能夠自動對新建立的容器進行監控,當容器刪除後又能及時的從監控服務中刪除,而傳統的zabbix的監控方式需要在每一個容器中安裝啟動的agent,並且容器自動發現註冊及模板關聯方面並沒有比較好的實現方式。

(1)官網:

https://prometheus.io/

 

(2)背景:

Prometheus是由前Google工程師從2012年開始在Soundcloud以開源軟體的形式進行研發的系統監控和告警的軟體。自此以後,許多公司和組織都採用了Prometheus作為監控告警工具。

Prometheus的開發者和使用者社群非常活躍,它現在是一個獨立的開源專案,可以獨立於任何公司進行維護。為了證明這一點,Prometheus於20165月加入CNCF基金會,成為繼Kubernetes之後的第二個CNCF託管專案。

(3)優勢:

◽ 由指標名稱和鍵/值對標籤標識的時間序列資料組成的多維資料模型

◽強大的查詢語言引擎PromQL

◽不依賴分散式儲存,單個服務節點具有自治能力

◽時間序列資料是服務通過HTTP協議主動拉去獲得的

◽ 也可以通過中間閘道器來推送時間序列資料

◽ 可以通過靜態配置檔案或服務發現來獲取監控指標

◽支援多種型別的圖示和儀表盤

(4)Prometheus的生態元件

◽Prometheus Server:收集和儲存時間序列資料

◽ Client Library:客戶端庫,目的在於為那些期望原生提供Instrumentation功能的應用程式提供便捷的開發途徑

◽ Push Gateway:接收那些通常由短期作業生成的指標資料的閘道器,並支援由Prometheus Server進行指標拉取操作

◽ Exporters:用於暴露現有應用程式或服務(不支援Instrumentation)的指標給Prometheus Server

◽ Alertmanager:從Prometheus Server接收到“告警通知”後,通過去重、分組、路由等預處理功能後以高效想使用者完成告警資訊傳送

◽ Data Visualization:資料視覺化與資料匯出,Granfana等

◽ Service Discovery:動態發現待監控的Target,從而完成監控配置的重要元件,在容器化環境中尤為重要,該元件目前由Prometheus Server內建支援

◽ peometheus alerting:報警通知

(5)Prometueu的架構

Prometheus Server直接從監控目標中或者間接通過推送閘道器來拉去監控資料,它在本地儲存所有抓取到的樣本資料,並對此資料執行一系列規則,以彙總和記錄現有資料的新時間序列或生成告警。可以通過grafana或者其他工具來實現監控資料的視覺化。

二、二進位制部署peometheus監控系統

2.1 prometheus.io官網下載並解壓二進位制包

 master節點

[root@master1 apps]# ln -sv /apps/prometheus-2.33.4.linux-amd64 /apps/prometheus
‘/apps/prometheus’ -> ‘/apps/prometheus-2.33.4.linux-amd64’
[root@master1 apps]# cd /apps/prometheus
[root@master1 prometheus]# ll
total 196068
drwxr-xr-x 2 3434 3434        38 Feb 23 00:59 console_libraries
drwxr-xr-x 2 3434 3434       173 Feb 23 00:59 consoles
-rw-r--r-- 1 3434 3434     11357 Feb 23 00:59 LICENSE
-rw-r--r-- 1 3434 3434      3773 Feb 23 00:59 NOTICE
-rwxr-xr-x 1 3434 3434 104419379 Feb 23 00:54 prometheus  #Prometheus服務可執行程式
-rw-r--r-- 1 3434 3434       934 Feb 23 00:59 prometheus.yml #prometheus配置檔案
-rwxr-xr-x 1 3434 3434  96326544 Feb 23 00:57 promtool #測試工具,用於檢測配置peometheus配置檔案,檢測meteics資料等

 [root@master1 prometheus]# ./promtool check config ./prometheus.yml #檢查配置檔案
 Checking ./prometheus.yml
 SUCCESS: ./prometheus.yml is valid prometheus config file syntax


 [root@master1 prometheus]# ./prometheus  #測試方式啟動prometheus(該方式開機不能自啟)

 

 2.2 建立prometheus service啟動指令碼

[root@master1 ~]# vim /etc/systemd/system/prometheus.service
[Unit] Description
=Prometheus Service Documentation=https://prometheus.io/docs/introduction/overview/ After=network.target [Service] Restart=on-failure WorkingDirectory=/apps/prometheus/ ExecStart=/apps/prometheus/prometheus --config.file=/apps/prometheus/prometheus.yml [Install] WantedBy=multi-user.targe
[root@master1 prometheus]# ss -tnl  #9090未被監聽
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      32768               192.168.181.110:10248                                         *:*                  
LISTEN     0      32768                             *:30088                                         *:*                  
LISTEN     0      32768               192.168.181.110:10249                                         *:*                  
LISTEN     0      32768               192.168.181.110:10250                                         *:*                  
LISTEN     0      32768                     127.0.0.1:9099                                          *:*                  
LISTEN     0      32768                             *:30443                                         *:*                  
LISTEN     0      32768               192.168.181.110:6443                                          *:*                  
LISTEN     0      511                       127.0.0.1:6443                                          *:*                  
LISTEN     0      32768                     127.0.0.1:36301                                         *:*                  
LISTEN     0      128                               *:111                                           *:*                  
LISTEN     0      32768               192.168.181.110:10256                                         *:*                  
LISTEN     0      32768               192.168.181.110:10257                                         *:*                  
LISTEN     0      8                                 *:179                                           *:*                  
LISTEN     0      32768                             *:30004                                         *:*                  
LISTEN     0      32768                             *:30005                                         *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      32768                          [::]:10081                                      [::]:*                  
LISTEN     0      32768                          [::]:10251                                      [::]:*                  
LISTEN     0      128                            [::]:111                                        [::]:*                  
LISTEN     0      32768                          [::]:80                                         [::]:*                  
LISTEN     0      32768                          [::]:10259                                      [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
[root@master1 prometheus]# systemctl enable prometheus.service
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /etc/systemd/system/prometheus.service.
[root@master1 prometheus]# systemctl restart prometheus.service
[root@master1 prometheus]# ss -tnl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      32768               192.168.181.110:10248                                         *:*                  
LISTEN     0      32768                             *:30088                                         *:*                  
LISTEN     0      32768               192.168.181.110:10249                                         *:*                  
LISTEN     0      32768               192.168.181.110:10250                                         *:*                  
LISTEN     0      32768                     127.0.0.1:9099                                          *:*                  
LISTEN     0      32768                             *:30443                                         *:*                  
LISTEN     0      32768               192.168.181.110:6443                                          *:*                  
LISTEN     0      511                       127.0.0.1:6443                                          *:*                  
LISTEN     0      32768                     127.0.0.1:36301                                         *:*                  
LISTEN     0      128                               *:111                                           *:*                  
LISTEN     0      32768               192.168.181.110:10256                                         *:*                  
LISTEN     0      32768               192.168.181.110:10257                                         *:*                  
LISTEN     0      8                                 *:179                                           *:*                  
LISTEN     0      32768                             *:30004                                         *:*                  
LISTEN     0      32768                             *:30005                                         *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      32768                          [::]:10081                                      [::]:*                  
LISTEN     0      32768                          [::]:9090                                       [::]:*                  
LISTEN     0      32768                          [::]:10251                                      [::]:*                  
LISTEN     0      128                            [::]:111                                        [::]:*                  
LISTEN     0      32768                          [::]:80                                         [::]:*                  
LISTEN     0      32768                          [::]:10259                                      [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
[root@master1 prometheus]# 

2.3 瀏覽器訪問

2.4 node-exporter部署

      node節點

下載並解壓node-exporter壓縮包

[root@node1 apps]# ls
node_exporter-1.3.1.linux-amd64.tar.gz  node-exporter-1.3.1-onekey-install.tar.gz
node-exporter-1.3.1-onekey-install.sh   node-exporter.service
[root@node1 apps]# tar -xvf node_exporter-1.3.1.linux-amd64.tar.gz
node_exporter-1.3.1.linux-amd64/
node_exporter-1.3.1.linux-amd64/LICENSE
node_exporter-1.3.1.linux-amd64/NOTICE
node_exporter-1.3.1.linux-amd64/node_exporter
[root@node1 apps]# ls
node_exporter-1.3.1.linux-amd64         node-exporter-1.3.1-onekey-install.sh      node-exporter.service
node_exporter-1.3.1.linux-amd64.tar.gz  node-exporter-1.3.1-onekey-install.tar.gz
[root@node1 apps]# ln -sv /apps/node_exporter-1.3.1.linux-amd64 /apps/node_exporter  #軟連線/apps/node_exportr’ -> ‘/apps/node_exporter-1.3.1.linux-amd64’

2.5 編寫service啟動檔案

[root@node1 apps]# cat /etc/systemd/system/node-exporter.service 
[Unit]
Descirption=Prometheus Node Exporter
After=network.target

[Service]
ExecStart=/apps/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target
開機自啟

  [root@node1 apps]# systemctl restart node-exporter.service

  [root@node1 apps]# systemctl enable node-exporter.service

[root@node1 apps]# ss -ntl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      32768               192.168.181.140:10250                                         *:*                  
LISTEN     0      32768                     127.0.0.1:9099                                          *:*                  
LISTEN     0      32768                             *:30443                                         *:*                  
LISTEN     0      511                       127.0.0.1:6443                                          *:*                  
LISTEN     0      128                               *:111                                           *:*                  
LISTEN     0      32768               192.168.181.140:10256                                         *:*                  
LISTEN     0      8                                 *:179                                           *:*                  
LISTEN     0      32768                             *:30004                                         *:*                  
LISTEN     0      32768                             *:30005                                         *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      32768               192.168.181.140:10248                                         *:*                  
LISTEN     0      32768                             *:30088                                         *:*                  
LISTEN     0      32768                     127.0.0.1:36393                                         *:*                  
LISTEN     0      32768               192.168.181.140:10249                                         *:*                  
LISTEN     0      32768                          [::]:9100                                       [::]:*                  
LISTEN     0      128                            [::]:111                                        [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      32768                          [::]:9094                                       [::]:*  

2.6 瀏覽器訪問

prometheus service上對prometheus主配置檔案進行配置

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.  #資料收集間隔時間,預設1分鐘。
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.      #資料掃描時間間隔,預設1分鐘。
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:       #報警通知配置
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:   #規則配置
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs: #資料採集目標配置
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

      -job_name: "prometheus-node"   #任務名字
       static_configs:   #靜態發現
         - targets: ["192.168.181.140:9100","192.168.181.141:9100","192.168.181.142:9100",]  #目標列表

2.7 儲存重啟prometheus並檢視埠是否起來

[root@master1 prometheus]# systemctl restart prometheus.service 
[root@master1 prometheus]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.181.110:10248   0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:30088           0.0.0.0:*               LISTEN     
tcp        0      0 192.168.181.110:10249   0.0.0.0:*               LISTEN     
tcp        0      0 192.168.181.110:10250   0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:9099          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:30443           0.0.0.0:*               LISTEN     
tcp        0      0 192.168.181.110:6443    0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6443          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:36301         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 192.168.181.110:10256   0.0.0.0:*               LISTEN     
tcp        0      0 192.168.181.110:10257   0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:179             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:30004           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:30005           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 :::10081                :::*                    LISTEN     
tcp6       0      0 :::9090                 :::*                    LISTEN     
tcp6       0      0 :::10251                :::*                    LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::10259                :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
[root@master1 prometheus]# 

2.7 可以通過瀏覽器訪問prometheus頁面看到node節點的資料

三、Grafana部署

3.1 下載grafana的rpm包

[root@master1 prometheus]# wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.3.7-1.x86_64.rpm
[root@master1 prometheus]# sudo yum install grafana-enterprise-8.3.7-1.x86_64.rpm
[root@master1 ~]# systemctl restart grafana-server
[root@master1 ~]# systemctl enable grafana-server
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.

3.2 新增資料來源

3.3 點選進去

.3.4 儲存成功說明資料來源沒問題

3.5 官方圖形模板下載

 3.6 使用官網獲得的模板的ID進行匯入

 3.7 新增資料來源,然後建立

 3.8 模板匯入成功