21

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
  • 55,923
  • 34
  • 110
  • 156

1 Answers1

32

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
  • 25,074
  • 5
  • 73
  • 75
  • simple, I just can't find in the doc. Nice !! – Larry Cai Jul 24 '17 at 20:10
  • Doesn't work for me with `kubectl` 1.22.1. But I found a workaround by using `KUBECTL_COMMAND_HEADERS=false`: https://github.com/kubernetes/kubectl/issues/1098#issuecomment-929743957. – David Ongaro Jan 28 '22 at 08:40