LoginSignup
13
1

More than 3 years have passed since last update.

BotkubeでSlackからKubernetes管理してみよう

Posted at

この記事は Kubernetes2 Advent Calendar 2019 12日目の記事です。

はじめに

Kubernetesを運用していて、kubectlコマンドを実行することってどれくらいありますか?
複数クラスタを管理する場合、監視システムやダッシュボード的なものでステータス確認したり、デプロイもCD環境から実行するようにしてたりすると思うので、あまりないかもしれないです。
でも、デプロイ後のステータスを、エビデンス目的や、他の人への連絡のためにkubectlを実行して、結果をSlackにコピペしたりすることないですか?
また、複数クラスタ管理していて、ターミナルからkubeconfig切り替えながらkubectl実行してると、どのクラスタの作業しているのかわからくなったり・・・ってことないですか?
そんなときはBotkubeを使うといいかもしれないです。

Botkube?

Slackからkubectlの実行ができたり、イベントをSlackに通知できたり、ChatOps的なことができるようになるツールです。

image.png
https://www.botkube.io/

インストール

  • Slack

    • Slack Appのページからインストールして、Tokenを取得します。
  • Kubernetes

    • helmでインストールできます。 v3だとtillerいらないので楽ですね
helm repo add infracloudio https://infracloudio.github.io/charts
helm install --version v0.9.1 --name-template botkube --namespace botkube -f values.yaml infracloudio/botkube

インストールに成功すると指定したSlack Channelにこんなんが出ます。

image.png

Daemonsetではないので、 Fargate for EKS でも動きました。

設定

https://www.botkube.io/configuration/
https://github.com/infracloudio/botkube/blob/master/helm/botkube/values.yaml
自分の環境のvalues.yamlはこんな感じです。

values.yaml
config:
  communications:
    slack:
      enabled: true
      channel: '(channel名 #不要)'
      token: '(SlackのToken xoxb-XXX)'
      notiftype: short   

  settings:
    clustername: (cluster_name)
    allowkubectl: true
    configwatcher: true
    upgradeNotifier: false

  resources:
    - name: pod
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: service
      namespaces:
        include:  
          - all
        ignore:
          - 
      events:
        - error
    - name: deployment
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: statefulset
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: ingress
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: node
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - create
        - delete
        - error
    - name: namespace
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: persistentvolume
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: persistentvolumeclaim
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: configmap
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: daemonset
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: job
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: role
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: rolebinding
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
    - name: clusterrole
      namespaces:
        include:
          - all
        ignore:
          - 
      events:
        - error
    - name: clusterrolebinding
      namespaces:
        include:  
          - all
        ignore:
          -
      events:
        - error
        - 
resources: 
  limits:
   cpu: 100m
   memory: 128Mi
  requests:
   cpu: 100m
   memory: 128Mi

使っている設定だけ簡単に説明しておきます。

  • config.communications.slack.notiftype

    • メッセージのフォーマットです。
    • short にすると image.png
    • long にすると image.png
  • config.settings.allowkubectl

    • true にすると後述のSlack上でのkubectlの実行ができるようになります。
  • config.settings.configwatcher

    • true にするとSlack上でbotkubeの設定が確認できます。 image.png
  • config.settings.upgradeNotifier

    • true にすると、バージョンアップしたことを通知してくれます。 image.png
  • config.resources

    • 通知させたいリソース、イベントを列挙します。自分の環境では、各リソースのerrorのみ通知、Nodeに関してはAutoScaleを実行しているため、create、delete、errorすべてを通知するようにしています。
    • Fargate for EKS の場合、PodごとにNodeが起動するので、 Nodeに関する項目は削除して通知させないほうがいいかと思います。

使い方

/botkubehelp と入力するとHelpが出力されます。

BotKube Help

Usage:
    @BotKube <kubectl command without kubectl prefix> [--cluster-name <cluster_name>]
    @BotKube notifier [stop|start|status|showconfig]
    @BotKube ping [--cluster-name <cluster-name>]
    @BotKube filters [list|enable|disable] [filter-name]

Description:

Kubectl commands:
    - Executes kubectl commands on k8s cluster and returns output.

    Example:
        @BotKube get pods
        @BotKube logs podname -n namespace
        @BotKube get deployment --cluster-name cluster_name

    Allowed kubectl commands:
        get, top, cluster-info, describe, explain, logs, version, auth, api-resources, api-versions, diff

Cluster Status:
    - List all available Kubernetes Clusters and check connection health. 
    - If flag specified, gives response from the specified cluster.

    Example:
        @BotKube ping
        @BotKube ping --cluster-name mycluster

Notifier commands:
    - Commands to manage incoming notifications (Runs only on configured channel).

    Example:
        @BotKube notifier stop          Stop sending k8s event notifications to Slack
        @BotKube notifier start         Start sending k8s event notifications to Slack
        @BotKube notifier status        Show running status of event notifier
        @BotKube notifier showconfig    Show BotKube configuration for event notifier

Filters commands:
    - Command to manage filters run on K8s events (Runs only on configured channel).

        Example:
        @BotKube filters list                     Show list of available filters
        @BotKube filters disable ImageTagChecker  Disable filter
        @BotKube filters enable ImageTagChecker   Enable filter

Options:
    --cluster-name                  Get cluster specific response

kubectlの実行

@Botkube のあとにコマンドを入力すると結果が出力されます。

  • kubectl get

image.png
image.png

Namespaceの指定もできます。
image.png

  • kubectl describe
    image.png

  • kubectl logs
    image.png

サイズが大きいときはテキストファイルとしてダウンロードされます。
image.png

Helpにあるとおり、実行できるのは下記の参照系コマンドのみです。

    Allowed kubectl commands:
        get, top, cluster-info, describe, explain, logs, version, auth, api-resources, api-versions, diff

これ以外を実行するとエラーになります。

image.png

このへんはRBACの設定変えても実行できないようになっているみたいです。

入力がめんどくさい?

慣れてくると @Botkube とか入力するのもめんどうになってきます。
そんな場合は、SlackのWorkflowでメニューから呼び出したり、emojiリアクションで実行できるようにしとくといいかもしれないです。

image.png

image.png

image.png

image.png

自分の環境ではCDにArgoCDを使っていて、Syncの開始/終了をHookでSlackに投げるようにしているので、その通知にリアクションすると状態が確認できるようにしています。
image.png

まとめ

Botkubeを使えばちょっとした確認をしたいときに、簡単にkubectlを実行できるようになります。
また、SlackのChannelとクラスタを紐付けておけば、ターミナル上での環境切り替えもいらなくなるし、間違いも起こりにくくなるのではないかと思います。
通知については、Namespaceや、create/delete/error等おおまかな設定しかできないので、アクションが必要な障害の検知などは監視システムで行うべきだと思いますが、なにかが起きたっていうのが分かるレベルでいいのであれば、十分利用できるのではないかと思います。

13
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
1