Is there a way to delete pods automatically through YAML after they have status 'Completed'?

6/4/2019

I have a YAML file which creates a pod on execution. This pod extracts data from one of our internal systems and uploads to GCP. It takes around 12 mins to do so after which the status of the pod changes to 'Completed', however I would like to delete this pod once it has completed.

apiVersion: v1
kind: Pod
metadata:
  name: xyz
spec:
  restartPolicy: Never
  volumes:
  - name: mount-dir
    hostPath:
      path: /data_in/datos/abc/
  initContainers:
   - name: abc-ext2k8s
     image: registrysecaas.azurecr.io/secaas/oracle-client11c:11.2.0.4-latest
     volumeMounts:
     - mountPath: /media
       name: mount-dir
     command: ["/bin/sh","-c"] 
     args: ["sqlplus -s CLOUDERA/MYY4nGJKsf@hal5:1531/dbmk @/media/ext_hal5_lk_org_localfisico.sql"]
  imagePullSecrets:
   - name: regcred

Is there a way to acheive this?

-- Sakshi Mittal
kubernetes
yaml

1 Answer

6/4/2019

Typically you don't want to create bare Kubernetes pods. The pattern you're describing of running some moderate-length task in a pod, and then having it exit, matches a Job. (Among other properties, a job will reschedule a pod if the node it's on fails.)

Just switching this to a Job doesn't directly address your question, though. The documentation notes:

When a Job completes, no more Pods are created, but the Pods are not deleted either. Keeping them around allows you to still view the logs of completed pods to check for errors, warnings, or other diagnostic output. The job object also remains after it is completed so that you can view its status. It is up to the user to delete old jobs after noting their status.

So whatever task creates the pod (or job) needs to monitor it for completion, and then delete the pod (or job). (Consider using the watch API or equivalently the kubectl get -w option to see when the created objects change state.) There's no way to directly specify this in the YAML file since there is a specific intent that you can get useful information from a completed pod.

If this is actually a nightly task that you want to run at midnight or some such, you do have one more option. A CronJob will run a job on some schedule, which in turn runs a single pod. The important relevant detail here is that CronJobs have an explicit control for how many completed Jobs they keep. So if a CronJob matches your pattern, you can set successfulJobsHistoryLimit: 0 in the CronJob spec, and created jobs and their matching pods will be deleted immediately.

-- David Maze
Source: StackOverflow