Prometheus 組成及架構(gòu)
Prometheus 生態(tài)圈中包含了多個(gè)組件,其中許多組件是可選的:
- Prometheus Server: 用于收集和存儲時(shí)間序列數(shù)據(jù)。
- Client Library: 客戶端庫,為需要監(jiān)控的服務(wù)生成相應(yīng)的 metrics 并暴露給 Prometheus server。當(dāng) Prometheus server 來 pull 時(shí),直接返回實(shí)時(shí)狀態(tài)的 metrics。
- Push Gateway: 主要用于短期的 jobs。由于這類 jobs 存在時(shí)間較短,可能在 Prometheus 來 pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端推送它們的 metrics。這種方式主要用于服務(wù)層面的 metrics,對于機(jī)器層面的 metrices,需要使用 node exporter。
- Exporters: 用于暴露已有的第三方服務(wù)的 metrics 給 Prometheus。
- Alertmanager: 從 Prometheus server 端接收到 alerts 后,會進(jìn)行去除重復(fù)數(shù)據(jù),分組,并路由到對應(yīng)的接收方式,發(fā)出報(bào)警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
Prometheus 官方文檔中的架構(gòu)圖:
從上圖可以看出,Prometheus 的主要模塊包括:Prometheus server, exporters, Pushgateway, PromQL, Alertmanager 以及圖形界面。
其大概的工作流程是:
- Prometheus server 定期從配置好的 jobs 或者 exporters 中拉 metrics,或者接收來自 Pushgateway 發(fā)過來的 metrics,或者從其他的 Prometheus server 中拉 metrics。
- Prometheus server 在本地存儲收集到的 metrics,并運(yùn)行已定義好的 alert.rules,記錄新的時(shí)間序列或者向 Alertmanager 推送警報(bào)。
- Alertmanager 根據(jù)配置文件,對接收到的警報(bào)進(jìn)行處理,發(fā)出告警。
- 在圖形界面中,可視化采集數(shù)據(jù)。
Prometheus官網(wǎng):https://prometheus.io/
1. Prometheus安裝及配置
192.168.16.251 Prometheus,grafana,alertmanager,Node-exporter
192.168.16.252 Node-exporter,Jmx-exporter,Cadvisor
創(chuàng)建Prometheus配置文件prometheus.yml
本地宿主機(jī)/root/prometheus/conf/下創(chuàng)建
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.
alerting: #指定alertmanager報(bào)警組件地址
alertmanagers:
- static_configs:
- targets: [ '192.168.16.251:9093']
rule_files: #指定報(bào)警規(guī)則文件
- "rules.yml"
scrape_configs:
- job_name: 'nodehost'
static_configs:
- targets: ['192.168.16.251:9100']
labels:
appname: 'Node1'
static_configs:
- targets: ['192.168.16.252:9100']
labels:
appname: 'Node2'
- job_name: 'tomcat'
static_configs:
- targets: ['192.168.16.173:12345']
labels:
appname: 'mytest'
- job_name: 'cadvisor'
static_configs:
- targets: [ '192.168.16.251:8080','192.168.16.252:8080','192.168.16.173:8080']
labels:
appname: 'cadvisor'
- job_name: 'prometheus'
static_configs:
- targets: [ '192.168.16.251:9090']
labels:
appname: 'prometheus'
上面我們使用靜態(tài)的方式指定了各Metris的地址,但后面應(yīng)用數(shù)量越來越多,手動的添加就不太現(xiàn)實(shí)了,Prometheus支持服務(wù)發(fā)現(xiàn)等多種方式
Consul服務(wù)發(fā)現(xiàn)配置下篇:http://www.lxweimin.com/p/085edb535070
具體信息移步官網(wǎng) https://prometheus.io/docs/prometheus/latest/configuration/configuration/
創(chuàng)建Prometheus規(guī)則文件rules.yml
本地宿主機(jī)/root/prometheus/conf/下創(chuàng)建
下面監(jiān)控宿主機(jī)和容器的內(nèi)存,CPU,磁盤等狀態(tài)
groups:
- name: example #定義規(guī)則組
rules:
- alert: InstanceDown #定義報(bào)警名稱
expr: up == 0 #Promql語句,觸發(fā)規(guī)則
for: 1m # 一分鐘
labels: #標(biāo)簽定義報(bào)警的級別和主機(jī)
name: instance
severity: Critical
annotations: #注解
summary: " {{ $labels.appname }}" #報(bào)警摘要,取報(bào)警信息的appname名稱
description: " 服務(wù)停止運(yùn)行 " #報(bào)警信息
value: "{{ $value }}%" # 當(dāng)前報(bào)警狀態(tài)值
- name: Host
rules:
- alert: HostMemory Usage
expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes * 100 > 80
for: 1m
labels:
name: Memory
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: "宿主機(jī)內(nèi)存使用率超過80%."
value: "{{ $value }}"
- alert: HostCPU Usage
expr: sum(avg without (cpu)(irate(node_cpu_seconds_total{mode!='idle'}[5m]))) by (instance,appname) > 0.65
for: 1m
labels:
name: CPU
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: "宿主機(jī)CPU使用率超過65%."
value: "{{ $value }}"
- alert: HostLoad
expr: node_load5 > 4
for: 1m
labels:
name: Load
severity: Warning
annotations:
summary: "{{ $labels.appname }} "
description: " 主機(jī)負(fù)載5分鐘超過4."
value: "{{ $value }}"
- alert: HostFilesystem Usage
expr: 1-(node_filesystem_free_bytes / node_filesystem_size_bytes) > 0.8
for: 1m
labels:
name: Disk
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: " 宿主機(jī) [ {{ $labels.mountpoint }} ]分區(qū)使用超過80%."
value: "{{ $value }}%"
- alert: HostDiskio
expr: irate(node_disk_writes_completed_total{job=~"Host"}[1m]) > 10
for: 1m
labels:
name: Diskio
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: " 宿主機(jī) [{{ $labels.device }}]磁盤1分鐘平均寫入IO負(fù)載較高."
value: "{{ $value }}iops"
- alert: Network_receive
expr: irate(node_network_receive_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*|virbr.*|ovs-system"}[5m]) / 1048576 > 3
for: 1m
labels:
name: Network_receive
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: " 宿主機(jī) [{{ $labels.device }}] 網(wǎng)卡5分鐘平均接收流量超過3Mbps."
value: "{{ $value }}3Mbps"
- alert: Network_transmit
expr: irate(node_network_transmit_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*|virbr.*|ovs-system"}[5m]) / 1048576 > 3
for: 1m
labels:
name: Network_transmit
severity: Warning
annotations:
summary: " {{ $labels.appname }} "
description: " 宿主機(jī) [{{ $labels.device }}] 網(wǎng)卡5分鐘內(nèi)平均發(fā)送流量超過3Mbps."
value: "{{ $value }}3Mbps"
- name: Container
rules:
- alert: ContainerCPU Usage
expr: (sum by(name,instance) (rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 60
for: 1m
labels:
name: CPU
severity: Warning
annotations:
summary: "{{ $labels.name }} "
description: " 容器CPU使用超過60%."
value: "{{ $value }}%"
- alert: ContainerMem Usage
# expr: (container_memory_usage_bytes - container_memory_cache) / container_spec_memory_limit_bytes * 100 > 10
expr: container_memory_usage_bytes{name=~".+"} / 1048576 > 1024
for: 1m
labels:
name: Memory
severity: Warning
annotations:
summary: "{{ $labels.name }} "
description: " 容器內(nèi)存使用超過1GB."
value: "{{ $value }}G"
部署Prometheus
docker run -d -p 9090:9090 --name=prometheus \
-v /root/prometheus/conf/:/etc/prometheus/ \
prom/prometheus
上面采用的官方鏡像,因?yàn)閱訁?shù)沒有指定--web.enable-lifecycle,所以無法使用熱加載,時(shí)區(qū)也是相差八個(gè)小時(shí),我們可以通過官方提供的Dockerfile進(jìn)行修改
下載源碼包,制作Prometheus鏡像
https://github.com/prometheus/prometheus
FROM centos:7
LABEL maintainer "The Prometheus Authors <prometheus-developers@googlegroups.com>, Custom by <leichen.china@gmail.com>"
COPY prometheus /bin/prometheus
COPY promtool /bin/promtool
COPY console_libraries/ /usr/share/prometheus/console_libraries/
COPY consoles/ /usr/share/prometheus/consoles/
WORKDIR /prometheus
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENTRYPOINT [ "/bin/prometheus" ]
CMD [ "--config.file=/etc/prometheus/prometheus.yml", \
"--storage.tsdb.path=/prometheus", \
"--web.console.libraries=/usr/share/prometheus/console_libraries", \
"--web.enable-lifecycle", \
"--web.console.templates=/usr/share/prometheus/consoles" ]
創(chuàng)建容器并運(yùn)行
docker build -t prometheus:latest .
docker run -d -p 9090:9090 --name prometheus -v /root/prometheus/conf/:/etc/prometheus/ prometheus:latest
訪問prometheus的9090端口,可以查看監(jiān)控?cái)?shù)據(jù)
2. 部署Node-exporter
docker run -d -p 9100:9100 -v "/:/host:ro,rslave" quay.io/prometheus/node-exporter --path.rootfs /host
3. 部署Cadvisor-exporter
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 --detach=true --name=cadvisor --net=host google/cadvisor:latest
訪問cadvisor的8080端口,可以看到容器的監(jiān)控指標(biāo)
4. 部署Jmx-exporter
下載jar :https://github.com/prometheus/jmx_exporter (jmx_prometheus_javaagent-0.11.0.jar )
配置文件: https://github.com/prometheus/jmx_exporter/tree/master/example_configs
中間件啟動參數(shù)添加:
CATALINA_OPTS="-javaagent:/app/tomcat-8.5.23/lib/jmx_prometheus_javaagent-0.11.0.jar=1234:/app/tomcat-8.5.23/conf/config.yaml"
5. Grafana安裝及配置
docker run -d -i -p 3000:3000 -e "GF_SERVER_ROOT_URL=http://grafana.server.name" -e "GF_SECURITY_ADMIN_PASSWORD=secret" --net=host grafana/grafana
web訪問 192.168.16.251:3000
user:admin,passwd:secret
首先我們添加數(shù)據(jù)源
import導(dǎo)入8919
Node-exporter展示模板
針對容器和JMX的監(jiān)控模板,我們可以去https://grafana.com/dashboards自行查找。
6. 配置報(bào)警alertmanager
創(chuàng)建alertmanager.yml報(bào)警通知文件
global:
resolve_timeout: 2m
smtp_smarthost: smtp.163.com:25
smtp_from: 12345678@163.com
smtp_auth_username: 12345678@163.com
smtp_auth_password: 123456 (授權(quán)碼)
templates: ##消息模板
- '/etc/alertmanager/template/wechat.tmpl'
route:
group_by: ['alertname_wechat']
group_wait: 30s
group_interval: 60s
receiver: 'wechat' # 優(yōu)先使用wechat發(fā)送
repeat_interval: 1h
routes: #子路由,使用email發(fā)送
- receiver: email
match_re:
serverity: email
receivers:
- name: 'email'
email_configs:
- to: '11111122@qq.com'
send_resolved: true # 發(fā)送已解決通知
- name: 'wechat'
wechat_configs:
- corp_id: 'wwd402ce40b1120f24' #企業(yè)ID
to_party: '2' # 通知組ID
agent_id: '1000002'
api_secret: '9nmYa4pWq63sQ123kToCbh_oNc' # 生成的secret
send_resolved: true
編寫微信通知模板
{{ define "wechat.default.message" }}
{{ range $i, $alert :=.Alerts }}
========監(jiān)控報(bào)警==========
告警狀態(tài):{{ .Status }}
告警級別:{{ $alert.Labels.severity }}
告警類型:{{ $alert.Labels.alertname }}
告警應(yīng)用:{{ $alert.Annotations.summary }}
告警主機(jī):{{ $alert.Labels.instance }}
告警詳情:{{ $alert.Annotations.description }}
觸發(fā)閥值:{{ $alert.Annotations.value }}
告警時(shí)間:{{ $alert.StartsAt.Format "2006-01-02 15:04:05" }}
========end=============
{{ end }}
{{ end }}
部署alertmanager
docker run -d -p 9093:9093 --name alertmanager -v /root/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml -v /root/alertmanager/template:/etc/alertmanager/template docker.io/prom/alertmanager:latest
訪問alertmanager的9093端口,可以看到當(dāng)前報(bào)警狀態(tài)