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

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

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

$ 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

当我退出 shell 时,我预计 pod 也会被删除。虽然它以完成状态存在。

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

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

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

只需添加--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.