監控軟件篇——prometheus+exporter組件+grafana


環境

linux: centos7.6

  • 公司里最常用的服務器系統

prometheus: 2.13.1.linux-amd64

  • 基於時間序列的數值數據的容器監控解決方案: 自帶了數據存儲和監控,是k8s官方指定監控方案。

node_exporter: 0.18.1.linux-amd64

  • 用於采集服務器層面的運行指標,然后轉為prometheus能夠識別的格式,然后建立監聽等中央服務主動來抓取數據

process-exporter: 0.5.0.linux-amd64

  • 主要用於監控服務進程的,也是將數據轉為固定格式,等中央服務器主動來抓取

grafana: 6.4.3.linux-amd64 >>>官方文檔https://grafana.com/docs/grafana/latest/

  • 一個成熟完整的展示數據的工具,可以整個市場大部分的數據源

准備安裝包
prometheus-2.13.1.linux-amd64.tar.gz
process-exporter-0.5.0.linux-amd64.tar.gz
node_exporter-0.18.1.linux-amd64.tar.gz
grafana-6.4.3.linux-amd64.tar.gz

  • 本文安裝均通過二進制包,解壓后直接使用

安裝prometheus

下載最新版本,並解壓

下載地址:https://prometheus.io/download/

tar xvfz prometheus-*.tar.gz
cd prometheus-*

以下是基本的配置,完整文檔介紹可以看 > 官方文檔:https://prometheus.io/docs/prometheus/2.13/configuration/configuration/

# my global config
global:
scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# 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']

如果后面配置了exporter組件,可以在scrape_configs里添加job,例如:

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: 'node_exporter'
 static_configs:
 - targets: ['localhost:9100']

啟動

# Start Prometheus.
# 默認情況下,Prometheus會將其數據庫存儲在./data (flag—storage.tsdb.path)中
$ cd prometheus-2.13.1.linux-amd64/
$ nohup ./prometheus --config.file=prometheus.yml >/dev/null &
  • 驗證:可以通過http://ip:9090訪問瀏覽器,或者http://ip:9090/metrics看是否能提供關於自身的各項指標

安裝node_exporter

下載二進制安裝包,安裝並啟動

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
nohup ./node_exporter >/dev/null &
  • 啟動不需要帶配置文件,此時服務器會有9100的監聽端口。
  • 驗證: 可以通過訪問瀏覽器http://ip:9100/metrics訪問服務器運行指標

安裝process_exporter

下載二進制安裝包

$ wget https://github.com/ncabatoff/process-exporter/releases/download/v0.5.0/process-exporter-0.5.0.linux-amd64.tar.gz
$ tar -xvf  process-exporter-0.5.0.linux-amd64.tar.gz
$ ll
-rw-r--r-- 1 3434 3434    1076 Apr 14  2019 LICENSE
-rwxr-xr-x 1 3434 3434 7681088 Apr 14  2019 process-exporter
-rw-r--r-- 1 3434 3434   12958 Apr 14  2019 README.md

  • 這個解壓完后默認是沒有配置文件的,需要我們自己寫一個,啟動時通過-config.path 指定配置文件。
    讀配置文件的本質就是,讀里面配置的服務名,通過服務名稱去進行pid的查找和分析,以下為范例<這里使用{{.Matches}}模板>匹配mysql和prometheus兩個服務:
$ vim process.yaml
process_names:
  - name: "{{.Matches}}"
    cmdline:
    - 'mysqld'

  - name: "{{.Matches}}"
    cmdline:
    - 'prometheus'

啟動

# Start process_exporter.
nohup ./process-exporter -config.path process.yaml >/dev/null &
  • 驗證:訪問瀏覽器http://ip:9256/metrics ,會返回配置文件里指定的服務進程信息

安裝grafana

下載二進制安裝包

wget https://dl.grafana.com/oss/release/grafana-6.4.3.linux-amd64.tar.gz 
tar -zxvf grafana-6.4.3.linux-amd64.tar.gz 

啟動

  • 這里使用官方說明的二進制啟動方式,有點忒簡單了,正式項目建議都配置為system系統服務
 $ cd grafana-6.4.3
 $ nohup ./bin/grafana-server web >/dev/null &
  • 驗證:訪問瀏覽器http://ip:3000, 默認的賬號密碼為: admin / admin

使用grafana配置prometheus數據源

進入grafana管理頁面

配置prometheus數據源

  • 點擊左邊齒輪圖標————>> Add data source —————>>點擊prometheus

  • 拉到最下面點擊 Save&Test, 然后再點齒輪就可以看到配置的數據源了

添加儀表盤<可直接看最后一步>

  • 點擊左邊的加號圖標————>>點擊Choose Visualization(添加儀表盤)

給儀表盤添加數據源<可直接看最后一步>

  • 點擊左邊的方塊圖標————>>選擇剛保存的儀表盤————>>點擊儀表盤的Title

使用現有儀表盤模板

  • 點擊左邊加好圖標————>>import————>>在Grafana.com Dashboard中填入8919,然后等待幾秒ok

  • 最后大功告成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM