以對話的形式管理你的Kubernetes叢集

空殼先生發表於2021-07-12

BotKube

BotKube 是一個用於監控和除錯 Kubernetes 叢集的訊息傳遞工具。 BotKube 可以與多個訊息傳遞平臺(如 Slack、Mattermost 或 Microsoft Teams)整合,以幫助您監控 Kubernetes 叢集、除錯關鍵部署,並通過對 Kubernetes 資源執行檢查來提供標準實踐建議。

安裝BotKube

1、本文使用slack作為訊息接收平臺,需要自行註冊slack,拿到Access token

xoxb-2169032185141-2256603089394-qeLWxe0sUtwfdlwnk6VWbw11

2、將 BotKube 使用者新增到 Slack 頻道
將 BotKube 應用程式安裝到您的 Slack 工作區後,您會看到一個名為“BotKube”的新機器人使用者新增到您的工作區中。 將該機器人新增到您想要接收通知的 Slack 頻道。
(您可以通過在頻道中邀請@BotKube 來新增它)

3、使用helm把BotKube安裝到kubernrtes叢集

helm repo add infracloudio https://infracloudio.github.io/charts
helm repo update

請自行替換<>內的資訊:
helm install --version v0.12.1 botkube --namespace botkube \
  --set communications.slack.enabled=true \
  --set communications.slack.channel=<SLACK_CHANNEL_NAME> \
  --set communications.slack.token=<SLACK_API_TOKEN_FOR_THE_BOT> \
  --set config.settings.clustername=<CLUSTER_NAME> \
  --set config.settings.kubectl.enabled=<ALLOW_KUBECTL> \
  --set image.repository=infracloudio/botkube \
  --set image.tag=v0.12.1 \
  infracloudio/botkube


- SLACK_CHANNEL_NAME 是新增@BotKube 的頻道名稱
- SLACK_API_TOKEN_FOR_THE_BOT 是將 BotKube 應用程式安裝到 Slack 工作區後收到的令牌
- CLUSTER_NAME 是在傳入訊息中設定的叢集名稱
- ALLOW_KUBECTL 設定為 true 以允許 BotKube 在叢集上執行 kubectl 命令

4、檢視安裝資訊

# helm list -n botkube
NAME   	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART          	APP VERSION
botkube	botkube  	1       	2021-07-09 10:41:35.813245746 +0800 CST	deployed	botkube-v0.12.1	v0.12.1
# kubectl get all -n botkube
NAME                           READY   STATUS    RESTARTS   AGE
pod/botkube-747ff4dc5d-795hz   1/1     Running   0          2d22h

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/botkube   1/1     1            1           2d22h

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/botkube-747ff4dc5d   1         1         1       2d22h

5、如果你需要修改配置,可以更新botkube-configmap,預設設定如下

recommendations: true
resources:
- events:
  - create
  - delete
  - error
  name: v1/pods
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/services
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - update
  - delete
  - error
  name: apps/v1/deployments
  namespaces:
    ignore:
    - null
    include:
    - all
  updateSetting:
    fields:
    - spec.template.spec.containers[*].image
    - status.availableReplicas
    includeDiff: true
- events:
  - create
  - update
  - delete
  - error
  name: apps/v1/statefulsets
  namespaces:
    ignore:
    - null
    include:
    - all
  updateSetting:
    fields:
    - spec.template.spec.containers[*].image
    - status.readyReplicas
    includeDiff: true
- events:
  - create
  - delete
  - error
  name: networking.k8s.io/v1beta1/ingresses
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/nodes
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/namespaces
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/persistentvolumes
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/persistentvolumeclaims
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: v1/configmaps
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - update
  - delete
  - error
  name: apps/v1/daemonsets
  namespaces:
    ignore:
    - null
    include:
    - all
  updateSetting:
    fields:
    - spec.template.spec.containers[*].image
    - status.numberReady
    includeDiff: true
- events:
  - create
  - update
  - delete
  - error
  name: batch/v1/jobs
  namespaces:
    ignore:
    - null
    include:
    - all
  updateSetting:
    fields:
    - spec.template.spec.containers[*].image
    - status.conditions[*].type
    includeDiff: true
- events:
  - create
  - delete
  - error
  name: rbac.authorization.k8s.io/v1/roles
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: rbac.authorization.k8s.io/v1/rolebindings
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: rbac.authorization.k8s.io/v1/clusterrolebindings
  namespaces:
    ignore:
    - null
    include:
    - all
- events:
  - create
  - delete
  - error
  name: rbac.authorization.k8s.io/v1/clusterroles
  namespaces:
    ignore:
    - null
    include:
    - all
settings:
  clustername: k8s-2
  configwatcher: true
  kubectl:
    commands:
      resources:
      - deployments
      - pods
      - namespaces
      - daemonsets
      - statefulsets
      - storageclasses
      - nodes
      verbs:
      - api-resources
      - api-versions
      - cluster-info
      - describe
      - diff
      - explain
      - get
      - logs
      - top
      - auth
      - describe
    defaultNamespace:
    - default
    - pro
    - qa
    enabled: true
    restrictAccess: false
  upgradeNotifier: true
ssl:
  enabled: false

使用Slack與BotKube對話

1、先ping下BotKube,看看輸出

2、檢視可使用的命令

3、以對話的形式執行kubectl命令,例如獲取pod,更多命令可以自己測試...

4、驗證slack收集kubernetes事件資訊,例如建立一個新的pod,這裡以nginx為例
我這裡直接用lens連線叢集,建立一個名為nginx的deployment,

直接建立即可,然後來到slack可以看到如下資訊,即botkube將這個create事件訊息推送到了你的slack頻道。

現在刪除deployment,再看slack頻道,收到delete的訊息

更多好玩的功能,請自行體會吧。。。

相關文章