Kube-Eventer的开挂操作

开发 前端
在Kubernetes中,事件分为两种,一种是Warning事件,表示产生这个事件的状态转换是在非预期的状态之间产生的;另外一种是Normal事件,表示期望到达的状态,和目前达到的状态是一致的。

[[400338]]

本文转载自微信公众号「运维开发故事」,作者没有文案的夏老师。转载本文请联系运维开发故事公众号。

离线事件告警

kube-eventer是由阿里开源的k8s离线事件收集器,开源地址

https://github.com/AliyunContainerService/kube-eventer/blob/master/docs/en/webhook-sink.md

在Kubernetes中,事件分为两种,一种是Warning事件,表示产生这个事件的状态转换是在非预期的状态之间产生的;另外一种是Normal事件,表示期望到达的状态,和目前达到的状态是一致的。

我们以NPD的event来讲解。事件影响节点的临时性问题,但是它是对于系统诊断是有意义的。NPD就是利用kubernetes的上报机制,通过检测系统的日志(例如centos中journal),把错误的信息上报到kuberntes的node上。这些日志(例如内核日志)中噪音信息太多,NPD会提取其中有价值的信息,可以将这些信息生成离线事件。这样我就可以得到node上的时间,及时进行处理。

一个标准的Kubernetes事件有如下几个重要的属性,通过这些属性可以更好地诊断和告警问题。Namespace:产生事件的对象所在的命名空间。

Kind:绑定事件的对象的类型,例如:Node、Pod、Namespace、Componenet等等。

Timestamp:事件产生的时间等等。

Reason:产生这个事件的原因。Message: 事件的具体描述。

 

目前的sinks支持大致如下:

Sink Name Description
dingtalk sink to dingtalk bot
sls sink to alibaba cloud sls service
elasticsearch sink to elasticsearch
honeycomb sink to honeycomb
influxdb sink to influxdb
kafka sink to kafka
mysql sink to mysql database
wechat sink to wechat

今天主要带来webhook的开挂技巧。首先看支持的参数:

  • level - Level of event (optional. default: Warning. Options: Warning and Normal)
  • namespaces - Namespaces to filter (optional. default: all namespaces,use commas to separate multi namespaces, namespace filter doesn't support regexp)
  • kinds - Kinds to filter (optional. default: all kinds,use commas to separate multi kinds. Options: Node,Pod and so on.)
  • reason - Reason to filter (optional. default: empty, Regexp pattern support). You can use multi reason fields in query.
  • method - Method to send request (optional. default: GET)
  • header - Header in request (optional. default: empty). You can use multi header field in query.
  • custom_body_configmap - The configmap name of request body template. You can use Template to customize request body. (optional.)
  • custom_body_configmap_namespace - The configmap namespace of request body template.

如果每个项目namespace与负责人是一一对应的,就可以根据configmap与sink关联起来。变更上线部署是最容易出现事件的时候,通过事件是可以快速的发现上线的镜像tag错误,镜像配置错误等问题。

首先configmap,通过custom_body_configmap的值来选择不同的配置文件。可以简单修饰一下,使其变得更加清晰。

添加加Cluster:name可以知道是哪个集群的event。

添加加"mentioned_list":["wangqin","@all"]可以@对应的负责人。

  1. --- 
  2. apiVersion: v1 
  3. data: 
  4.   content: >- 
  5.        {"msgtype""text","text": {"content""Cluster:name\nEventType:{{ .Type }}\nEventNamespace:{{ .InvolvedObject.Namespace }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventObject:{{ .InvolvedObject.Name }}\nEventReason:{{ .Reason }}\nEventTime:{{ .LastTimestamp }}\nEventMessage:{{ .Message }}","mentioned_list":["wangqing","@all"]}} 
  6. kind: ConfigMap 
  7. metadata: 
  8.   name: custom-webhook-body 
  9.   namespace: nameapce 

命令部分的技巧

sink是一个数组,可以加很多条。

主要说明用webhook向企业微信的的通知。注意reason是可以支持正则表达式的。通过configmap就一起完成了k8s机器的事件告警。

  1. command: 
  2.   - "/kube-eventer" 
  3.   - "--source=kubernetes:https://kubernetes.default" 
  4.   ## .e.g,dingtalk sink demo 
  5.  - --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=[^Unhealthy]&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body0&custom_body_configmap_namespace=xxxx&method=POST 
  6.  - --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=BackOff&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body1&custom_body_configmap_namespace=xxxx&method=POST 
  7.  - --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=Failed&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body2&custom_body_configmap_namespace=xxxxx&method=POST 

案列:

创建一个企业微信群的机器人。比如:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx。

  1. apiVersion: apps/v1 
  2. kind: Deployment 
  3. metadata: 
  4.   labels: 
  5.     name: kube-eventer 
  6.   name: kube-eventer 
  7.   namespace: namespace 
  8. spec: 
  9.   replicas: 1 
  10.   selector: 
  11.     matchLabels: 
  12.       app: kube-eventer 
  13.   template: 
  14.     metadata: 
  15.       labels: 
  16.         app: kube-eventer 
  17.       annotations:  
  18.         scheduler.alpha.kubernetes.io/critical-pod: '' 
  19.     spec: 
  20.       dnsPolicy: ClusterFirstWithHostNet 
  21.       serviceAccount: kube-eventer 
  22.       containers: 
  23.         - image: registry.aliyuncs.com/acs/kube-eventer-amd64:v1.2.0-484d9cd-aliyun 
  24.           name: kube-eventer 
  25.           command: 
  26.             - "/kube-eventer" 
  27.             - "--source=kubernetes:https://kubernetes.default" 
  28.             ## .e.g,dingtalk sink demo 
  29.             - --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=[^Unhealthy]&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body0&custom_body_configmap_namespace=xxxx&method=POST 
  30.             #- --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=BackOff&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body1&custom_body_configmap_namespace=xxxx&method=POST 
  31.             #- --sink=webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx&level=Warning&reason=Failed&namespaces=xxxx&header=Content-Type=application/json&custom_body_configmap=custom-webhook-body2&custom_body_configmap_namespace=xxxxx&method=POST 
  32.           env: 
  33.           # If TZ is assigned, set the TZ value as the time zone 
  34.           - name: TZ 
  35.             value: "Asia/Shanghai"  
  36.           volumeMounts: 
  37.             - name: localtime 
  38.               mountPath: /etc/localtime 
  39.               readOnly: true 
  40.             - name: zoneinfo 
  41.               mountPath: /usr/share/zoneinfo 
  42.               readOnly: true 
  43.           resources: 
  44.             requests: 
  45.               cpu: 200m 
  46.               memory: 100Mi 
  47.             limits: 
  48.               cpu: 500m 
  49.               memory: 250Mi 
  50.       volumes: 
  51.         - name: localtime 
  52.           hostPath: 
  53.             path: /etc/localtime 
  54.         - name: zoneinfo 
  55.           hostPath: 
  56.             path: /usr/share/zoneinfo 
  57. --- 
  58. apiVersion: rbac.authorization.k8s.io/v1 
  59. kind: ClusterRole 
  60. metadata: 
  61.   name: kube-eventer 
  62. rules: 
  63.   - apiGroups: 
  64.       - "" 
  65.     resources: 
  66.       - events 
  67.       - configmaps 
  68.     verbs: 
  69.       - get 
  70.       - list 
  71.       - watch 
  72. --- 
  73. apiVersion: rbac.authorization.k8s.io/v1 
  74. kind: ClusterRoleBinding 
  75. metadata: 
  76.   name: kube-eventer 
  77. roleRef: 
  78.   apiGroup: rbac.authorization.k8s.io 
  79.   kind: ClusterRole 
  80.   name: kube-eventer 
  81. subjects: 
  82.   - kind: ServiceAccount 
  83.     name: kube-eventer 
  84.     namespace: namespace 
  85. --- 
  86. apiVersion: v1 
  87. kind: ServiceAccount 
  88. metadata: 
  89.   name: kube-eventer 
  90.   namespace: namespace 
  91.  
  92. --- 
  93. apiVersion: v1 
  94. data: 
  95.   content: >- 
  96.        {"msgtype""text","text": {"content""Cluster:name\nEventType:{{ .Type }}\nEventNamespace:{{ .InvolvedObject.Namespace }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventObject:{{ .InvolvedObject.Name }}\nEventReason:{{ .Reason }}\nEventTime:{{ .LastTimestamp }}\nEventMessage:{{ .Message }}","mentioned_list":["wangqing","@all"]}} 
  97. kind: ConfigMap 
  98. metadata: 
  99.   name: custom-webhook-body 
  100.   namespace: nameapce 

 

这样就可以完成向谁告警,谁进行处理的简单分配。有了事件告警,可以及时发现服务问题与集群问题并进行修复。

 

责任编辑:武晓燕 来源: 运维开发故事
相关推荐

2017-03-01 19:45:15

戴尔服务器

2015-12-31 11:30:10

趋势科技/信息安全

2022-07-21 10:05:13

勒索软件网络安全

2017-06-29 11:00:49

2018-02-13 14:56:24

戴尔

2017-02-10 16:39:47

戴尔商用电脑促销

2021-09-10 09:58:35

AvlBST时间

2022-03-17 09:33:28

AI深度学习思考

2019-04-26 13:26:00

预测股票深度学习股票

2024-01-04 17:24:02

2022-01-15 23:04:03

人工智能高等数学技术

2015-07-30 13:49:23

2020-04-07 11:12:23

编程IBM印度裔

2023-04-03 14:52:00

谷歌数学

2012-06-29 15:03:23

傲游浏览器

2020-11-10 10:10:37

首席信息官ITCIO

2023-11-29 09:29:48

Kuberneteskube

2023-10-30 22:23:12

Cacherkube版本

2022-03-25 19:03:07

IT人生开发

2021-08-17 07:15:15

ciliumKubernetes集群
点赞
收藏

51CTO技术栈公众号