Prometheus+Grafana監控MySQL、Redis數據庫


 俗話說,沒有監控的系統就是在裸奔,好的監控就是運維人員的第三只手,第三只眼。本文將使用prometheus及Grafana搭建一套監控系統來監控主機及數據庫(MySQL、Redis)。

1.  安裝Grafana

Grafana是一個可視化面板(Dashboard),有着非常漂亮的圖表和布局展示,功能齊全的度量儀表盤和圖形編輯器,支持Graphite、zabbix、InfluxDB、Prometheus等數據源。

1.1 下載並安裝

下載地址:https://grafana.com/grafana/download

選擇最新的版本進行安裝,按照網站的提示運行腳本即可(監控服務器需可訪問外網,如無法訪問外網可與我溝通如何離線快速部署)。

運行如下腳本

wget https://dl.grafana.com/oss/release/grafana-6.3.3-1.x86_64.rpm
sudo yum localinstall grafana-6.3.3-1.x86_64.rpm

1.2  啟動grafana

安裝完成后,grafana服務默認已安裝,配置文件為/etc/grafana/grafana.ini,如需修改路徑及端口,可在該文件中修改

啟動grafana

 /etc/init.d/grafana-server  start

 

1.3  登錄grafana

 訪問頁面http://服務器IP:3000 ,默認賬號、密碼admin/admin  首次登錄將提示修改密碼,建議修改

 

2.  安裝Prometheus

2.1   Prometheus 主程序安裝

 Prometheus 主程序,主要是負責存儲、抓取、聚合、查詢方面

可登錄官網進行下載,官網下載地址:https://prometheus.io/download/

根據操作系統類別選擇文件進行下載,本次部署在linux上

/**  下載*/
wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz 

/**  解壓*/
tar -zxvf prometheus-2.12.0.linux-amd64.tar.gz

2.2  啟動prometheus主程序

生產環境可參考如下方式啟動

/** 生產環境啟動*/
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &

/**   
 --web.enable-lifecycle  加上此參數可以遠程熱加載配置文件,無需重啟prometheus,調用指令是curl -X POST http://ip:9090/-/reload
-- storage.tsdb.retention.time 數據默認保存時間為15天,啟動時加上此參數可以控制數據保存時間

*/

其他的參數及配置可以在prometheus.yml中調整及配置

 

3.  在需監控的機器上部署exporter

 3.1   監控linux主機

下載監控linux主機的node_exporter,依舊從官網下載

/**  下載  */
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

/**  解壓  */
tar  -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

可以按照默認方式啟動

/** 啟動 node_exporter*/
cd  node_exporter-0.18.1.linux-amd64
nohup ./node_exporter  &
/**
默認端口9100
*/

3.2  監控MySQL

3.2.1 下載

 下載監控MySQL的mysqld_exporter,依舊從官網下載

/**  下載  */
wget  https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

/**  解壓  */
tar -zxvf  mysqld_exporter-0.12.1.linux-amd64.tar.gz

 

3.2.2  監控賬號及修改文件配置

在MySQL里配置MySQL監控賬號

/**  創建賬號  */
mysql> CREATE USER 'mysql_monitor'@'localhost' identified by 'mysql_monitor'; 
/** 授權 */
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysql_monitor'@'localhost'; 
mysql> GRANT SELECT ON performance_schema.* TO 'mysql_monitor'@'localhost'; 

/** 
注意,不用版本對權限要求不一致,啟動時注意查看日志,如權限不足則繼續授權或創建對應的賬號 
*/

配置文件修改

cd mysqld_exporter-0.12.0.linux-amd64

vim .my.cnf 
/**  添加如下配置 */
[client]
port=3306
user=mysql_monitor
password=mysql_monitor

3.2.3 啟動監控腳本

nohup   ./mysqld_exporter --config.my-cnf=.my.cnf  & 

 

3.3  監控redis

 3.3.1 下載redis_exporter

官網上沒有redis_exporter, 可以從github上獲取,另外redis插件無需放在redis機器上也可以

/**  下載  */
wget https://github.com/oliver006/redis_exporter/releases/download/v0.30.0/redis_exporter-v0.30.0.linux-amd64.tar.gz
/**  解壓  */
tar -zxvf  redis_exporter-v0.30.0.linux-amd64.tar.gz

3.3.2  啟動redis_exporter

/**  redis無密碼 */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6379 -web.listen-address 0.0.0.0:9121  &

/**  redis有密碼  */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6479 -redis.password 123456   -web.listen-address 0.0.0.0:9122 & 

/**   
 -web.listen-address  可以自定義監控端口
*/

 

4.  配置prometheus配置文件

4.1  添加各監控項

配置文件可以有多種配置方式,可以根據不同的分類和習慣配置。可參考如下方式配置

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

  - job_name: 'OS'

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

    static_configs:
    - targets: ['192.168.56.114:9100']
      labels:
          instance: '192.168.56.114'
          
    - targets: ['192.168.56.116:9100']
      labels:
          instance: '192.168.56.116'

    - targets: ['192.168.56.117:9100']
      labels:
          instance: '192.168.56.117'
##  上述job單獨做主機監控,每台主機的instance不同

  - job_name: 'mysql'

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

    static_configs:
    - targets: ['192.168.56.116:9104']
      labels:
          instance: '192.168.56.116'
    
    - targets: ['192.168.56.117:9104']
      labels:
          instance: '192.168.56.117'

  ## 以上是監控mysql的,instance和主機的instance的相同

  - job_name: 'redis'

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

    static_configs:
    - targets: ['192.168.56.118:9121','192.168.56.118:9122']
      labels:
          instance: '192.168.56.118'

    - targets: ['192.168.56.118:9100']
      labels:
          instance: '192.168.56.118'    
#   可以類似上述這種,redis的主機及各redis監控項組合在一起,instance使用相同的

 

4.2  啟動或熱加載prometheus

/**  啟動  */
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &

/**   
-- storage.tsdb.retention.time 數據默認保存時間為15天,啟動時加上此參數可以控制數據保存時間
*/

/**  熱加載  */
curl -X POST http://ip:9090/-/reload 


/**   
熱加載的前提是啟動時加了--web.enable-lifecycle 
*/

 

5.  配置各監控儀表盤

5.1  下載各監控儀表盤

以上模板grafana官方網站均有,可以根據自己的需要下載對應的模板,對應地址為https://grafana.com/grafana/dashboards

找到對應的儀表盤模板后進入下載

 

 

5.2  配置數據源

本次使用的均為prometheus數據源,因此配置一個prometheus的數據源

如果之前在grafana上沒有配置過數據源 登錄后會提示創建

 選擇prometheus

 

配置prometheus地址

最終save & Test即可

 

5.3 導入儀表盤

將5.1中下載的模板導入

導入

修改名稱及數據源

import即可

5.4 配置完成后即可查看各監控情況

主機監控如下

 

MySQL

 

 

Redis

 其他如果需要其他監控項也可以自定義添加

 


免責聲明!

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



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