kubernetes之监控Prometheus实战--邮件告警--微信告警(二)

kubernetes插件

上面是咱们最经常使用的 grafana 当中的 dashboard 的功能的使用,而后咱们也能够来进行一些其余的系统管理,好比添加用户,为用户添加权限等等,咱们也能够安装一些其余插件,好比 grafana 就有一个专门针对 Kubernetes 集群监控的插件:grafana-kubernetes-appnode

要安装这个插件,须要到 grafana 的 Pod 里面去执行安装命令:git

kubectl get pods -n kube-ops NAME READY STATUS RESTARTS AGE grafana-75f64c6759-gknxz      1/1       Running   0 1d node-exporter-48b6g           1/1       Running   0 7d node-exporter-4swrs           1/1       Running   0 7d node-exporter-4w2dd           1/1       Running   0 7d node-exporter-fcp9x           1/1       Running   0 7d prometheus-56b6d68c48-6xpvw   1/1       Running   0 7d [root@k8s-master ~]# kubectl exec -it grafana-75f64c6759-gknxz /bin/bash -n kube-ops grafana@grafana-75f64c6759-gknxz:/usr/share/grafana$ grafana-cli plugins install grafana-kubernetes-app installing grafana-kubernetes-app @ 1.0.1 from url: https://grafana.com/api/plugins/grafana-kubernetes-app/versions/1.0.1/download
into: /var/lib/grafana/plugins ✔ Installed grafana-kubernetes-app successfully Restart grafana after installing plugins . <service grafana-server restart>

安装完成后须要重启 grafana 才会生效,咱们这里直接删除 Pod,重建便可,而后回到 grafana 页面中,切换到 plugins 页面能够发现下面多了一个 Kubernetes 的插件,点击进来启用便可,而后点击Next up旁边的连接配置集群github

这里咱们能够添加一个新的 Kubernetes 集群,这里须要填写集群的访问地址:https://kubernetes.default,而后比较重要的是集群访问的证书,勾选上TLS Client AuthWith CA Cert这两项。正则表达式

集群访问的证书文件,用咱们访问集群的 kubectl 的配置文件中的证书信息(~/.kube/config)便可,其中属性certificate-authority-dataclient-certificate-dataclient-key-data就对应这 CA 证书、Client 证书、Client 私钥,不过 config 文件里面的内容是base64编码事后的,因此咱们这里填写的时候要作base64解码。docker

配置完成后,能够直接点击Deploy(实际上前面的课程中咱们都已经部署过相关的资源了),而后点击Save,就能够获取到集群的监控资源信息了。api

能够看到上面展现了整个集群的状态,能够查看上面的一些 Dashboard:bash

 

AlertManager

Alertmanager 主要用于接收 Prometheus 发送的告警信息,它支持丰富的告警通知渠道,并且很容易作到告警信息进行去重,降噪,分组等,是一款前卫的告警通知系统。服务器

安装

从官方文档https://prometheus.io/docs/alerting/configuration/中咱们能够看到下载AlertManager二进制文件后,能够经过下面的命令运行:微信

$ ./alertmanager --config.file=simple.yml

其中-config.file参数是用来指定对应的配置文件的,因为咱们这里一样要运行到 Kubernetes 集群中来,因此咱们使用docker镜像的方式来安装,使用的镜像是:prom/alertmanager:v0.15.3app

首先,指定配置文件,一样的,咱们这里使用一个 ConfigMap 资源对象:(alertmanager-conf.yaml)

apiVersion: v1 kind: ConfigMap metadata: name: alert-config namespace: kube-ops data: config.yml: |- global: # 在没有报警的状况下声明为已解决的时间 resolve_timeout: 5m # 配置邮件发送信息 smtp_smarthost: 'smtp.qq.com:587' smtp_from: 'zhaikun1992@qq.com' smtp_auth_username: 'zhaikun1992@qq.com' smtp_auth_password: '**' #改为本身的密码 smtp_hello: 'qq.com' smtp_require_tls: false # 全部报警信息进入后的根路由,用来设置报警的分发策略 route: # 这里的标签列表是接收到报警信息后的从新分组标签,例如,接收到的报警信息里面有许多具备 cluster=A 和 alertname=LatncyHigh 这样的标签的报警信息将会批量被聚合到一个分组里面 group_by: ['alertname', 'cluster'] # 当一个新的报警分组被建立后,须要等待至少group_wait时间来初始化通知,这种方式能够确保您能有足够的时间为同一分组来获取多个警报,而后一块儿触发这个报警信息。 group_wait: 30s # 当第一个报警发送后,等待'group_interval'时间来发送新的一组报警信息。 group_interval: 5m # 若是一个报警信息已经发送成功了,等待'repeat_interval'时间来从新发送他们 repeat_interval: 5m # 默认的receiver:若是一个报警没有被一个route匹配,则发送给默认的接收器 receiver: default # 上面全部的属性都由全部子路由继承,而且能够在每一个子路由上进行覆盖。 routes: - receiver: email group_wait: 10s match: team: node receivers: - name: 'default' email_configs: - to: 'zhai_kun@suixingpay.com' send_resolved: true
    - name: 'email' email_configs: - to: 'zhaikun1992@qq.com' send_resolved: true

这里有一个坑。若是咱们邮件服务器使用的是25或者465端口的话,或报以下错误:

smtp.*****.com:465 fail to send mail alert due to 'does not advertise the STARTTLS extension

这里是一个BUG,能够参考

 建立

$ kubectl create -f alertmanager-conf.yaml configmap "alert-config" created

而后配置 AlertManager 的容器,咱们能够直接在以前的 Prometheus 的 Pod 中添加这个容器,对应的 YAML 资源声明以下:

- name: alertmanager image: prom/alertmanager:v0.15.3 imagePullPolicy: IfNotPresent args: - "--config.file=/etc/alertmanager/config.yml"
- "--storage.path=/alertmanager/data"
ports: - containerPort: 9093 name: http volumeMounts: - mountPath: "/etc/alertmanager" name: alertcfg resources: requests: cpu: 100m memory: 256Mi limits: cpu: 100m memory: 256Mi volumes: - name: alertcfg configMap: name: alert-config

这里咱们将上面建立的 alert-config 这个 ConfigMap 资源对象以 Volume 的形式挂载到 /etc/alertmanager 目录下去,而后在启动参数中指定了配置文件--config.file=/etc/alertmanager/config.yml,而后咱们能够来更新这个 Prometheus 的 Pod:

$ kubectl apply -f prome-deploy.yaml deployment.extensions "prometheus" configured

AlertManager 的容器启动起来后,咱们还须要在 Prometheus 中配置下 AlertManager 的地址,让 Prometheus 可以访问到 AlertManager,在 Prometheus 的 ConfigMap 资源清单中添加以下配置:

alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"]

更新这个资源对象后,稍等一小会儿,执行 reload 操做:

$ kubectl delete -f prome-cm.yaml configmap "prometheus-config" deleted $ kubectl create -f prome-cm.yaml configmap "prometheus-config" created kubectl get svc -n kube-ops NAME TYPE CLUSTER-IP      EXTERNAL-IP PORT(S) AGE grafana NodePort 10.97.81.127    <none>        3000:30489/TCP 75d prometheus NodePort 10.111.210.47   <none>        9090:31990/TCP,9093:30250/TCP 77d $ curl -X POST "http://10.111.210.47:9090/-/reload"

报警规则

如今咱们只是把AlertManager运行起来了。可是它并不知道要报什么警。由于没有任何地方告诉咱们要报警,因此咱们还须要配置一些报警规则来告诉咱们对哪些数据进行报警。

警报规则容许你基于 Prometheus 表达式语言的表达式来定义报警报条件,并在触发警报时发送通知给外部的接收者。

一样在 Prometheus 的配置文件中添加以下报警规则配置:

rule_files: - /etc/prometheus/rules.yml

其中rule_files就是用来指定报警规则的,这里咱们一样将rules.yml文件用 ConfigMap 的形式挂载到/etc/prometheus目录下面便可:

apiVersion: v1 kind: ConfigMap metadata: name: prometheus-config namespace: kube-ops data: prometheus.yml: | ... rules.yml: |
    groups: - name: test-rule rules: - alert: NodeMemoryUsage expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes * 100 > 20
        for: 2m labels: team: node annotations: summary: "{{$labels.instance}}: High Memory usage detected" description: "{{$labels.instance}}: Memory usage is above 20% (current value is: {{ $value }}"

上面咱们定义了一个名为NodeMemoryUsage的报警规则,其中:

  • for语句会使 Prometheus 服务等待指定的时间, 而后执行查询表达式。
  • labels语句容许指定额外的标签列表,把它们附加在告警上。
  • annotations语句指定了另外一组标签,它们不被当作告警实例的身份标识,它们常常用于存储一些额外的信息,用于报警信息的展现之类的。

为了方便演示,咱们将的表达式判断报警临界值设置为20,从新更新 ConfigMap 资源对象,因为咱们在 Prometheus 的 Pod 中已经经过 Volume 的形式将 prometheus-config 这个一个 ConfigMap 对象挂载到了/etc/prometheus目录下面,因此更新后,该目录下面也会出现rules.yml文件,因此前面配置的rule_files路径也是正常的,更新完成后,从新执行reload操做,这个时候咱们去 Prometheus 的 Dashboard 中切换到alerts路径下面就能够看到有报警配置规则的数据了:

由于咱们配置了for等待时间,由于如今是PENDING状态。过两分钟后,咱们会发现进入FIRING状态

咱们能够看到页面中出现了咱们刚刚定义的报警规则信息,并且报警信息中还有状态显示。一个报警信息在生命周期内有下面3种状态:

  • inactive: 表示当前报警信息既不是firing状态也不是pending状态
  • pending: 表示在设置的阈值时间范围内被激活了
  • firing: 表示超过设置的阈值时间被激活了

咱们这里的状态如今是firing就表示这个报警已经被激活了,咱们这里的报警信息有一个team=node这样的标签,而最上面咱们配置 alertmanager 的时候就有以下的路由配置信息了:

routes: - receiver: email group_wait: 10s match: team: node

因此咱们这里的报警信息会被email这个接收器来进行报警,咱们上面配置的是邮箱,因此正常来讲这个时候咱们会收到一封以下的报警邮件:

咱们能够看到收到的邮件内容中包含一个View In AlertManager的连接,咱们一样能够经过 NodePort 的形式去访问到 AlertManager 的 Dashboard 页面: 

 

$ kubectl get svc -n kube-ops NAME TYPE CLUSTER-IP      EXTERNAL-IP PORT(S) AGE grafana NodePort 10.97.81.127    <none>        3000:30489/TCP 75d prometheus NodePort 10.111.210.47   <none>        9090:31990/TCP,9093:30250/TCP   77d

经过任意节点IP:30250进行访问,咱们就能够查看到 AlertManager 的 Dashboard 页面:

 

在这个页面中咱们能够进行一些操做,好比过滤、分组等等,里面还有两个新的概念:Inhibition(抑制)和 Silences(静默)。

  • Inhibition:若是某些其余警报已经触发了,则对于某些警报,Inhibition 是一个抑制通知的概念。例如:一个警报已经触发,它正在通知整个集群是不可达的时,Alertmanager 则能够配置成关心这个集群的其余警报无效。这能够防止与实际问题无关的数百或数千个触发警报的通知,Inhibition 须要经过上面的配置文件进行配置。
  • Silences:静默是一个很是简单的方法,能够在给定时间内简单地忽略全部警报。Silences 基于 matchers配置,相似路由树。来到的警告将会被检查,判断它们是否和活跃的 Silences 相等或者正则表达式匹配。若是匹配成功,则不会将这些警报发送给接收者。

因为全局配置中咱们配置的repeat_interval: 5m,因此正常来讲,上面的测试报警若是一直知足报警条件(CPU使用率大于20%)的话,那么每5分钟咱们就能够收到一条报警邮件。

如今咱们添加一个 Silences,以下图所示,匹配 k8s-master 节点的内存报警:

添加完成后,等下一次的报警信息触发后,咱们能够看到报警信息里面已经没有了节点 k8s-master 的报警信息了(报警节点变成3个了):

因为咱们上面添加的 Silences 是有过时时间的,因此在这个时间段事后,k8s-master 的报警信息就会恢复了。

微信告警

一、登陆企业微信,建立第三方应用,点击建立应用按钮 -> 填写应用信息:

咱们打开已经建立好的promethues应用。获取一下信息:

 

新增告警信息模板

apiVersion: v1 kind: ConfigMap metadata: name: wechat-tmpl namespace: kube-ops data: wechat.tmpl: | {{ define "wechat.default.message" }} {{ range .Alerts }} ========start========== 告警程序: prometheus_alert 告警级别: {{ .Labels.severity }} 告警类型: {{ .Labels.alertname }} 故障主机: {{ .Labels.instance }} 告警主题: {{ .Annotations.summary }} 告警详情: {{ .Annotations.description }} 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} ========end========== {{ end }} {{ end }}

把模板挂载到alertmanager 的pod中的/etc/alertmanager-tmpl 

 volumeMounts: ........ - mountPath: "/etc/alertmanager-tmpl" name: wechattmpl ........ volumes: ......... - name: wechattmpl configMap: name: wechat-tmpl

在rules.yml中添加一个新的告警信息。

- alert: NodeFilesystemUsage expr: (node_filesystem_size_bytes{device="rootfs"} - node_filesystem_free_bytes{device="rootfs"}) / node_filesystem_size_bytes{device="rootfs"} * 100 > 10
        for: 2m labels: team: wechat annotations: summary: "{{$labels.instance}}: High Filesystem usage detected" description: "{{$labels.instance}}: Filesystem usage is above 10% (current value is: {{ $value }}"

alertmanager中默认支持微信告警通知。咱们能够经过官网查看 咱们的配置以下:

apiVersion: v1 kind: ConfigMap metadata: name: alert-config namespace: kube-ops data: config.yml: |- global: resolve_timeout: 5m smtp_smarthost: 'smtp.qq.com:587' smtp_from: 'zhaikun1992@qq.com' smtp_auth_username: 'zhaikun1992@qq.com' smtp_auth_password: 'jrmujtmydxtibaid' smtp_hello: 'qq.com' smtp_require_tls: true #指定wechar告警模板 templates: - "/etc/alertmanager-tmpl/wechat.tmpl" route: group_by: ['alertname', 'cluster', 'alertname_wechat'] group_wait: 30s group_interval: 5m repeat_interval: 5m receiver: default routes: - receiver: email group_wait: 10s match: team: node #配置微信的路由分组 - receiver: 'wechat' group_wait: 10s match: team: wechat receivers: - name: 'default' email_configs: - to: 'zhai_kun@suixingpay.com' send_resolved: true
    - name: 'email' email_configs: - to: 'zhaikun1992@qq.com' send_resolved: true #配置微信接收器 - name: 'wechat' wechat_configs: - corp_id: '***' to_party: '**' to_user: "***" agent_id: '***' api_secret: '****' send_resolved: true
wechat_configs 配置详情
  • send_resolved 告警解决是否通知,默认是false
  • api_secret 建立微信上应用的Secret
  • api_url wechat的url。默认便可
  • corp_id 企业微信---个人企业---最下面的企业ID
  • message 告警消息模板:默认 template "wechat.default.message"
  • agent_id 建立微信上应用的agent_id
  • to_user 接受消息的用户 全部用户可使用 @all
  • to_party 接受消息的部门

咱们部署验证一下:

kubectl create -f alertmanager-cm.yaml kubectl create -f prome-server.yaml kubectl create -f prome-confmap.yaml kubectl create -f wechat-tmpl.yaml

稍等两分钟,咱们的微信企业号会接收到告警信息。

相关文章
相关标签/搜索