简体   繁体   English

列表为空时如何使redis pod自动删除

[英]How to make redis pod auto-delete when list is empty

I want to deploy redis pod which loads a list.我想部署加载列表的 redis pod。 Then I will have kubernetes job which will execute bash script with variable taken from that list in redis.然后我将拥有 kubernetes 作业,该作业将使用从 redis 中的列表中获取的变量执行 bash 脚本。

How can I make this redis pod to be auto deleted when all items from a list are used?当使用列表中的所有项目时,如何使这个 redis pod 自动删除?

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.默认情况下,Kubernetes 会保留已完成的作业和关联对象以用于调试目的,删除时您将丢失它们生成的所有日志。

That being said, a job can be automatically deleted by using the TTL mechanism for finished Jobs .话虽如此,可以使用已完成 Jobs 的 TTL 机制自动删除作业。

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:在这里,您可以找到启用 TTL 并设置为在完成 100 秒后删除作业和相关对象(pod、服务等)的作业清单示例:

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

I wrote a bash script which is checking a list, if it is empty it kills a redis-server.我写了一个检查列表的 bash 脚本,如果它是空的,它会杀死一个 redis 服务器。 That means pod is finished and with ttlSecondsAfterFinished job will be deleted这意味着 pod 已完成,并且 ttlSecondsAfterFinished 作业将被删除

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM