Prometheus監控實戰應用

Jeff的技術棧發表於2022-05-04

1.Prometheus的主要特徵及概述

概述:

Prometheus(由go語言(golang)開發)是一套開源的監控&報警&時間序列資料庫的組合。適合監控docker容器。因為kubernetes(俗稱k8s)的流行帶動了prometheus的發展。

時間序列資料特點:
1.效能好
2.儲存成本低,高效的壓縮演算法,節省儲存空間,有效降低IO。

Prometheus有著非常高效的時間序列資料儲存方法,每個取樣資料僅僅佔用3.5byte左右空間,上百萬條時間序列,30秒間隔,保留60天,大概花了200多G(來自官方資料)

特徵:

1.多維度資料模型
2.靈活的查詢語言
3.不依賴分散式儲存,單個伺服器節點是自主的
4.以HTTP方式,通過pull模型拉去時間序列資料
5.也可以通過中間閘道器支援push模型
6.通過服務發現或者靜態配置,來發現目標服務物件
7.支援多種多樣的圖表和介面展示

2.普羅米修斯原理架構圖

3.下載安裝啟動Prometheus

官網下載地址:https://prometheus.io/download/
//下載
wgt https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz
//解壓
tar -xf prometheus-2.35.0.linux-amd64.tar.gz -C /usr/local
//改名
mv prometheus-2.35.0.linux-amd64 prometheus
//預設啟動
nohup ./prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
//埠檢查
lsof -i:9090
ss -naltp |grep 9090
 
//瀏覽器訪問192.168.0.215:9090

4.web客戶端操作

//瀏覽器訪問192.168.0.215:9090
預設監控本機

5.預設影像

6.目錄解釋

console_libraries目錄:
consoles目錄:
LICENSE問價:
NOTICE檔案:
prometheus檔案:預設啟動的可執行檔案
prometheus.yml配置檔案:預設配置檔案
promtool檔案:

7.配置檔案

vi prometheus.yml

global:
  scrape_interval:     60s # 拉取時間間隔
  evaluation_interval: 60s # 告警時間間隔

- job_name: "prometheus"  #監控名稱取名字
    static_configs:
      - targets: ["localhost:9090"] #被監控機器的ip和埠

8.監控指標

指標配置下載:https://prometheus.io/download/

8.1.監控其他機器node_exporter

在其他機器安裝node_exporter,埠9100

第一步:下載安裝node_exporter

//下載
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
//解壓
tar -xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
//改名
mv node_exporter-1.3.1.linux-amd64 node_exporter
//啟動
nohup /usr/local/node_exporter/node_exporter &
//瀏覽器輸入,監控資料,埠9100
http://192.168.0.216:9100/metrics

第二步:配置到prometheus

vi /usr/local/prometheus/prometheus.yml

  - job_name: "node"
    static_configs:
      - targets: ["192.168.0.216:9100"]
        labels:
          instance: 192.168.0.216
          group: node
      - targets: ["192.168.0.247:9100"]
        labels:
          instance: 192.168.0.247
          group: node
      - targets: ["192.168.0.235:9100"]
        labels:
          instance: 192.168.0.235
          group: node
      - targets: ["192.168.0.236:9100"]
        labels:
          instance: 192.168.0.236
          group: node

//重啟prometheus
lsof -i:9090
kill -9 xxxx
nohup ./prometheus &

檢視:

8.2監控mysql指標mysqld_exporter

第一步:下載安裝mysqld_exporter

埠:9104

//下載
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

//解壓
tar -xf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
//改名
mv mysqld_exporter-0.14.0.linux-amd64 mysqld_exporter
//建立mysqld_exporter需要使用mysql的使用者名稱和密碼,檔案需要手動建立
vi /usr/local/mysqld_exporter/my.cnf
[client]
host=192.168.0.215
user=root
password=123456
port=3306
//啟動mysqld_exporter
nohup ./mysqld_exporter --config.my-cnf=./my.cnf &
//檢查埠
lsof -i:9104

第二步:配置到prometheus

vi /usr/local/prometheus/prometheus.yml

- job_name: "sg-215-mysql"
    static_configs:
      - targets: ["192.168.0.215:9104"]

//重啟prometheus
lsof -i:9090
kill -9 xxxx
nohup ./prometheus &

檢視:

8.3監控postgres指標postgres_exporter

第一步:下載安裝postgres_exporter

//下載
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.10.1/postgres_exporter-0.10.1.linux-386.tar.gz
//解壓
tar -xf postgres_exporter-0.10.1.linux-386.tar.gz -C /usr/local/
//改名
mv postgres_exporter-0.10.1.linux-386 postgres_exporter
//新增環境變數
export DATA_SOURCE_NAME="postgresql://postgres:iLoveShark@192.168.0.247:32432/postgres?sslmode=disable"
//啟動
nohup ./postgres_exporter &
//瀏覽器輸入:http://192.168.0.215:9187/metrics

第二步:配置到prometheus

- job_name: "postgreSql"
    static_configs:
      - targets: ["192.168.0.215:9187"]
        labels:
          instance: 192.168.0.247:32432
          group: postgreSql
      - targets: ["192.168.0.216:9187"]
        labels:
          instance: hk-center.pg.rds.aliyuncs.com:5432
          group: postgreSql

8.4監控redis指標redis_exporter

第一步:下載安裝redis_exporter

//下載
wget https://github.com/oliver006/redis_exporter/releases/download/v1.37.0/redis_exporter-v1.37.0.linux-386.tar.gz
//解壓
tar -xf redis_exporter-v1.37.0.linux-386.tar.gz -C /usr/local/
//改名
mv redis_exporter-v1.37.0.linux-386 redis_exporter
//啟動
./redis_exporter -help //檢視引數
nohup ./redis_exporter -redis.addr 192.168.0.247:30279 & //無密碼
nohup ./redis_exporter -redis.addr 192.168.0.247:30279 -redis.password 123456 & //有密碼

//瀏覽器輸入:http://192.168.0.215:9121/metrics

第二步:配置到prometheus

- job_name: "redis"
    static_configs:
      - targets: ["192.168.0.215:9121"]
        labels:
          instance: 192.168.0.247:30279
          group: redis

9.監控站點

9.1blackbox_exporter應用場景

HTTP 測試: 定義 Request Header 資訊、判斷 Http status / Http Respones Header / Http Body 內容
TCP 測試:  業務元件埠狀態監聽、應用層協議定義與監聽
ICMP 測試: 主機探活機制
POST 測試: 介面聯通性
SSL證書過期時間

9.2下載安裝blackbox_exporter

https://prometheus.io/download/

//下載
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.20.0/blackbox_exporter-0.20.0.linux-amd64.tar.gz
//解壓
tar -xf blackbox_exporter-0.20.0.linux-amd64.tar.gz -C /usr/local/
//改名
mv blackbox_exporter-0.20.0.linux-amd64 blackbox_exporter
//啟動
nohup ./blackbox_exporter &
//瀏覽器輸入http://192.168.0.215:9115/

9.3網站監控-prometheus配置

vi /usr/local/prometheus/prometheus.yml

//重啟prometheus
lsof -i:9090
kill -9 xxxx
nohup ./prometheus &

網站監控:

  - job_name: 'http_status'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: ['https://admin.d.blueshark.com']
        labels:
          instance: admin.d.blueshark.com
          group: web
      - targets: ['https://admin.k.blueshark.com']
        labels:
          instance: admin.k.blueshark.com
          group: web
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 192.168.0.215:9115

9.4ping檢測-prometheus配置

ping檢測:

  - job_name: 'ping_status'
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets: ['192.168.0.249']
        labels:
          instance: 'ping_status'
          group: 'icmp'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 192.168.0.215:9115

9.5埠檢測--prometheus配置

埠檢測:

  - job_name: 'port_status'
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets: ['192.168.0.215:80', '192.168.0.216:80', '192.168.0.217:80']
        labels:
          instance: 'port_status'
          group: 'port'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 192.168.0.215:9115

相關文章