Reputation: 6140
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?
Upvotes: 1
Views: 2209
Reputation: 84
There's a way to rerun the job with the following command:
kubectl get job <job name> -n <namespace> -o json | jq 'del(.spec.selector)' | jq 'del(.spec.template.metadata.labels)'| kubectl replace --force -f -
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.
containers:
- name: <container name>
command:
- /bin/sh
- -c
- |
<first command>;
<second command>;
Upvotes: 0
Reputation: 2072
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.
Upvotes: 1