prometheus 结合cAdvisor、AlertManager、node-exporter、 监控容器并实现邮箱告警
prometheus 监控容器
rometheus是一款面向云原生应用程序的开源监控工具,作为第一个从CNCF毕业的监控工具而言,开发者对于Prometheus寄予了巨大的希望。 在Kubernetes社区中,很多人认为Prometheus是容器场景中监控的第一方案,成为容器监控标准的制定者。
什么是 cAdvisor
cAdvisor (Container Advisor) 是 Google 开源的一个容器监控工具,可用于对容器资源的使用情况和性能进行监控。它以守护进程方式运行,用于收集、聚合、处理和导出正在运行容器的有关信息。具体来说,该组件对每个容器都会记录其资源隔离参数、历史资源使用情况、完整历史资源使用情况的直方图和网络统计信息。
cAdvisor 本身就对 Docker 容器支持,并且还对其它类型的容器尽可能的提供支持,力求兼容与适配所有类型的容器。
由以上介绍我们可以知道,cAdvisor 是用于监控容器引擎的。由于其监控的实用性,Kubernetes 已经默认将其与 Kubelet 融合,所以我们无需再单独部署 cAdvisor 组件来暴露节点中容器运行的信息,直接使用 Kubelet 组件提供的指标采集地址即可。
Cadvisor 进行收集,通过 Prometheus 作为数据源,利用 Grafana 进行展示。
环境说明
主机 | ip | 安装软件 |
---|---|---|
master | 192.168.200.144 | docker-ce |
client | 192.168.200.145 | docker-ce |
master、client主机安装docker并配置镜像加速
docker安装
配置docker-ce 源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# curl -o docker-ce.repo https:
//mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/do
cker-ce.repo
安装 docker-ce 以及依赖包和工具
[root@master ~]# dnf -y install yum-utils device-mapper-persistent-data lvm2
[root@master ~]# yum -y install docker-ce --allowerasing
安装完成后,使用 docker version 命令查看docker的版本信息
[root@master ~]# docker version
Client: Docker Engine - CommunityVersion: 20.10.12API version: 1.41Go version: go1.16.12Git commit: e91ed57Built: Mon Dec 13 11:45:22 2021OS/Arch: linux/amd64Context: defaultExperimental: true
配置docker镜像 加速
[root@master ~]# mkdir -p /etc/docker
[root@master ~]# vi /etc/docker/daemon.json
{"registry-mirrors": ["https://7z2g0ixw.mirror.aliyuncs.com"]
}[root@master ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service
等待docker在两台主机部署完成后,在 master 主机上拉取prom/Prometheus官方镜像
[root@master ~]# docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
3cb635b06aa2: Pull complete
34f699df6fe0: Pull complete
33d6c9635e0f: Pull complete
f2af7323bed8: Pull complete
c16675a6a294: Pull complete
827843f6afe6: Pull complete
3d272942eeaf: Pull complete
7e785cfa34da: Pull complete
05e324559e3b: Pull complete
170620261a59: Pull complete
ec35f5996032: Pull complete
5509173eb708: Pull complete
Digest: sha256:cb9817249c346d6cfadebe383ed3b3cd4c540f623db40c4ca00da2ada45259bb
Status: Downloaded newer image for prom/prometheus:latest
docker.io/prom/prometheus:latest[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest a3d385fc29f9 12 days ago 201MB
在 client 上获取prometheus.yml配置文件
# 将prometheus的安装包上传至主机中,解压,将prometheus.yaml配置文件传输到master主机的/opt目录中
[root@client ~]# ls
anaconda-ks.cfg prometheus-2.31.1.linux-amd64.tar.gz
[root@client ~]# tar xf prometheus-2.31.1.linux-amd64.tar.gz
[root@client ~]# cd prometheus-2.31.1
[root@client prometheus-2.31.1]# scp /root/prometheus-2.31.1/prometheus.yml 192.168.200.144:/opt/prometheus.yml
root@192.168.200.144's password:
prometheus.yml
master 主机上使用官方镜像运行prometheus 容器,并进行端口和目录文件映射
[root@master ~]# cat /opt/prometheus.yml # scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:# Load rules once and periodically evaluate them accordin
g to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint
to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to
any timeseries scraped from this config.- job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"]# 映射端口和配置文件到主机上且设置随docker启动而启动容器
[root@master ~]# docker run -d --name prometheus --resta
rt always -p 9090:9090 -v /opt/prometheus.yml:/etc/prome
theus/prometheus.yml prom/prometheus
75a99ef651b902499b981b509593d9ad128d6cca6fcb596bd4b148c5d0f7d3ac# 查看容器运行状态
[root@master ~]# docker ps | grep prometheus
75a99ef651b9 prom/prometheus "/bin/prometheus --c…" 12 seconds ago Up 9 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
访问ip+9090
查看此时prometheus 监控的对象(本机)
client 端 部署cAdvisor
[root@client ~]# docker pull google/cadvisor
Using default tag: latest
latest: Pulling from google/cadvisor
ff3a5c916c92: Pull complete
44a45bb65cdf: Pull complete
0bbe1a2fe2a6: Pull complete
Digest: sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04
Status: Downloaded newer image for google/cadvisor:latest
docker.io/google/cadvisor:latest
[root@client ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
google/cadvisor latest eb1210707573 3 years ago 69.6MB
在 client 主机上使用官方镜像运行cadvisor容器并进行目录、端口映射
docker run \--volume=/:/rootfs:ro \--volume=/var/run:/var/run:ro \--volume=/sys:/sys:ro \--volume=/var/lib/docker/:/var/lib/docker:ro \--volume=/dev/disk/:/dev/disk:ro \--publish=8080:8080 \--detach=true \--name=cadvisor \--privileged \--device=/dev/kmsg \google/cadvisor
用上述命令运行运行cadvisor
[root@client ~]# docker rm -f 153dea3a6d5b
153dea3a6d5b
[root@client ~]#
[root@client ~]#
[root@client ~]# docker run \
> --volume=/:/rootfs:ro \
> --volume=/var/run:/var/run:ro \
> --volume=/sys:/sys:ro \
> --volume=/var/lib/docker/:/var/lib/docker:ro \
> --volume=/dev/disk/:/dev/disk:ro \
> --publish=8080:8080 \
> --detach=true \
> --name=cadvisor \
> --privileged \
> --device=/dev/kmsg \
> google/cadvisor
57297e7bc46fd269834fcff4e9338a37f8b888ff05e08cfc5d08cc78896fcf7a
[root@client ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57297e7bc46f google/cadvisor "/usr/bin/cadvisor -…" 23 seconds ago Up 22 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp cadvisor
ip+8080
查看docker容器详细信息
在 master 主机上配置prometheus.yml文件
使prometheus能够接受到cadvisor采集的信息从而实现对cadvisor所处主机的监控
[root@master ~]# vi /opt/prometheus.yml
# my global config
global:# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them accordin
g to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint
to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to
any timeseries scraped from this config.- job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"]- job_name: "Rong Qi" static_configs: - targets: ["192.168.200.145:8080"] 重启Prometheus容器,重载配置文件
[root@master ~]# docker restart prometheus
prometheus查看容器运行情况
[root@master ~]# docker ps | grep prometheus
75a99ef651b9 prom/prometheus "/bin/prometheus --c…" 17 minutes ago Up 10 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
网页访问,查看监控情况
master端安装Grafana
用来获取Prometheus的监控数据,从而实现监控数据可视化
拉取grafan/grafan官方镜像
[root@master ~]# docker pull grafana/grafana
Using default tag: latest
latest: Pulling from grafana/grafana
97518928ae5f: Pull complete
5b58818b7f48: Pull complete
d9a64d9fd162: Pull complete
4e368e1b924c: Pull complete
867f7fdd92d9: Pull complete
387c55415012: Pull complete
07f94c8f51cd: Pull complete
ce8cf00ff6aa: Pull complete
e44858b5f948: Pull complete
4000fdbdd2a3: Pull complete
Digest: sha256:18d94ae734accd66bccf22daed7bdb20c6b99aa0f2c687eea3ce4275fe275062
Status: Downloaded newer image for grafana/grafana:latest
docker.io/grafana/grafana:latest
使用镜像运行grafana容器,并映射端口提供服务
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest a3d385fc29f9 12 days ago 201MB
grafana/grafana latest 9b957e098315 2 weeks ago 275MB
[root@master ~]# docker run -dit --name grafan -p 3000:3
000 grafana/grafana
39435e61acd4dbcd2accdd80dc725526d89cbf1df3630bc3f2eb1d77fd1ad98a
[root@master ~]# docker ps | grep grafan
39435e61acd4 grafana/grafana "/run.sh" 10 seconds ago Up 8 seconds 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp grafan
访问主页(ip+3000端口号)
第一次登入,需要修改密码
添加prometheus 数据源(就是prometheus的访问地址)
填写过后,向下划,点击保存并测试
添加数据源完成,后导入一个模板
输入模板编码11600
选择数据源
另一个模板
AlertManager 配置邮件告警
AlertManager 默认配置文件为 alertmanager.yml,在容器内路径为 /etc/alertmanager/alertmanager.yml,默认配置如下:
global:resolve_timeout: 5mroute:group_by: ['alertname']group_wait: 10sgroup_interval: 10srepeat_interval: 1hreceiver: 'web.hook'
receivers:
- name: 'web.hook'webhook_configs:- url: 'http://127.0.0.1:5001/'
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']
简单介绍一下主要配置的作用:
- global: 全局配置,包括报警解决后的超时时间、SMTP 相关配置、各种渠道通知的 API 地址等等。
- route: 用来设置报警的分发策略,它是一个树状结构,按照深度优先从左向右的顺序进行匹配。
- receivers: 配置告警消息接受者信息,例如常用的 email、wechat、slack、webhook 等消息通知方式。
- inhibit_rules: 抑制规则配置,当存在与另一组匹配的警报(源)时,抑制规则将禁用与一组匹配的警报(目标)。
那么,我们就来配置一下使用 Email 方式通知报警信息,这里以 网易 邮箱为例,配置如下:
[root@client ~]# vi alertmanager.yml
global:resolve_timeout: 6msmtp_from: 'syb0806@163.com'smtp_smarthost: 'smtp.163.com:465'smtp_auth_username: 'syb0806@163.com'smtp_auth_password: 'AMUXINGCHEN'smtp_require_tls: falsesmtp_hello: 'qq.com'
route:group_by: ['alertname']group_wait: 6sgroup_interval: 6srepeat_interval: 6mreceiver: 'email'
receivers:
- name: 'email'email_configs:- to: '1978974056@qq.com'send_resolved: true
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']
- smtp_smarthost: 这里为 QQ 邮箱 SMTP 服务地址,官方地址为 smtp.qq.com 端口为 465 或 587,同时要设置开启 POP3/SMTP 服务。
- smtp_auth_password: 这里为第三方登录 QQ 邮箱的授权码,非 QQ 账户登录密码,否则会报错,获取方式在 QQ 邮箱服务端设置开启 POP3/SMTP 服务时会提示。
- smtp_require_tls: 是否使用 tls,根据环境不同,来选择开启和关闭。如果提示报错 email.loginAuth failed: 530 Must issue a STARTTLS command first,那么就需要设置为 true。着重说明一下,如果开启了 tls,提示报错 starttls failed: x509: certificate signed by unknown authority,需要在 email_configs 下配置 - - insecure_skip_verify: true 来跳过 tls 验证。
创建alertmanager容器,将配置文件 copy 到 client主机上
[root@client ~]# docker cp alertmanager:/etc/alertmanager/alertmanager.yml /root/alertmanager.yml[root@client ~]# mkdir prometheus
[root@client ~]# mv alertmanager.yml prometheus
[root@client ~]# cd prometheus
[root@client prometheus]# ls
alertmanager.yml
修改 AlertManager 启动命令,将本地 alertmanager.yml 文件挂载到容器内指定位置。
[root@client ~]# docker run -d --name alertmanager -p 9093:9093 -v /root/prometheus/alertmanager.yml:/etc/alertmanager/alertmanager.yml prom/alertmanager
3b32978a4d39bae1a2a946802551b70b8e05b04ae2ed01d5f55795170f980b9a[root@client ~]# docker ps | grep alertmanager
3b32978a4d39 prom/alertmanager "/bin/alertmanager -…" 26 seconds ago Up 24 seconds 0.0.0.0:9093->9093/tcp, :::9093->9093/tcp alertmanager
Prometheus 配置 AlertManager 告警规则(master端)
接下来,我们需要在 Prometheus 配置 AlertManager 服务地址以及告警规则,新建报警规则文件 node-up.rules 如下:
$ mkdir -p /opt/prometheus/rules && cd /opt/prometheus/rules/
$ vim node-up.rules
groups:
- name: node-uprules:- alert: node-upexpr: up{
job="node-exporter"} == 0for: 15slabels:severity: 1team: nodeannotations:summary: "192.168.200.145 已停止运行超过 15s!"
说明一下:该 rules 目的是监测 node 是否存活,expr 为 PromQL 表达式验证特定节点 job=“node-exporter” 是否活着,for 表示报警状态为 Pending 后等待 15s 变成 Firing 状态,一旦变成 Firing 状态则将报警发送到 AlertManager。
然后,修改 prometheus.yml 配置文件,添加 rules 规则文件。
[root@master ~]# vim /opt/prometheus.yml
# my global config
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.# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:- 192.168.200.145:9093 // 修改此行rule_files: // 添加两行- "/usr/local/prometheus/rules/*.rules"