小工具介绍: kube watch

用于观测 Kubernetes 资源情况,并实时通知到各种协作软件/聊天软件

作者 guoxudong 发表于 2019年12月4日 更新于 2023年1月10日

前言

这次要介绍一个 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 页面

image

选择 App NameDevelopment Slack Workspace

添加 Bot 用户

image

添加 App 到 Workspace

image

获取 Bot-token

image

通知效果

在 Slack 中,创建` 更新 删除` 分别以绿、黄和红色代表

image

在钉钉中,我进行了汉化

image

image

结语

对于 kubewatch 我们这里主要用作监控各种 CronJob 的定时触发状态,已经 ConfigMap 和 Secrets 的状态变化,同时也观察 HPA 触发的弹性伸缩的状态,可以实时观测到业务高峰的到来,是一个不错的小工具。