DEV Community

Cover image for Notify kubernetes events with botkube
Ashok Nagaraj
Ashok Nagaraj

Posted on

Notify kubernetes events with botkube

At Cisco we use Webex as our IM collaboration engine. It is same as Teams/Slack with communication capabilities. One can build a bot for interaction and use APIs to communicate with webex teams.
As a platform team we want to notify interested parties when events happen like pod creation, deployment rollout, pod errors ...
When exploring around I came across botkube which can help solve our problem to a major extent

Architecture

Image description
Source: https://www.botkube.io/architecture/

Modes of usage
  • Full fledged chatbot - integrations to Slack, Mattermost and MS Teams
  • Sink - to push events, used for datastores like Elasticsearch and webhooks for use-cases
  • Custom filters - for validations and recommendations

Install botkube
  1. Prepare a webhook listener (<WEBHOOK_URL>)
  2. Decide on a name for your cluster (any string: <CLUSTER_NAME>)
  3. Get and update configuration values
$ wget https://raw.githubusercontent.com/infracloudio/botkube/v0.12.4/helm/botkube/sample-res-config.yaml
$ mv sample-res-config.yaml /path/to/config.yaml
$ vi /path/to/config.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Install
$ helm repo add infracloudio https://infracloudio.github.io/charts
$ helm repo update
$ helm install --version v0.12.4 botkube --namespace botkube \
--set communications.webhook.enabled=true \
--set communications.webhook.url=<WEBHOOK_URL> \
--set config.settings.clustername=<CLUSTER_NAME> \
--set image.repository=infracloudio/botkube \
--set image.tag=v0.12.4 \
-f /path/to/config.yaml
infracloudio/botkube
Enter fullscreen mode Exit fullscreen mode
  1. Check the installation:
$ kubectl get-all -n botkube
NAME                                  NAMESPACE  AGE
configmap/botkube-configmap           botkube    6d22h
configmap/kube-root-ca.crt            botkube    6d22h
pod/botkube-785d6c889-srbzg           botkube    6d22h
secret/botkube-communication-secret   botkube    6d22h
secret/botkube-sa-token-btjvj         botkube    6d22h
secret/default-token-d5s9n            botkube    6d22h
secret/sh.helm.release.v1.botkube.v1  botkube    6d22h
serviceaccount/botkube-sa             botkube    6d22h
serviceaccount/default                botkube    6d22h
deployment.apps/botkube               botkube    6d22h
replicaset.apps/botkube-785d6c889     botkube    6d22h
Enter fullscreen mode Exit fullscreen mode

get-all is a krew plugin


Usage

Now when identified events are triggered, webhook url is POSTed with an appropriate json payload

$ kubectl logs -n botkube pod/botkube-785d6c889-srbzg
...
INFO[2022-04-09T13:27:26Z] Registering resource lifecycle informer
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/deployments
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/statefulsets
INFO[2022-04-09T13:27:26Z] Adding informer for resource:apps/v1/daemonsets
INFO[2022-04-09T13:27:26Z] Adding informer for resource:v1/nodes
INFO[2022-04-09T13:27:26Z] Registering kubernetes events informer for types: warning
INFO[2022-04-09T13:27:26Z] Registering kubernetes events informer for types: normal
INFO[2022-04-09T13:27:26Z] Registering watcher on configfile /config/resource_config.yaml
Enter fullscreen mode Exit fullscreen mode

Payloads will be like:

{
    "meta": {
        "cluster": "macbook-kind",
        "kind": "Deployment",
        "name": "test-dep4",
        "namespace": "default"
    },
    "status": {
        "level": "info",
        "type": "create"
    },
    "summary": "Deployment *default/test-dep4* has been created in *macbook-kind* cluster\n",
    "timestamp": "2022-04-12T00:38:15Z"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

botkube is capable of a lot more including building a full-fledged chatbot for managing kubernetes cluster and provides extension points as well. Do check-out the documentation

There is another similar project called kubewatch, but not active anymore

Top comments (0)