Prometheus 邁出DevOps第一步

Prometheus 邁出DevOps第一步

前言

平臺服務資源監控,是邁出DevOps的第一步,服務資源的穩定是基礎,監控資源是核心,保障基礎設施的穩定,是其他各種服務的前提。
現在市面上監控系統很多:Zabbix、Open-Falcon、Prometheus。最終選擇的是 Prometheus,github
Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受採用Prometheus,社會也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。

Prometheus基本原理是通過HTTP協議週期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供HTTP接口就可以接入監控系統,不需要任何SDK或者其他的集成過程。這樣做非常適合虛擬化環境比如VM或者Docker 。

Prometheus應該是爲數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一,也是我安裝的主要原因。
在這裏插入圖片描述
下面使用Prometheus進行系統監控基礎實戰,包含兩個系統:Windows10以及Centos7

安裝Prometheus

下載解壓

wget https://github.com/prometheus/prometheus/releases/download/v2.13.1/prometheus-2.13.1.linux-amd64.tar.gz

配置Prometheus

Prometheus安裝目錄打開配置文件prometheus.yml,註冊探針exporter

# 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: 'wmi_exporter'
    static_configs:
    - targets: ['localhost:9182']
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['10.231.xxx.xxx:9183']

啓動Prometheus

$ ./prometheus.exe --config.file=prometheus.yml --web.listen-address=:9090

瀏覽器鍵入:127.0.0.1:9090,點擊Status欄目的Targets選項,如圖:
在這裏插入圖片描述
頁面State字段顯示Up,說明prometheus成功的pull數據;顯示Down,說明沒有pull到數據,需要安裝探針。

安裝探針

官方提供了一些探針exporter,https://prometheus.io/download/,還有很多探針不是官網提供的,但是都是實現了Prometheus的規範,瞭解了它的規範,自己也可以定製化探針。

Windows wmi_exporter

  1. 下載
    地址:https://github.com/martinlindhe/wmi_exporter/releases/download/v0.9.0/wmi_exporter-0.9.0-amd64.msi
    下載後雙擊安裝即可,默認端口:9182
  2. 測試
    瀏覽器鍵入:http://:9182/metrics
    在這裏插入圖片描述

Centos node_exporter

github地址:https://github.com/prometheus/node_exporter

$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
$ tar -xvf node_exporter-0.18.1.linux-amd64.tar.gz
$ cd node_exporter-0.18.1.linux-amd64
$ ./node_exporter --web.listen-address=":9100"

默認端口:9100,測試同上.

測試

再次查看Prometheus控制檯,如下:
在這裏插入圖片描述
可以看到,狀態已經變成Up, OK,切換到Graph,查看內存
在這裏插入圖片描述
到這裏理論上已經結束,就是可視化有點low,還是推薦使用種類多的圖形庫來監控可視化,比如:Grafana,美觀、強大的可視化監控指標展示工具,也是真Go哈哈!

監控可視化

grafana 是一款採用 go 語言編寫的開源應用,主要用於大規模指標數據的可視化展現,是網絡架構和應用分析中最流行的時序數據展示工具,目前已經支持絕大部分常用的時序數據庫。
https://grafana.com/
prometheus提供了一些簡單的監控圖標,實際中我們一般用Grafana進行展現
https://grafana.com/grafana/download?platform=
在這裏插入圖片描述
如何在yml語法配置多個類型

後記

探針就是基於pa的提供的HTTP協議以及數據規範,供給它調用。其實可以理解爲採集器,定時主動去採集,就像分佈式日誌採集系統ELK,不同的是,這裏採集的數據不會主動推送到pa,而是pa根據需要的時候,去pull下來。
輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus。

與其他監控系統相比,Prometheus的主要特點是:

  • 一個多維數據模型(時間序列由指標名稱定義和設置鍵/值尺寸)。
  • 非常高效的存儲,平均一個採樣數據佔~3.5bytes左右,320萬的時間序列,每30秒採樣,保持60天,消耗磁盤大概228G。
  • 一種靈活的查詢語言。
  • 不依賴分佈式存儲,單個服務器節點。
  • 時間集合通過HTTP上的PULL模型進行。
  • 通過中間網關支持推送時間。
  • 通過服務發現或靜態配置發現目標。
  • 多種模式的圖形和儀表板支持。

參考文章

prometheus github
prometheus下載地址
prometheus + grafana 實時監控 Windows性能
grafana官網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章