Prometheus+Pushgateway+Grafana可視化監控全流程,並實現自定義監控

一、Prometheus 簡介

Prometheus 是一套開源的系統監控報警框架。它啓發於 Google 的 borgmon 監控系統,由工作在 SoundCloud 的 google 前員工在 2012 年創建,作爲社區開源項目進行開發,並於 2015 年正式發佈。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,成爲受歡迎度僅次於 Kubernetes 的項目。
廢話不多說,直接開幹

二、Prometheus安裝及配置

下載安裝Prometheus(https://prometheus.io/download/)

$ wget https://github.com/prometheus/prometheus/releases/download/v2.3.0/prometheus-2.3.0.linux-amd64.tar.gz
$ tar zxvf prometheus-2.3.0.linux-amd64.tar.gz -C /usr/local/
$ ln -sv /usr/local/prometheus-2.3.0.linux-amd64/ /usr/local/prometheus
$ cd /usr/local/prometheus

 

改Prometheus配置文件prometheus.yml (替換你要監控的IP地址):

- job_name: 'prometheus'

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

    static_configs:
    - targets: ['localhost:9090']
      labels:
          instance: prometheus

 

啓動Prometheus

nohup ./prometheus --config.file=prometheus.yml &

Prometheus內置了一個web界面,我們可通過http://prometheusip:9090進行訪問:

三、安裝pushgateway

Pushgateway 是 Prometheus 生態中一個重要工具,使用它的原因主要是:

  • Prometheus 採用 pull 模式,可能由於不在一個子網或者防火牆原因,導致 Prometheus 無法直接拉取各個 target 數據。
  • 在監控業務數據的時候,需要將不同數據彙總, 由 Prometheus 統一收集。

由於以上原因,不得不使用 pushgateway,但在使用之前,有必要了解一下它的一些弊端:

  • 將多個節點數據彙總到 pushgateway, 如果 pushgateway 掛了,受影響比多個 target 大。
  • Prometheus 拉取狀態 up 只針對 pushgateway, 無法做到對每個節點有效。
  • Pushgateway 可以持久化推送給它的所有監控數據。

因此,即使你的監控已經下線,prometheus 還會拉取到舊的監控數據,需要手動清理 pushgateway 不要的數據。

                                      

使用 prom/pushgateway 的 Docker 鏡像

docker pull prom/pushgateway

接下來啓動Push Gateway:

docker run -d \
  --name=pg \
  -p 9091:9091 \
  prom/pushgateway

訪問url:

http://pushgatewayIP:9091/

要使Push Gateway正常工作,必須要在prometheus中配置對應的job才行

修改配置文件 

vim /opt/prometheus/prometheus.yml

然後重啓Prometheus使配置生效,訪問targets,等待1分鐘,等待pushgateway狀態爲UP

我們開始嘗試向pushgateway發送http請求,linux中比較簡單:

向 {job="some_job"} 添加單條數據:

echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job

使用自己搭建的http通訊庫或者postman時注意text內容最後需要換行(\n)(這裏卡了我好久)

然後我們查看pushgateway後臺,如下圖則http請求接收成功

 

四、安裝運行Grafana
Grafana安裝配置介紹

$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.1-1.x86_64.rpm 
$ sudo yum localinstall grafana-5.0.1-1.x86_64.rpm


編輯配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下兩個參數的值:

[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards


最後我們運行Grafana服務

$ systemctl daemon-reload
$ systemctl start grafana-server
$ systemctl status grafana-server


我們可通過http://Grafanaip:3000訪問Grafana網頁界面(默認登陸帳號/密碼爲admin/admin):

然後我們到Data Sources頁面添加數據源,選擇我們的Prometheus:

最後創建儀表盤

metrics中可填寫自定義數據獲取來源,alert可設置郵件報警等

五、設置grafana報警
首先配置郵件服務

yum install -y sendmail
vi /etc/grafana/grafana.ini 

(配置文件添加如下,初步還沒有自己搭建郵件服務器,我用的QQ郵箱服務器)


重啓grafana

systemctl restart grafana-server


在grafana的web界面添加接收告警的郵箱地址,如果發送成功,右上角會有提示

稍後你會收到一封郵件,如下

 

最後整體效果如下:

 

 

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