How to delete the pod for one time task in kubernetes automatically?

7/24/2017

In order to check status, I started the busybox in kubernetes using interactive shell.

$ kubectl run -i --tty busybox --image=busybox --restart=Never -- sh
/ # exit
$ kubectl run -i --tty busybox --image=busybox --restart=Never -- sh
Error from server (AlreadyExists): pods "busybox" already exists

When I exit from the shell, I expect the pod will be deleted as well. While it exists there in completed status.

$ kubectl get pods -a
NAME                     READY     STATUS      RESTARTS   AGE
busybox                  0/1       Completed   0          58m

I have to delete the pod, it is annoying.

DO we have simple parameter I can use to ask k8s to delete the pod for this one task job ?

-- Larry Cai
busybox
kubernetes
minikube

1 Answer

7/24/2017

Just add --rm:

$ kubectl run busybox -i --tty --image=busybox --restart=Never --rm -- sh
If you don't see a command prompt, try pressing enter.
/ # exit
$ kubectl get pod busybox
Error from server (NotFound): pods "busybox" not found

--rm=false: If true, delete resources created in this command for attached containers.

-- Janos Lenart
Source: StackOverflow