前言
這次要介紹一個 Kubernetes 資源觀測工具,實時監控 Kubernetes 叢集中各種資源的建立、更新和删除,并實時通知到各種協作軟體/聊天軟體,目前支援的通知管道有:
-
slack
-
hipchat
-
mattermost
-
flock
-
webhook
我這邊開發了釘釘的通知管道,但是在上遊
ISSUE#198中提出的貢獻請求并沒有得到回應,是以這邊隻能 fork 了代碼,然後自己進行了開發,以支援釘釘通知。
安裝
這裡推薦使用 helm 進行安裝,快速部署
helm install kubewatch stable/kubewatch \
--set rbac.create=true \
--set slack.channel='#YOUR_CHANNEL' \
--set slack.token='xoxb-YOUR_TOKEN' \
--set resourcesToWatch.pod=true \
--set resourcesToWatch.daemonset=true
如果想使用釘釘通知,則可以在
GitHub上拉取我的代碼,代碼中包含 helm chart 包,可直接進行安裝
git clone https://github.com/sunny0826/kubewatch-chat.git
cd kubewatch-chat
helm install kubewatch kubewatch \
--set dingtalk.sign="XXX" \
--set dingtalk.token="XXXX-XXXX-XXXX"
釘釘配置
在釘釘中建立
智能群助手
,之後
擷取 token
複制的 webhook 中
https://oapi.dingtalk.com/robot/send?access_token={YOUR_TOKEN}
,
{YOUR_TOKEN}
就是要填入的 token。

安全設定
釘釘智能群助手在更新後新增了安全設定,提供三種驗證方式
自定義關鍵詞
加簽
IP位址(段)
,這裡推薦使用
IP位址(段)的方式
,直接将 Kubernetes 叢集的出口 IP 填入設定即可。同時也提供了
加簽
的方式,拷貝秘鑰,将其填入
dingtalk.sign
中。
項目配置
編輯
kubewatch/value.yaml
,修改配置
## Global Docker image parameters
## Please, note that this will override the image parameters, including dependencies, configured to use the global value
## Current available global Docker image parameters: imageRegistry and imagePullSecrets
##
# global:
# imageRegistry: myRegistryName
# imagePullSecrets:
# - myRegistryKeySecretName
slack:
enabled: false
channel: ""
token: "xoxb"
hipchat:
enabled: false
# room: ""
# token: ""
# url: ""
mattermost:
enabled: false
# channel: ""
# url: ""
# username: ""
flock:
enabled: false
# url: ""
webhook:
enabled: false
# url: ""
dingtalk:
enabled: true
token: ""
sign: ""
# namespace to watch, leave it empty for watching all.
namespaceToWatch: ""
# Resources to watch
resourcesToWatch:
deployment: true
replicationcontroller: false
replicaset: false
daemonset: false
services: false
pod: true
job: false
persistentvolume: false
image:
registry: docker.io
# repository: bitnami/kubewatch
repository: guoxudongdocker/kubewatch-chart
# tag: 0.0.4-debian-9-r405
tag: latest
pullPolicy: Always
## Optionally specify an array of imagePullSecrets.
## Secrets must be manually created in the namespace.
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
##
# pullSecrets:
# - myRegistryKeySecretName
## String to partially override kubewatch.fullname template (will maintain the release name)
##
# nameOverride:
## String to fully override kubewatch.fullname template
##
# fullnameOverride:
rbac:
# If true, create & use RBAC resources
#
create: true
serviceAccount:
# Specifies whether a ServiceAccount should be created
create: true
# The name of the ServiceAccount to use.
# If not set and create is true, a name is generated using the fullname template
name:
resources: {}
# limits:
# cpu: 100m
# memory: 300Mi
# requests:
# cpu: 100m
# memory: 300Mi
# Affinity for pod assignment
# Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
# affinity: {}
# Tolerations for pod assignment
# Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
tolerations: []
# Node labels for pod assignment
# Ref: https://kubernetes.io/docs/user-guide/node-selection/
nodeSelector: {}
podAnnotations: {}
podLabels: {}
replicaCount: 1
使用
value.yaml
git clone https://github.com/sunny0826/kubewatch-chat.git
cd kubewatch-chat
helm install my-release -f kubewatch/values.yaml
Slack 配置
Slack 為 kubewatch 預設的通知軟體,這裡就不簡介 Slack 的安裝和注冊,直接從建立 APP 開始
建立一個 APP
進去建立
APP 頁面選擇
App Name
和
Development Slack Workspace
添加 Bot 使用者
添加 App 到 Workspace
擷取 Bot-token
通知效果
在 Slack 中,
建立
更新
删除
分别以綠、黃和紅色代表
在釘釘中,我進行了漢化
結語
對于 kubewatch 我們這裡主要用作監控各種 CronJob 的定時觸發狀态,已經 ConfigMap 和 Secrets 的狀态變化,同時也觀察 HPA 觸發的彈性伸縮的狀态,可以實時觀測到業務高峰的到來,是一個不錯的小工具。