环境准备:
主机名称 | IP | 安装组件 |
Prometheus | 192.168.1.1 | NodeEXporter、cAdvisor、Prometheus Server、Grafana |
node1 | 192.168.1.2 | NodeEXporter、cAdvisor |
node2 | 192.168.1.4 | NodeEXporter、cAdvisor |
注意事项:
全部关闭防火墙,禁用selinux。
需要部署的组件:
Prometheus Server: 普罗米修斯的主服务器。
NodeEXporter: 负责收集Host硬件信息和操作系统信息。
cAdvisor: 负责收集Host上运行的容器信息。
Grafana: 负责展示普罗米修斯监控界面。
1、3个节点,全部部署node-EXporter,和cAdvisor.
//部署node-EXporter,收集硬件和系统信息。
docker run -d -p 9100:9100 --name exporter -v /proc:/host/proc -v /sys:/host/sys -v /:/rootfs --net=host prom/node-exporter --path.procfs /host/proc --path.sysfs /host/sys --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"效果图:http://192.168.1.1:9100/ http://192.168.1.2:9100/ http://192.168.1.4:9100/

PS:注意,这里使用了--net=host不等于host网络,作用是Prometheus Server可以直接与Node-EXporter通信。
//部署安装cAdvisor,收集节点容器信息。
docker run -v /:/rootfs:ro -v /var/run:/var/run/:rw -v /sys:/sys:ro -v /var/lib/docker:/var/lib/docker:ro -p 8080:8080 --detach=true --name=cadvisor --net=host google/cadvisor访问测试:http://IP:8080/

2、在Prometheus上部署Prometheus Server服务。
在部署prometheus之前,我们需要对它的配置文件进行修改,所以我们先运行一个容器,先将其配置文件拷贝出来。
#运行普罗米修斯容器
[root@Prometheus ~]# docker run -d -p 9090:9090 --name prometheus --net=host prom/prometheus
WARNING: Published ports are discarded when using host network mode
0015d42a6f0348ff5ba6c0dd7e51cf9bac8656be346b6f433293754002012e8c
#copy配置文件到宿主机
[root@Prometheus ~]# docker cp prometheus:/etc/prometheus/prometheus.yml ./
#修改宿主机copy的配置文件
[root@Prometheus ~]# vim prometheus.yml
static_configs:
- targets: ['localhost:9090','localhost:8080','localhost:9100','192.168.1.5:8080','192.168.1.5:9100','192.168.1.6:8080','192.168.1.6:9100']PS: 这里指定了prometheus的监控项,包括它也会监控自己收集到的数据。
//重新运行prometheus容器,并且挂载修改的yml文件
[root@Prometheus ~]# docker rm -f prometheus
prometheus
[root@Prometheus ~]# docker run -d -p 9090:9090 --name prometheus --net=host -v /root/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
WARNING: Published ports are discarded when using host network mode
2857c1a73e934ad117f0bb6be59c3cec6efe701b776df59df8f54248bcd539d2
#测试访问:IP:9090三、在prometheus主机上,部署grafana服务,用来展示prometheus收集到的数据。
[root@Prometheus ~]# mkdir grafana-storage
[root@Prometheus ~]# chmod 777 -R grafana-storage/
[root@Prometheus ~]# docker run -d -p 3000:3000 --name grafana -v /root/grafana-storage:/var/lib/grafana -e "GF_SECURITY_ADMIN_PASSWORD=123.com" grafana/grafana
b95a4c30105ed9a0b8448da49efd4e11e4505f04e2f7a4b454f0e9910bbc4e48
#上述命令中的“-e”选项是为了设置默认的登录用户admin,密码为“123.com”。
#如果启动容器的过程中,提示iptables等相关的错误信息,
#则需要执行命令systemctl restart docker,重启docker服务,然后重新运行容器
#但是需要注意,若运行容器时没有增加“--restart=always”选项的话,
#那么在重启docker服务后,还需将所有容器手动重启。
#重启所有容器命令“docker ps -a -q | xargs docker start”
#访问测试:IP:3000
//设置Grafana,添加数据来源(prometheus)



PS:看到这个提示,说明prometheus和grafana服务的是正常连接的。此时,虽然grafana收集到了数据,但怎么显示它,仍然是个问题,grafana支持自定义显示信息,不过要自定义起来非常麻烦,不过好在,grafana官方为我们提供了一些模板,来供我们使用。
https://grafana.com/grafana/dashboards

导入模板用两种方法:JSON、id!
JSON如下:




效果图:

四、配置AlertManager
接下来,我们需要启动 AlertManager 来接受 Prometheus 发送过来的报警信息,并执行各种方式的告警。同样以 Docker 方式启动AlertManager,最简单的启动命令如下
#运行报警邮件管理容器
[root@Prometheus ~]# docker run --name alertmanager -d -p9093:9093 prom/alertmanager:latest
#copy配置文件
[root@Prometheus ~]# docker cp alertmanager:/etc/alertmanager/alertmanager.yml .
#讲原来的yml改名称,修改刚copy的配置文件
[root@Prometheus ~]# cp alertmanager.yml alertmanager.ymlbak这里 AlertManager 默认启动的端口为 9093,启动完成后,浏览器访问http://:9093 可以看到默认提供的 UI 页面,不过现在是没有任何告警信息的,因为我们还没有配置报警规则来触发报警。
AlertManager 配置邮件告警
AlertManager 默认配置文件为 alertmanager.yml,在容器内路径为/etc/alertmanager/alertmanager.yml
docker cp alertmanager:/etc/alertmanager/alertmanager.yml /root/简单介绍一下主要配置的作用:
- global: 全局配置,包括报警解决后的超时时间、SMTP 相关配置、各种渠道通知的 API 地址等等
- route: 用来设置报警的分发策略,它是一个树状结构,按照深度优先从左向右的顺序进行匹配。
- receivers: 配置告警消息接受者信息,例如常用的 email、wechat、slack、webhook 等消息通知方式。
- inhibit_rules: 抑制规则配置,当存在与另一组匹配的警报(源)时,抑制规则将禁用与一组匹配的警报(目标)。
那么,我们就来配置一下使用 Email 方式通知报警信息,这里以 QQ 邮箱为例,当然在配置QQ邮箱之前,需要我们登录QQ邮箱,打开SMTP服务,并获取授权码。
#写入自己的QQ邮箱与效验码
[root@Prometheus ~]# vim /root/alertmanager.yml
global:
resolve_timeout: 5m
smtp_from: '2050753687@qq.com'
smtp_smarthost: 'smtp.qq.com:465'
smtp_auth_username: '2050753687@qq.com'
smtp_auth_password: 'rxtfqkwfafoidcea'
smtp_require_tls: false
smtp_hello: 'qq.com'
route:
group_by: ['alertname']
group_wait: 5s
group_interval: 5s
repeat_interval: 5m
receiver: 'email'
receivers:
- name: 'email'
email_configs:
- to: '2050753687@qq.com'
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']#重新运行
[root@Prometheus ~]# docker rm -f alertmanager
alertmanager
[root@Prometheus ~]# docker run -d --name alertmanager -p 9093:9093 -v /root/alertmanager.yml:/etc/alertmanager/alertmanager.yml prom/alertmanager
8c56092d9ef2946f88c10e2e648e62df33bbbc2ac8d9cd503c8700e4445686a0//访问查看

五、Prometheus 配置 AlertManager 告警规则
接下来,我们需要在 Prometheus 配置 AlertManager 服务地址以及告警规则,新建报警规则文件 node-up.rules 如下
[root@Prometheus ~]# mkdir -p prometheus/rules
[root@Prometheus ~]# cd prometheus/rules/
[root@Prometheus rules]# vim node-up.rules
[root@Prometheus rules]# cat node-up.rules
groups:
- name: node-up
rules:
- alert: node-up
expr: up{job="prometheus"} == 0
for: 15s
labels:
severity: 1
team: node
annotations:
summary: "{{ $labels.instance }} 已停止运行超过 15s!"
然后,修改 prometheus.yml 配置文件,添加 rules 规则文件
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.1.1:9093
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "/usr/local/prometheus/rules/*.rules"
# - "first_rules.yml"
# - "second_rules.yml"

[root@Prometheus rules]# docker rm -f prometheus
[root@Prometheus rules]# docker run -d -p 9090:9090 -v/root/prometheus.yml:/etc/prometheus/prometheus.yml -v/root/prometheus/rules:/usr/local/prometheus/rules --name prometheus --net=host prom/prometheus此时在prometheus主页上可以看到相应规则

触发报警发送 Email
然后开始模拟node1节点 exporter组件失败
[root@node1 ~]# docker stop exporter
exporter
切换到QQ邮箱

AlertManager 配置自定义邮件模板
看到上边默认发送的邮件模板,虽然所有核心的信息已经包含了,但是邮件格式内容可以更优雅直观一些,那么,AlertManager 也是支持自定义邮件模板配置的,首先新建一个模板文件 email.tmpl。
[root@master rules]# cd /root/prometheus/
[root@master prometheus]# ls
rules
[root@master prometheus]# mkdir alertmanager-tmpl
[root@master prometheus]# vim /root/prometheus/alertmanager-tmpl/email.tmpl
#编辑模板,注意QQ写自己登录查看的!
[root@master prometheus]# cat /root/prometheus/alertmanager-tmpl/email.tmpl
{{ define "email.from" }}2050753687@qq.com{{ end }}
{{ define "email.to" }}2050753687@qq.com{{ end }}
{{ define "email.to.html" }}
{{ range .Alerts }}
=========start==========<br>
告警程序: prometheus_alert<br>
告警级别: {{ .Labels.severity }} 级<br>
告警类型: {{ .Labels.alertname }}<br>
故障主机: {{ .Labels.instance }}<br>
告警主题: {{ .Annotations.summary }}<br>
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} <br>
=========end==========<br>
{{ end }}
{{ end }}上边模板文件配置了 email.from、email.to、email.to.html 三种模板变量,可以在 alertmanager.yml 文件中直接配置引用。这里email.to.html 就是要发送的邮件内容,支持 Html 和 Text 格式,这里为了显示好看,采用 Html 格式简单显示信息。下边 {{ range .Alerts }}是个循环语法,用于循环获取匹配的 Alerts 的信息,下边的告警信息跟上边默认邮件显示信息一样,只是提取了部分核心值来展示。然后,需要增加 alertmanager.yml 文件 templates 配置如下:


global:
resolve_timeout: 5m
smtp_from: '2050753687@qq.com'
smtp_smarthost: 'smtp.qq.com:465'
smtp_auth_username: '2050753687@qq.com'
smtp_auth_password: 'rxtfqkwfafoidcea'
smtp_require_tls: false
smtp_hello: 'qq.com'
templates:
- '/etc/alertmanager-tmpl/*.tmpl'
route:
group_by: ['alertname']
group_wait: 5s
group_interval: 5s
repeat_interval: 5m
receiver: 'email'
receivers:
- name: 'email'
email_configs:
- to: '{{ template "email.to" }}'
html: '{{ template "email.to.html" . }}'
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance'][root@master /]# vim /root/alertmanager.yml
然后,修改 AlertManager 启动命令,将本地 email.tmpl 文件挂载到容器内指定位置并重启。
[root@master /]# docker rm -f alertmanager
[root@master /]# docker run -d --name alertmanager -p9093:9093 -v /root/alertmanager.yml:/etc/alertmanager/alertmanager.yml -v /root/prometheus/alertmanager-tmpl/email.tmpl:/etc/alertmanager-tmpl prom/alertmanager
#注意挂载的路径是文件夹不是文件!!!重启完毕后,同样模拟触发报警条件(停止 node-exporter 服务),也是可以正常发送模板邮件出来的,这次就是我们想要的风格啦!

















