prometheus 監控測試服務器集群


公司有幾台測試服務器,由於公司運維也幫忙去做服務器報警,但是由於測試服務器本來性能和線上機器硬件就不一樣,所以讓運維老師去掉了測試服務器報警,我們自己使用prometheus監控幾台測試服務器,當出現故障的時候把報警數據發送到企業微信中.

prometheus介紹

Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社會也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。

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

Prometheus應該是為數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一。

輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus

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

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

prometheus架構概覽

 

image
 

 

它的服務過程是這樣的Prometheus daemon負責定時去目標上抓取metrics(指標) 數據,每個抓取目標需要暴露一個http服務的接口給它定時抓取。

Prometheus

支持通過配置文件、文本文件、zookeeper、Consul、DNS SRV lookup等方式指定抓取目標。支持很多方式的圖表可視化,例如十分精美的Grafana,自帶的Promdash,以及自身提供的模版引擎等等,還提供HTTP API的查詢方式,自定義所需要的輸出。

Alertmanager

是獨立於Prometheus的一個組件,可以支持Prometheus的查詢語句,提供十分靈活的報警方式。

PushGateway:這個組件是支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。

如果有使用過statsd的用戶,則會覺得這十分相似,只是statsd是直接發送給服務器端,而Prometheus主要還是靠進程主動去抓取。

prometheus的數據模型

Prometheus從根本上所有的存儲都是按時間序列去實現的,相同的metrics(指標名稱) 和label(一個或多個標簽) 組成一條時間序列,不同的label表示不同的時間序列。為了支持一些查詢,有時還會臨時產生一些時間序列存儲。

metrics name&label指標名稱和標簽

每條時間序列是由唯一的”指標名稱”和一組”標簽(key=value)”的形式組成。

指標名稱:一般是給監測對像起一名字,例如http_requests_total這樣,它有一些命名規則,可以包字母數字之類的的。通常是以應用名稱開頭監測對像數值類型單位這樣。例如:push_total、userlogin_mysql_duration_seconds、app_memory_usage_bytes。

標簽:就是對一條時間序列不同維度的識別了,例如一個http請求用的是POST還是GET,它的endpoint是什么,這時候就要用標簽去標記了。最終形成的標識便是這樣了:http_requests_total{method=”POST”,endpoint=”/api/tracks”}。

記住,針對http_requests_total這個metrics name無論是增加標簽還是刪除標簽都會形成一條新的時間序列。

查詢語句就可以跟據上面標簽的組合來查詢聚合結果了。

如果以傳統數據庫的理解來看這條語句,則可以考慮http_requests_total是表名,標簽是字段,而timestamp是主鍵,還有一個float64字段是值了。(Prometheus里面所有值都是按float64存儲)。

prometheus四種數據類型

Counter

Counter用於累計值,例如記錄請求次數、任務完成數、錯誤發生次數。一直增加,不會減少。重啟進程后,會被重置。

例如:http_response_total{method=”GET”,endpoint=”/api/tracks”} 100,10秒后抓取http_response_total{method=”GET”,endpoint=”/api/tracks”} 100。

Gauge

Gauge常規數值,例如 溫度變化、內存使用變化。可變大,可變小。重啟進程后,會被重置。

例如: memory_usage_bytes{host=”master-01″} 100 < 抓取值、memory_usage_bytes{host=”master-01″} 30、memory_usage_bytes{host=”master-01″} 50、memory_usage_bytes{host=”master-01″} 80 < 抓取值。

Histogram

Histogram(直方圖)可以理解為柱狀圖的意思,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。它特別之處是可以對記錄的內容進行分組,提供count和sum全部值的功能。

例如:{小於10=5次,小於20=1次,小於30=2次},count=7次,sum=7次的求和值。

Summary

Summary和Histogram十分相似,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。同樣提供 count 和 sum 全部值的功能。

例如:count=7次,sum=7次的值求值。

它提供一個quantiles的功能,可以按%比划分跟蹤的結果。例如:quantile取值0.95,表示取采樣值里面的95%數據。

依賴鏡像

docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana

部署prometheus

配置

mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml

yml內容

yml中配置了一個prometheus自己和一台linux監控

global:
scrape_interval: 60s
evaluation_interval: 60s

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

- job_name: linux
static_configs:
- targets: ['192.168.91.132:9100']
labels:
instance: localhost

啟動prometheus

啟動的時候掛載了prometheus.yml文件

docker run -d \
-p 9090:9090 \
-v /Users/qamac/Documents/script/docker_prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus

查看目標機器

http://192.168.143.242:9090/targets

如果出現status是down的情況說明沒有連接成功,需要檢查對應服務是否啟動成功及對應端口

 

image
 

 

出現下圖,說明配置成功

 

image
 

 

查看采集metrics

點擊下面這個接口,會跳轉到metrics頁面,通過輪訓的方式更新數據

http://192.168.143.242:9090/metrics

 

image
 

 

 

image
 

 

部署node-exporter

node-exporter啟動后會在服務器上啟動一個進程采集數據,prometheus會每隔幾秒通過接口獲取服務器的metrics數據.

注意本地mac啟動不能加--net="host"

docker run -d -p 9100:9100 \
-v "/proc:/host/proc:ro" \
-v "/sys:/host/sys:ro" \
-v "/:/rootfs:ro" \
--net="host" \
prom/node-exporter

部署grafana

啟動grafana

docker run -d -p 3000:3000 grafana

grafana地址

登錄賬號密碼:admin/admin

http://192.168.143.242:3000

grafana配置

prometheus配置

配置prometheus數據源

 

image
 

 

grafana模版

導入dashboards模版

https://grafana.com/grafana/dashboards/8919

 

image
 

 

展示

 

image
 

 

配置多個機器監控,需要在每一台機器部署node-exporter.

 

image
 

 

配置告警規則

報警規則配置

rules.yml中配置監控服務的內存、cpu、磁盤告警策略

Server: '{{$labels.instance}}'
summary: "{{$labels.instance}}: High Memory usage detected"
explain: "內存使用量超過90%,目前剩余量為:{{ $value }}M"
description: "{{$labels.instance}}: Memory usage is above 90% (current value is: {{ $value }})"

- alert: CPU報警
expr: (100 - (avg by (instance)(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)) > 90
for: 2m
labels:
team: node
annotations:
Alert_type: CPU報警
Server: '{{$labels.instance}}'
explain: "CPU使用量超過90%,目前剩余量為:{{ $value }}"
summary: "{{$labels.instance}}: High CPU usage detected"
description: "{{$labels.instance}}: CPU usage is above 90% (current value is: {{ $value }})"

- alert: 磁盤報警
expr: 100.0 - 100 * ((node_filesystem_avail_bytes{mountpoint=~"/", device!="rootfs"} / 1000 / 1000 ) / (node_filesystem_size_bytes{mountpoint=~"/", device!="rootfs"} / 1024 / 1024)) > 90
for: 2m
labels:
team: node
annotations:
Alert_type: 磁盤報警
Server: '{{$labels.instance}}'
explain: "磁盤使用量超過90%,目前剩余量為:{{ $value }}G"
summary: "{{$labels.instance}}: High Disk usage detected"
description: "{{$labels.instance}}: Disk usage is above 90% (current value is: {{ $value }})"

- alert: 服務器下線告警
expr: up == 0
for: 1m
labels:
user: admin
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

加載配置

prometheus.yml加載rule_files

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ["192.168.1.232:9093"]
# - alertmanager:9093


# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules.yml"

啟動prometheus

docker run -d -p 9090:9090 --name=prometheus1 \
-v /Users/qamac/Documents/script/docker_prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /Users/qamac/Documents/script/docker_prometheus/memory_over.yml:/etc/prometheus/rules.yml \
prom/prometheus

部署alertmanager

郵箱配置

可以通過郵件的形式發送告警郵件

global:
smtp_smarthost: 'smtp.126.com:25'  #163服務器
smtp_from: 'xxxxx@126.com'        #發郵件的郵箱
smtp_auth_username: 'xxxxx@126.com'  #發郵件的郵箱用戶名,也就是你的郵箱
smtp_auth_password: 'xxxxx'        #發郵件的郵箱密碼

route:
group_by: ['alertname']

repeat_interval: 1h

receiver: live-monitoring

receivers:
- name: 'live-monitoring'
email_configs:
- to: 'xxxxx@xxxxx.com'        #收郵件的郵箱

webhook配置

因為我司用企業微信比較多,再加上平時也不怎么看郵件.
所以想自定義一個webhook地址,把告警發到企業微信群中.

global:
resolve_timeout: 5m

route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://127.0.0.1:5000/send'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
~

啟動alertmanager

docker run -d -p 9093:9093 -v /data/docker_alertmanager/simple.yml/:/etc/alertmanager/config.yml --name alertmanager1 prom/alertmanager 

alertmanager的web頁面

http://192.168.1.232:9093/#/status

 

image
 

 

下圖是配置的告警方式

 

image
 

 

prometheus中報警模塊

http://192.168.143.242:9090/alerts

訪問上面的地址,可以看到已經加載了告警規則

 

image
 

 

報警的幾個狀態

  • Inactive: 既不是pending也不是firing的時候狀態變為inactive
  • Pending:警報被激活,但是低於配置的持續時間。這里的持續時間即rule里的FOR字段設置的時間.改狀態下不發送報警.
  • Firing: 警報已被激活,而且超出設置的持續時間。該狀態下發送報警.

如下圖的for字段是配置2分鍾循環,第一次觸發規則是Pending狀態,如果超過2分鍾就變成了Firing狀態,才發送告警

 

image
 

 

webhook服務

我們需要一個webhook服務接受報警的消息然后在發給企業微信群中.

這里我使用python flask框架開發web服務.

報警消息的格式

{
"status": "firing",
"labels": {
"instance": "localhost",
"job": "linux",
"user": "admin",
"alertname": "NodeMemoryUsage"
},
"endsAt": "2020-01-06T08:38:59.334190464Z",
"generatorURL": "http://13b226ded726:9090/graph?g0.expr=%28node_memory_MemTotal_bytes+-+%28node_memory_MemFree_bytes+%2B+node_memory_Buffers_bytes+%2B+node_memory_Cached_bytes%29%29+%2F+node_memory_MemTotal_bytes+%2A+100+%3E+5&g0.tab=1",
"startsAt ": "2020-01-05T15:33:59.334190464Z",
"annotations": {
"description": "localhost: Memory usage is above 80% (current value is:22.168394749407362)",
"summary": "localhost: High Memory usage detected"
}
}

定義send接口

 

image
 

 

解析響應數據

 

image
 

 

dockerfile

這里使用docker把服務打包成鏡像部署

FROM python3.7
RUN pip3 install requests && pip3 install flask && pip3 install logzero && pip3 install gunicorn && pip3 install flask_script
EXPOSE 5000

ENTRYPOINT ["/run.sh"]

企業微信報警

 

image
 

 

參考

基於docker 搭建Prometheus+Grafana

https://www.cnblogs.com/xiao987334176/p/9930517.html

【集群監控】Docker上部署Prometheus+Alertmanager+Grafana實現集群監控

https://www.cnblogs.com/caizhenghui/p/9184082.html

部署AlertManager

https://yunlzheng.gitbook.io/prometheus-book/parti-prometheus-ji-chu/alert/install-alert-manager

prometheus alertmanager webhook 配置教程

https://blog.csdn.net/shida_csdn/article/details/81980021

Alertmanager 部署配置

https://www.cnblogs.com/winstom/p/11940570.html#測試觸發告警

https://yunlzheng.gitbook.io/prometheus-book/parti-prometheus-ji-chu/quickstart/why-monitor

監控指標以及prometheus規則-不斷完善中

https://blog.51cto.com/1000682/2374417

BAT大廠都在用的Docker。學會這三招,面試、工作輕松hold住

https://mp.weixin.qq.com/s/DpC3mC8XqKgrneW1HwH4Bw


免責聲明!

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



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