skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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>;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search