0

I am running thousands of job in rancher using rancher CLI. I want to delete the job as soon as the task is finished. THe problem is When the job finishes the job remain "Active" there.

I tried suggestion from How to automatically remove completed Kubernetes Jobs created by a CronJob? but its not working for me. As i can see the underlying docker container gets into "Terminated" state but the deployed job remains active.

Right now i have to synchronously wait for job to finish and then i am firing "kubectl delete" command to delete job. But i want to do that asynchronously(delete all job that are "done").

Any idea?

Update

Here is the yaml file for my job

apiVersion: batch/v1
kind: Job
metadata:
  name: runtest
  namespace: default
spec:
  ttlSecondsAfterFinished: 60
  template:
    metadata:
      labels:
        job-name: runtest
    spec:
      restartPolicy: Never
      containers:
      - args:
        - sh
        - /code/rancher_test/run_9.sh
        image: x11vnc/docker-desktop
        name: runtest
        stdin: true
        tty: true
        securityContext:
          runAsUser: 53197
        volumeMounts:
        - mountPath: /code
          name: code
      volumes:
      - hostPath:
          path: /code
          type: ""
        name: code
Krishnom
  • 1,348
  • 12
  • 39

1 Answers1

2

You can enable TTL Controller (for k8s 1.12 and above), if it's not enabled already and, add ttlSecondsAfterFinished: N parameter, in which case the pods, that created the job, and the job itself will be deleted after N seconds.

For example:

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-ttl
spec:
  ttlSecondsAfterFinished: 0
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

This typical kubernetes pi job will clean itself up immediatly after getting to completed.

suren
  • 7,817
  • 1
  • 30
  • 51
  • I have attached the yaml file in my question. I am actually using the "ttlSecondsAfterFinished" property but its not working. – Krishnom Dec 01 '19 at 08:54
  • then you don't have TTL Controller enabled. IS it pure k8s or a cloud provider one? For example on GKE you can't as you don't have access to the master. – suren Dec 01 '19 at 09:17
  • Thanks for the hint. I can see that this is not enabled in featureGates in rancher. Can you tell How can i enable TTL controller in kube config? – Krishnom Dec 06 '19 at 10:26