Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 898
Larry Cai Avatar asked Jul 24 '17 08:07

Larry Cai


People also ask

How do I delete specific pods in Kubernetes?

Destroy Pod The action of deleting the pod is simple. To delete the pod you have created, just run kubectl delete pod nginx . Be sure to confirm the name of the pod you want to delete before pressing Enter. If you have completed the task of deleting the pod successfully, pod nginx deleted will appear in the terminal.

How do you clean up Kubernetes pods?

If you create pods directly (not via a deployment), you can delete them directly, and they will stay deleted. Pods (that were created directly), deployments, and services can all be deleted independently of one another, order doesn't matter. If you want to delete them but not the namespace, delete them in any order.

How do I stop my Kubernetes pod from running?

Simply run “kubectl delete pod Nginx” to remove the pod you have made. The pod will be deleted as you can see in the below image upon the execution of the following command in the shell. If you have successfully destroyed the pod, the command “pod Nginx deleted” will be shown in the console.

Will deleting a pod restart it?

If you manually deploy a single pod and then delete it, your service will go down and won't come back up. If a service is running through a replica set but with only one pod, the service will become unavailable after deleting the pod.


1 Answers

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.

like image 55
Janos Lenart Avatar answered Oct 20 '22 01:10

Janos Lenart