Triggering shell commands or scripts on kubernetes events
November 25, 2021 at 09:02
How do you trigger a shell script or an ansible command when a critical pod is deleted from your kubernetes cluster?
When I was researching on how I can do something like this, I started searching for “best kubernetes event handlers”. If you do a google search for this, probably the top hits you get will be the Container Lifecycle Events and Hooks.
But my requirement was not that complex, definitely someone else must have already figured this out by now.
This led me to https://github.com/flant/shell-operator. This operator framework is quite simple, to the point and their documentation is spot on. You can easily identify what you need and whether this suits your requirements and is recognized by kubernetes.io for extending the Kubernetes functionality. You can read more about it at https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
But this post is just a simple post(to be honest, my first technical post) to give you an example on how to leverage this operator pattern to make your life easier.
First lets define the problem statement — “I want to run the ansible — version command when a Deployment with the name varghese in the ns-shell-operator namespace is deleted”