'How to make redis pod auto-delete when list is empty

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?



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriel Robledo Ahumada
Solution 2 a11eksandar