kube-eventer的开挂操作

简介: kube-eventer的开挂操作

离线事件告警


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: 事件的具体描述。


640.png


目前的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"]可以@对应的负责人。


---
apiVersion: v1
data:
  content: >-
       {"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"]}}
kind: ConfigMap
metadata:
  name: custom-webhook-body
  namespace: nameapce


命令部分的技巧


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


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


command:
  - "/kube-eventer"
  - "--source=kubernetes:https://kubernetes.default"
  ## .e.g,dingtalk sink demo
 - --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
 - --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
 - --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。


apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: kube-eventer
  name: kube-eventer
  namespace: namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kube-eventer
  template:
    metadata:
      labels:
        app: kube-eventer
      annotations: 
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      serviceAccount: kube-eventer
      containers:
        - image: registry.aliyuncs.com/acs/kube-eventer-amd64:v1.2.0-484d9cd-aliyun
          name: kube-eventer
          command:
            - "/kube-eventer"
            - "--source=kubernetes:https://kubernetes.default"
            ## .e.g,dingtalk sink demo
            - --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
            #- --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
            #- --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
          env:
          # If TZ is assigned, set the TZ value as the time zone
          - name: TZ
            value: "Asia/Shanghai" 
          volumeMounts:
            - name: localtime
              mountPath: /etc/localtime
              readOnly: true
            - name: zoneinfo
              mountPath: /usr/share/zoneinfo
              readOnly: true
          resources:
            requests:
              cpu: 200m
              memory: 100Mi
            limits:
              cpu: 500m
              memory: 250Mi
      volumes:
        - name: localtime
          hostPath:
            path: /etc/localtime
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kube-eventer
rules:
  - apiGroups:
      - ""
    resources:
      - events
      - configmaps
    verbs:
      - get
      - list
      - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kube-eventer
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kube-eventer
subjects:
  - kind: ServiceAccount
    name: kube-eventer
    namespace: namespace
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-eventer
  namespace: namespace
---
apiVersion: v1
data:
  content: >-
       {"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"]}}
kind: ConfigMap
metadata:
  name: custom-webhook-body
  namespace: nameapce


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


相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
缓存 NoSQL 网络安全
基于Redis6.2.6版本部署Redis Cluster集群
基于Redis6.2.6版本部署Redis Cluster集群 文章目录 基于Redis6.2.6版本部署Redis Cluster集群 1.Redis6.2.6简介以及环境规划 2.二进制安装Redis程序 2.1.二进制安装redis6.2.6 2.2.创建Reids Cluster集群目录 3.配置Redis Cluster三主三从交叉复制集群 3.1.准备六个节点的redis配置文件 3.2.将六个节点全部启动 3.3.配置集群节点之间相互发现 3.4.为集群中的充当Master的节点分配槽位 3.5.配置三主三从交叉复制模式 4.快速搭建Redis Cluster集群
2342 1
基于Redis6.2.6版本部署Redis Cluster集群
|
5月前
|
人工智能 自然语言处理 知识图谱
Yuxi-Know:开源智能问答系统,基于大模型RAG与知识图谱技术快速构建知识库
Yuxi-Know是一个结合大模型RAG知识库与知识图谱技术的智能问答平台,支持多格式文档处理和复杂知识关系查询,具备多模型适配和智能体拓展能力。
1165 55
Yuxi-Know:开源智能问答系统,基于大模型RAG与知识图谱技术快速构建知识库
|
缓存 Linux
初学环境一:CentOS7更换国内yum源
初学环境一:CentOS7更换国内yum源
4116 0
|
10月前
|
传感器 算法 安全
CAN 帧中 CRC 场的作用
CAN帧中的CRC场用于检测数据传输错误,通过计算发送数据的校验码并在接收端进行验证,确保数据的完整性和准确性。
|
11月前
|
弹性计算 安全 Linux
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
|
11月前
|
弹性计算 网络协议 数据库
在阿里云国际站上解析域名到服务器详细教程
在阿里云国际站上解析域名到服务器详细教程
|
Kubernetes Linux Docker
Linux下minikube启动失败(It seems like the kubelet isn‘t running or healthy)(1)
Linux下minikube启动失败(It seems like the kubelet isn‘t running or healthy)(1)
Linux下minikube启动失败(It seems like the kubelet isn‘t running or healthy)(1)
|
缓存 NoSQL Java
Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用
SpringCache不支持灵活的缓存时间和集群,适合数据量小的单机服务或对一致性要求不高的场景。`@EnableCaching`启用缓存。`@Cacheable`用于缓存方法返回值,`value`指定缓存名称,`key`定义缓存键,可按SpEL编写,`unless`决定是否不缓存空值。当在类上使用时,类内所有方法都支持缓存。`@CachePut`每次执行方法后都会更新缓存,而`@CacheEvict`用于清除缓存,支持按键清除或全部清除。Spring Cache结合Redis可支持集群环境。
982 6
|
SQL 存储 运维
go+vue自建运维管理平台
go+vue自建运维管理平台
539 0
go+vue自建运维管理平台
|
SpringCloudAlibaba Cloud Native Docker
SpringCloudAlibaba:4.1云原生网关higress的搭建
SpringCloudAlibaba:4.1云原生网关higress的搭建
345 1

热门文章

最新文章