1. 程式人生 > 實用技巧 >Promethus(普羅米修斯)監控系統

Promethus(普羅米修斯)監控系統

文章目錄


前言

安裝,配置和使用簡單的Prometheus例項。您將在本地下載並執行Prometheus,對其進行配置以抓取自身和示例應用程式,然後使用查詢,規則和圖形使用收集的時間序列資料。


一、什麼是Prometheus(普羅米修斯)?

Prometheus是最初在SoundCloud上構建的開源系統監視和警報工具包 。自2012年成立以來,許多公司和組織都採用了Prometheus,該專案擁有非常活躍的開發人員和使用者社群。現在,它是一個獨立的開源專案,並且獨立於任何公司進行維護。為了強調這一點並闡明專案的治理結構,Prometheus在2016年加入了 Cloud Native Computing Foundation,這是繼Kubernetes之後的第二個託管專案。

1.1特徵

  • prometheus server:主要用於抓取資料和儲存時序資料,另外還提供查詢和 Alert Rule 配置管理
  • client libraries:用於對接 Prometheus Server,檢測應用程式程式碼可以查詢和上報資料
  • push gateway:用於批量,短期的監控資料的彙總節點,主要用於業務資料彙報等
  • exporters:各種彙報exporter,例如node_exporter,mysql_exporter,HAProxy,StatsD,Graphite
  • alertmanager:告警通知管理

1.2架構

下圖說明了Prometheus的體系結構及其某些生態系統元件:


Prometheus直接或通過中間推送閘道器從已檢測作業中刪除指標,以用於短期作業。它在本地儲存所有報廢的樣本,並對這些資料執行規則,以彙總和記錄現有資料中的新時間序列,或生成警報。Grafana或其他API使用者可以用來視覺化收集的資料。

二、下載Prometheus

個人喜好在根目錄下建立一個資料夾把軟體下載過去

建立資料夾
# mkdir -p /data/source
# cd /data/source/
官網下載連結
# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

三、安裝

3.1下載最新版本的Prometheus後

解壓縮到/usr/local/share/prometheus
#tar -zxvf prometheus-2.23.0.linux-amd64.tar.gz -C /usr/local/share/
進入資料夾
#cd /usr/local/share/
給他起個短一點的名字
#mv prometheus-2.23.0.linux-amd64 prometheus
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Prometheus伺服器是一個稱為prometheus的二進位制檔案。我們可以執行二進位制檔案,並通過傳遞–help標誌來檢視有關其選項的幫助。

檢視幫助
#./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
      --version                  Show application version.
      --config.file="prometheus.yml"  
                                 Prometheus configuration file path.
      --web.listen-address="0.0.0.0:9090"  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

*在啟動Prometheus之前,先對其進行配置。

3.2配置Prometheus

Prometheus配置為YAML。Prometheus下載在一個名為的檔案中帶有一個示例配置,prometheus.yml這是一個入門的好地方。

刪除了示例檔案中的大多數註釋,以使其更加簡潔(註釋以開頭的行#)。

配置如下:

global:		#global塊控制Prometheus伺服器的全域性配置
  scrape_interval:     15s		#scrape_interval控制,Prometheus多久刮一次目標。
  evaluation_interval: 15s		#evaluation_interval選項控制Prometheus多久評估一次規則。

alerting:
  alertmanagers:
  - static_configs:
    - targets:

rule_files:		#rule_files塊指定希望Prometheus伺服器載入的任何規則的位置。
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:		#scrape_configs控制Prometheus監視哪些資源。
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.3執行普羅米修斯

要使用我們新建立的配置檔案啟動Prometheus,轉到包含Prometheus二進位制檔案的目錄並執行:

./prometheus --config.file=prometheus.yml
  • 1

執行log

理論上來說這樣就算是安裝完成了,但這樣太簡陋了。因為每次啟動 Prometheus server 都需要手動執行命令:

# /usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml
  • 1

這實在是太不方便了!應該把它配置成服務,用 systemd 來管理。

四、新增到systemd來管理

4.1先建立一個名為 prometheus 的使用者:

# adduser prometheus
  • 1

把目錄 /usr/local/share/prometheus/ 的所有者設定為 prometheus 使用者:

# chown -R prometheus:prometheus /usr/local/share/prometheus/
  • 1

然後建立檔案 /etc/systemd/system/prometheus.service,內容如下:

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
User=prometheus
Restart=on-failure
WorkingDirectory=/usr/local/share/prometheus/
ExecStart=/usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

好了,現在可以通過 systemd 來控制 Prometheus 服務了,
重新載入systemctl服務:

# systemctl daemon-reload
  • 1

啟動服務:

# systemctl start prometheus
  • 1

再把服務配置為開機時啟動:

# systemctl enable prometheus
  • 1

檢查一下服務的狀態:

# systemctl status prometheus
  • 1

五、web端

現在可以在瀏覽器上瀏覽到有關其自身的狀態頁。

還可以通過導航到自己的指標終結點:http:// localhost:9090 / metrics來驗證Prometheus是否正在提供有關自身的指標。

5.1用瀏覽器檢視資料

讓我們嘗試檢視Prometheus收集的有關自身的一些資料。要使用Prometheus的內建表示式瀏覽器,請導航至 http:// localhost:9090 / graph並在“圖形”選項卡中選擇“控制檯”檢視。

正如您可以從http:// localhost:9090 / metrics收集的那樣,稱為Prometheus匯出的有關其自身的一個指標 promhttp_metric_handler_requests_total(/metricsPrometheus伺服器已處理的請求總數)。繼續並將其輸入到表示式控制檯中:

promhttp_metric_handler_requests_total
  • 1


Graphite InfluxDB Kapacitor OpenTSDB Nagios Sensu


六、使用NODE EXPORTER監視LINUX主機指標

Node Exporter公開了各種與硬體和核心相關的指標。

在localhost配置為從執行的Node Exporter刮取指標的Prometheus例項上啟動

安裝並執行節點匯出器

Prometheus Node Exporter是一個單個靜態二進位制檔案,可以通過tarball安裝。從Prometheus下載頁面下載後,將其解壓縮並執行:

# wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
./node_exporter
  • 1
  • 2
  • 3
  • 4

您應該看到類似以下的輸出,表明節點匯出器正在執行,並且在埠9100上公開了指標:

節點匯出器指標

安裝並執行節點匯出器後,可以通過對/metrics端點進行cURL驗證來匯出指標:

http://localhost:9100/metrics
  • 1

您應該看到如下輸出:

配置您的Prometheus例項

需要正確配置本地執行的Prometheus例項,才能訪問Node Exporter指標。以下prometheus.yml示例配置檔案將通過以下命令告訴Prometheus例項從Node Exporter進行抓取以及抓取頻率localhost:9100:

本節說明如何下載和安裝Grafana,如何在基於RPM的Linux系統上啟動並執行該服務,以及安裝軟體包的詳細資訊。

6.1下載並安裝

# wget <rpm package url>
  • 1
# rpm -Uvh <local rpm package>
  • 1

6.2啟動伺服器

grafana-server將以grafana使用者身份啟動該過程,該過程是在軟體包安裝期間建立的。systemd命令在大多數情況下都可以使用,但是某些較舊的Linux系統可能需要init.d。安裝程式應提示您輸入正確的命令。

如果安裝了.rpm軟體包,則可以使用systemd或啟動伺服器init.d。如果安裝了二進位制.tar.gz檔案,則需要執行二進位制檔案。

6.2.1用systemd啟動伺服器

要啟動服務並驗證服務已啟動:

# systemctl daemon-reload
# systemctl start grafana-server
# systemctl status grafana-server
  • 1
  • 2
  • 3

配置Grafana伺服器以在啟動時啟動:

# systemctl enable grafana-server