利用 Prometheus 監控測試服務器集群實踐詳解


乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

公司有幾臺測試服務器(由於測試服務器本來性能和線上機器硬件就不一樣,所以讓運維老師去掉了測試服務器報警),測試團隊自己使用 Prometheus 監控幾臺測試服務器,當出現故障的時候,把報警數據直接發送到企業微信中。

Prometheus 特點介紹

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

Prometheus 基本原理是通過 HTTP 協議週期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供 HTTP 接口就可以接入監控系統,不需要任何斯達克學院測試或者其他的集成過程。這樣做非常適合虛擬化環境比如 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 架構概覽


乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


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

Prometheus

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

Alertmanager

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

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

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


Prometheus 的數據模型

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

metrics name&label 指標名稱和標籤。

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

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

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

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

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

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

prometheus 四種數據類型

Gauge

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

memoryusagebytes{host=”master-01″} 100 < 抓取值、memoryusagebytes{host=”master-01″} 30、memoryusagebytes{host=”master-01″} 50、memoryusagebytes{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% 數據。

依賴鏡像

<code>docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana/<code>


部署 prometheus

配置

<code>mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml/<code>


yml 內容

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

<code>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/<code>


啟動 prometheus

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

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


查看目標機器

<code>http://192.168.143.242:9090/targets/<code>


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

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

出現下圖 , 說明配置成功。

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


查看採集 metrics

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

<code>http://192.168.143.242:9090/metrics/<code>
乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


部署 node-exporter

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

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

<code>docker run -d -p 9100:9100 \\
-v "/proc:/host/proc:ro" \\

-v "/sys:/host/sys:ro" \\
-v "/:/rootfs:ro" \\
--net="host" \\
prom/node-exporter/<code>


部署 Grafana

啟動 grafana

<code>docker run -d -p 3000:3000 grafana/<code>


grafana 地址

登錄賬號密碼:admin/admin

<code>http://192.168.143.242:3000/<code>


grafana 配置

prometheus 配置

配置 prometheus 數據源

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


grafana 模版

導入 dashboards 模版

<code>https://grafana.com/grafana/dashboards/8919/<code>
乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


展示


乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


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


乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


配置告警規則

報警規則配置

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

<code>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."/<code>


加載配置

prometheus.yml 加載 rule_files

<code># 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"/<code>


啟動 prometheus

<code>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/<code>


部署 alertmanager

郵箱配置

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

<code>global:
smtp_smarthost: 'smtp.126.com:25'  #163 服務器

smtp_from: '[email protected]'        #發郵件的郵箱
smtp_auth_username: '[email protected]'  #發郵件的郵箱用戶名,也就是你的郵箱
smtp_auth_password: 'xxxxx'        #發郵件的郵箱密碼

route:
group_by: ['alertname']

repeat_interval: 1h

receiver: live-monitoring

receivers:
- name: 'live-monitoring'
email_configs:
- to: '[email protected]'        #收郵件的郵箱/<code>


webhook 配置

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

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

~/<code>


啟動 alertmanager

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


alertmanager 的 web 頁面

<code>http://192.168.1.232:9093/#/status/<code>
乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

下圖是配置的告警方式

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


prometheus 中報警模塊

<code>http://192.168.143.242:9090/alerts/<code>

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

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


報警的幾個狀態

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

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

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解


Webhook 服務

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

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

報警消息的格式

<code>{
"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"
}
}/<code>


定義 send 接口


乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

解析響應數據

乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

Dockerfile

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

<code>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"]/<code>
乾貨|利用 Prometheus 監控測試服務器集群實踐詳解

以上,期待與各位同學多交流探討。


分享到:


相關文章: