天天看點

阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援

前言

kube-eventer

是Kubernetes社群中針對事件監控、報警、chatOps場景的開源元件,更多資訊可以

點選檢視

。在早期的

kube-eventer

中已經支援了釘釘、微信、slack等即時通信軟體機器人的接入,但是每個機器人的演進速度、功能支援有所不同,造成開發者無法在不同的即時通信機器人之間擁有一緻性的體驗。為了解決這個問題,在最新版本的

kube-eventer

推出了支援泛化能力的

Webhook

Sink。開發者可以通過自定義請求位址、鑒權方式、請求體結構等内容支援各種類Webhook的事件離線信道。

功能介紹

新推出的

Webhook

Sink支援根據事件的Kind、Reason、Level、Namespace進行過濾,支援通過泛化的邏輯将資料離線給自定義Webhook系統、釘釘、微信、貝洽(bear chat)、slack等等。那麼如何使用泛化的Webhook來實作上述的邏輯呢?首先我們先來看下Webhook Sink的參數與定義。

--sink=webhook:<WEBHOOK_URL>?level=Warning&namespaces=ns1,ns2&kinds=Node,Pod&method=POST&header=customHeaderKey=customHeaderValue            

參數全部聲明如下

  • 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, Regexp pattern support)
  • 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 field 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. (optional.)

其中level、namespaces、kinds、reason都是用來過濾的Filter,其中reson是支援正則的,可以通過标準的正規表達式提供更強大的過濾能力,并且reason可以在參數中設定多條,例如reaon=(a|b)&reson=(c|d)。預設情況下webhook的body為

{
    "EventType": "{{ .Type }}",
    "EventKind": "{{ .InvolvedObject.Kind }}",
    "EventReason": "{{ .Reason }}",
    "EventTime": "{{ .EventTime }}",
    "EventMessage": "{{ .Message }}"
}           

開發者可以通過解析這個Json格式的Body擷取事件的内容,此外開發者也可以通過custom_body_configmap與custom_body_configmap_namespace字段進行自定義設定。其中configmap的結構如下,預設kube-eventer會從configmap的content字段中擷取Body的内容。

apiVersion: v1
data:
  content: >-
    {"EventType": "{{ .Type }}","EventKind": "{{ .InvolvedObject.Kind }}""EventReason": "{{
    .Reason }}","EventTime": "{{ .EventTime }}","EventMessage": "{{ .Message
    }}"}
kind: ConfigMap
metadata: 
  name: custom-webhook-body 
  namespace: kube-system            

泛化Webhook配置例子

具體配置與效果如下

釘釘

參數例子

--sink=webhook:https://oapi.dingtalk.com/robot/send?access_token=token&level=Normal&kinds=Pod&header=Content-Type=application/json&custom_body_configmap=custom-body&custom_body_configmap_namespace=kube-system&method=POST
           

configmap内容

{"msgtype": "text","text": {"content":"EventType:{{ .Type }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventReason:{{ .Reason }}\nEventTime:{{ .EventTime }}\nEventMessage:{{ .Message }}"},"markdown": {"title":"","text":""}}           

顯示效果

阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援

微信

--sink=webhook:http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=633a31f6-7f9c-4bc4-97a0-0ec1eefa5898&level=Normal&kinds=Pod&header=Content-Type=application/json&custom_body_configmap=custom-body&custom_body_configmap_namespace=kube-system&method=POST           
{"msgtype": "text","text": {"content": "EventType:{{ .Type }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventReason:{{ .Reason }}\nEventTime:{{ .EventTime }}\nEventMessage:{{ .Message }}"}}           

slack

--sink=webhook:https://hooks.slack.com/services/d/B00000000/XXX?&level=Normal&kinds=Pod&header=Content-Type=application/json&custom_body_configmap=custom-body&custom_body_configmap_namespace=kube-system&method=POST           
{"channel": "testing","username": "Eventer","text":"EventType:{{ .Type }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventReason:{{ .Reason }}\nEventTime:{{ .EventTime }}\nEventMessage:{{ .Message }}"}           
阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援

貝洽(bear chat)

--sink=webhook:https://hook.bearychat.com/=bwIsS/incoming/xxxxxx?kinds=Pod&header=Content-Type=application/json&custom_body_configmap=custom-body&custom_body_configmap_namespace=kube-system&method=POST           
"text":"EventType:{{ .Type }}\nEventKind:{{ .InvolvedObject.Kind }}\nEventReason:{{ .Reason }}\nEventTime:{{ .EventTime }}\nEventMessage:{{ .Message }}"           
阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援

在容器服務中使用自定義Webhook

容器服務中已經在應用市場中提供了

ack-node-problem-detector

的Chart,預設提供了kube-eventer和npd的打包與內建。開發者可以通過在Chart中OtherSinks的字段進行自定義Webhook的使用。

阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援
阿裡雲容器Kubernetes監控(十) - kube-eventer釋出Webhook信道支援

最後

泛化Webhook的支援可以讓更多的ChatBot無需修改代碼即可進行接入,例如本文中未提到的Teams、WhatsApp等等。有興趣的開發者可以送出案例到

Github