skip to Main Content

I want to deploy redis pod which loads a list. Then I will have kubernetes job which will execute bash script with variable taken from that list in redis.

How can I make this redis pod to be auto deleted when all items from a list are used?

2

Answers


  1. Chosen as BEST ANSWER

    I wrote a bash script which is checking a list, if it is empty it kills a redis-server. That means pod is finished and with ttlSecondsAfterFinished job will be deleted

    #!/bin/sh
    
    list=$(/usr/local/bin/redis-cli -h $HOSTNAME lrange dealer_codes -1 0)
    if [ $? -eq 0 ]
    then
      /usr/local/bin/redis-cli -h $HOSTNAME shutdown
    fi
    

  2. By default, Kubernetes keeps the completed jobs and associated objects for debugging purposes, and you will lose all the generated logs by them when deleted.

    That being said, a job can be automatically deleted by using the TTL mechanism for finished Jobs.

    Here you can find an example of a job’s manifest with the TTL enabled and set to delete the job and associated objects (pods, services, etc.) 100 sec after its completion:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi-with-ttl
    spec:
      ttlSecondsAfterFinished: 100
      template:
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search