如何自动删除kubernetes中一次性任务的pod?

Lar*_*Cai 5 busybox kubernetes minikube

为了检查状态,我busybox使用交互式shell 启动了kubernetes.

$ 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
Run Code Online (Sandbox Code Playgroud)

当我从shell退出时,我希望pod也会被删除.虽然它存在于完成状态.

$ kubectl get pods -a
NAME                     READY     STATUS      RESTARTS   AGE
busybox                  0/1       Completed   0          58m
Run Code Online (Sandbox Code Playgroud)

我必须删除pod,这很烦人.

我们是否可以使用简单的参数来请求k8s删除这个任务作业的pod?

Jan*_*art 7

只需添加--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
Run Code Online (Sandbox Code Playgroud)

--rm=false:如果为true,则删除在此命令中为附加容器创建的资源.