cronjob can't delete failed pod

10/21/2021

I have a cronjob which run once every midnight, however one day I deployed wrong version of it and consequently the pod it made went failed soon.

So.. the problem is when I delete the failed pod the cronjob immediately recreates it. How can I stop it? Well anyhow its image is already broken so however it recreates new pod again and again it'd go fail.

my question is "How can I delete failed pod created by cronjob?"

P.S. I'm using rancher but I'm not sure my problem is related to it.

-- 0xF4D3C0D3
devops
kubernetes
kubernetes-cronjob
kubernetes-pod
rancher

1 Answer

10/21/2021

my question is "How can I delete failed pod created by cronjob?"

You can use ttlSecondsAfterFinished to control how long you want to keep either Complete or Failed job in your cluster.

apiVersion: batch/v1
kind: CronJob
metadata:
  ...
spec:
  schedule: ...
  ...
  jobTemplate:
    spec:
      ttlSecondsAfterFinished: 43200 <-- example 12 hrs. 0 to clean immediately.
      template:
      ...

Another thing to note this is a K8s v1.21 beta feature at the point of this writing.

-- gohm&#39;c
Source: StackOverflow