Sitemap
DevOps Mate

DevOps Mate

Kubetctl Slack Bot

2 min readSep 5, 2022

--

Hope everyone who knows about Kubernetes cluster should know kubectl which is the CLI used to interact with Kubernetes cluster. It is an awesome tool and can help everyone to work effectively with the cluster. But getting the kube context and authenticating to the cluster might be a challenging if you just need to check something quickly when you dont have access to your laptop. It will be even more challenging when the cluster is in inernal network which can be only accessible over VPN along with other factors like the switching the context, need of a terminal window etc.

In our organisation, we faced this challenge and to overcome this, I have created a simple python flask rest api which will run as an app inside the cluster and will interact with it to execute the kubectl commands when access over slack.This will help to check something quick on the phone when you are on 24/7 support and no need to carry your laptop all the time with you.

Add the python app as Slack bot and make it listen to slack commands. When a valid slack command is invoked, it will execute the request and send the response back to slack. App flow details are given below,

Press enter or click to view image in full size

Full code base available here.

Get DevOps Mate’s stories in your inbox

Join Medium for free to get updates from this writer.

App requires below environment variables inorder to run,

  • CLUSTER_NAME ~ Name of the cluster to run kubectl command against
  • ROLE_NAME ~ Role name use to run the kubectl. Role should have access to run kubectl
  • TOKEN ~ Slack bot token to validate the request are from slack
  • CHANNEL ~ Channel name from which to execute the slash command
  • COMMAND ~ Name of slack slash command

Slack response will be something like below,

Press enter or click to view image in full size

The app is made to perform only read operation like GET, DESCRIBE, VERSION and not to configure to perform write operation. The restriction is at the app level but it can be easily managed using RBAC of kubernetes. It can be extended to peform all the operations but it needs thorough testing and to ensure it shouldn’t impact your cluster.

It is build primarily for EKS cluster with the IAM role should have access to EKS cluster to run kubectl commands.But can also be extended to other cloud provider or any EKS cluster to create the kube config file which should have access to the cluster to run kubectl commands.

Hope you like this simplified bot. Please share your feedbacks and comments. Reach to me for any queries.

--

--