1. 程式人生 > 實用技巧 >新一代監控神器Prometheus+Grafana介紹及使用

新一代監控神器Prometheus+Grafana介紹及使用

一、介紹

1.什麼是Prometheus?

普羅米修斯是一個開源的系統監控及報警工具,2016年加入了Cloud Native Computing Foundation是繼Kubernetes之後的第二個託管專案。

2.Prometheus的特徵有什麼?

  • 具有由metric名稱和鍵值對標示的時間序列資料的多位資料模型
  • 有一個靈活的查詢語言promQL
  • 不依賴分散式儲存,只和本地磁碟有關
  • 通過HTTP來拉取(pull)時間序列資料
  • 也支援推送(push)方式新增時間序列資料
  • 多種圖形和儀表盤支援

3.Prometheus的元件都有哪些?來張官方圖:

  • Prometheus Server 用於定時抓取資料
    指標(metrics)、儲存時間序列資料(TSDB)
  • Jobs/exporte 收集被監控端資料並暴露指標給Prometheus
  • Pushgateway 監控端的資料會用push的方式主動傳給此元件,隨後被Prometheus 服務定時pull此元件資料即可
  • Alertmanager 報警元件,可以通過郵箱、微信等方式
  • Web UI 用於多樣的UI展示,一般為Grafana
  • 還有一些例如配置自動發現目標的小元件和後端儲存元件

4.什麼時候使用Prometheus

  • 監控的物件動態可變,無法預先配置的時候
  • Prometheus 是專為雲環境(k8s/docker)提供的監控工具
  • 想要更直觀更簡單的直接觀察某項指標的資料變化時

5.看到一個寫的非常不錯的關Prometheus儲存的文章

https://www.cnblogs.com/zqj-blog/p/12205063.html

二、搭建

1.安裝Prometheus

官網下載地址:https://prometheus.io/download/選擇自己所需版本即可

## 解壓安裝
tar zxf prometheus-2.22.0.linux-amd64.tar.gz -C /opt/vfan/
mv prometheus-2.22.0.linux-amd64 prometheus-2.22.0
cd prometheus-2.22.0/

## 可以通過--help或--version檢視服務啟動引數和版本等
./prometheus --help
./prometheus --version

## 啟動服務,並指定配置檔案
nohup ./prometheus --config.file="prometheus.yml" &> /dev/null &

## 檢視端口占用情況(預設9090)
[root@VM-0-10-centos prometheus-2.22.0]# ss -tnlp
State       Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port
LISTEN      0      50                                                  *:3306                                                            *:*                   users:(("mysqld",pid=28673,fd=14))
LISTEN      0      128                                                 *:22                                                              *:*                   users:(("sshd",pid=1306,fd=3))
LISTEN      0      128                                                :::80                                                             :::*                   users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4))
LISTEN      0      128                                                :::9090                                                           :::*                   users:(("prometheus",pid=11771,fd=10))
或者直接使用docker容器執行,直接掛載一下配置檔案:
docker run \
    -p 9090:9090 \
    -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

檢視預設prometheus.yml檔案:vim prometheus.yml

# 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']

目前只在監控Prometheus本機

可以登入普羅米修斯(伺服器ip:9090)web介面,Status—>Rules下檢視目前正在監控的目標

可以看到獲取監控資訊的終點是 本機ip+埠+/metrics:

也可以檢視監控圖形:Graph—>選擇監控項—>Execute

這種圖形介面顯然不太直觀,所以引入Grafana。

2.安裝node-exporter外掛,新增監控機器

下載連結:https://prometheus.io/download/選擇自己所需版本即可

## 解壓安裝
tar zxf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/vfan/
mv node_exporter-1.0.1.linux-amd64 node_exporter
cd node_exporter/

## 可以檢視服務啟動引數
./node_exporter --help
    --web.listen-address=":9100"    #可以指定監聽埠
    --collector.ntp.server="127.0.0.1"  #可以指定ntp server

## 直接執行即可,--web.listen-address引數可以指定監聽埠,預設9100。 
nohup ./node_exporter --web.listen-address=":9100" &> /dev/null &

[root@VM-0-10-centos node_exporter]# ss -tnlp
State       Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port
LISTEN      0      50                                                  *:3306                                                            *:*                   users:(("mysqld",pid=28673,fd=14))
LISTEN      0      128                                                 *:22                                                              *:*                   users:(("sshd",pid=1306,fd=3))
LISTEN      0      128                                                :::9100                                                           :::*                   users:(("node_exporter",pid=26134,fd=3))
LISTEN      0      128                                                :::80                                                             :::*                   users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4))
LISTEN      0      128             

prometheus.yaml中新增node_exporter配置

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

然後重啟普羅米修斯服務,重啟後再次檢視監控目標:

已經開始監控新的node

3.安裝Grafana

下載連結:https://grafana.com/grafana/download

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

## 解壓安裝
tar zxf grafana-7.2.2.linux-amd64.tar.gz -C /opt/vfan/
cd grafana-7.2.2

## 檢視啟動引數
./grafana-server --help

## 啟動服務,預設埠3000
nohup ./grafana-server &> /dev/null &

[root@VM-0-10-centos conf]# ss -tnlp
State       Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port
LISTEN      0      50                                                  *:3306                                                            *:*                   users:(("mysqld",pid=28673,fd=14))
LISTEN      0      128                                                 *:22                                                              *:*                   users:(("sshd",pid=1306,fd=3))
LISTEN      0      128                                                :::9100                                                           :::*                   users:(("node_exporter",pid=26134,fd=3))
LISTEN      0      128                                                :::80                                                             :::*                   users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4))
LISTEN      0      128                                                :::3000                                                           :::*                   users:(("grafana-server",pid=31050,fd=10))
LISTEN      0      128    

Grafana預設的配置檔案為:vim grafana-7.2.2/conf/defaults.ini;主要有監聽埠、日誌路徑、預設登入帳號密碼等

[server]
# Protocol (http, https, h2, socket)
protocol = http

# The ip address to bind to, empty will bind to all interfaces
http_addr =

# The http port to use
http_port = 3000

# The public facing domain name used to access grafana from a browser
domain = localhost

[security]
# disable creation of admin user on first start of grafana
disable_initial_admin_creation = false

# default admin user, created on startup
admin_user = admin

# default admin password, can be changed before first start of grafana, or in profile settings
admin_password = admin

現在可以通過ip+埠方式來訪問Grafana:

第一次登陸會強制性修改密碼,修改後即可進入

4.配置Grafana,增加視覺化模板

第一步:新增資料來源

選擇Prometheus,只需將URL修改為Prometheus服務地址,其餘預設即可(也可自行修改):

可以將Prometheus服務及Grafana服務的監控模板匯入進去:

但要注意,匯入Grafana的模板後,要在Prometheus.yml增加Grafana的監控:vim prometheus.yml

scrape_configs:
  - job_name: 'grafana'
    static_configs:
    - targets: ['localhost:3000']

點選儲存,儲存後檢視資料來源:

檢視剛剛匯入的模板,已經形成監控圖形:

至此,Prometheus+Grafana基本元件搭建完成。

三、配置Grafana模板,配合Prometheus使用

1、監控系統指標

前提條件:

  • 被監控的主機系統上已經安裝node_exporter
  • Prometheus.yml中已經新增此主機的Job

也就是以上第二步的第2點

前提條件準備完畢後,我們可以找一些實用且直觀的模板來直接套用,不僅可以節省時間成本,實際效果也相當不錯,如果有什麼地方不能滿足自己的需求,還可以在此基礎上修改:

前往Grafana的官網下載Dashboard模板:https://grafana.com/grafana/dashboards

選擇Prometheus,再根據關鍵字搜尋

  (1).點進去一個node_exporter的模板,可以檢視樣圖,然後直接下載JSON檔案

  (2).點選加號—>import—>Upload JSON file

  (3).模板匯入後,即可進行監控

2、監控mysql服務各項指標

(1).Prometheus官網提供了mysqld的metric指標採集外掛,可以直接下載:https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

## 解壓即可
tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz

(2).下載安裝完畢後,啟動前,需要在mysql中建立一個Prometheus收集資料的賬號:

mysql> create user 'promethues'@'localhost' IDENTIFIED BY 'promethues1';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select,replication client,process on *.* to 'promethues'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

注意:這裡的的localhost不是指mysqld服務的ip,是指mysqld_exporterip,因為promethues服務來找mysqld獲取資料時,是先找到mysqld_exporter,然後mysqld_exporter再去mysqld獲取資料。所以要保證mysqld_exporterip可以連線mysqld服務。

(3).在mysqld_exporter元件中配置mysql資訊

建立一個儲存mysql使用者名稱密碼的檔案:vim mysqld_exporter/.my.cnf

[client]
user=promethues
password=promethues1

(4).啟動mysqld_exporter元件,配置promethues.yml,並指定mysql賬號資訊檔案

## 可以檢視一些啟動資訊
./mysqld_exporter --help

## 啟動,指定埠號,預設9104,指定連線mysql的使用者檔案
nohup ./mysqld_exporter --web.listen-address=":9104" --config.my-cnf=".my.cnf" &> /dev/null  &
## 新增以下配置:vim prometheus.yml
- job_name: 'mysqld'
    static_configs:
    - targets: ['localhost:9104']

(5).並在Grafana新增mysqld模板

然後還是前往Grafana查詢自己喜歡的模板:

根據上邊的步驟,將JSON檔案匯入,即可生成儀表盤:

以上只是簡單演示了兩個比較常用的外掛,普羅米修斯官方還有許多外掛可供使用。大家可慢慢研究。下文也將介紹Prometheus監控K8S叢集的手段。