I’d like to run a particular Kubernetes job whenever the pod of particular deployment restarts.
In particular, I have a Redis deployment. It is not backed by permanent storage. When the pod in the Redis deployment restarts, I’d like to populate some keys in Redis.
Is there a way to trigger a job on pod restart?
2
Answers
The best option comes to my mind is an k8s operator – A simple python/go script watches your target pod (by label, name, namespace, etc.) and performs some actions when the state changes.
An operator is just a deployment with special features. There are various ways to implement, one of them is https://sdk.operatorframework.io/docs/building-operators/golang/quickstart/
You can also use https://github.com/kubernetes-client/python#examples (check the second example).
You can get rid of the job and write your redis logic inside the operator itself.
There’s a way to rerun the job with the following command:
This works however it’s not totally automatic.
The solution which was more effective was to copy the commands in the job ,which were relevant for a single pod, and put in the deployment of the pod. Then whenever it restarts these commands are run.