kubernetes cleanup of pods,service,deployment etc

7/12/2019

To setup the kubernetes, I started with creating namespace, deployment, service. To clean the resources, do I need to follow any order like remove the service first then pods and then deployment and finally namespace? how to clean the resources in a proper way? Because I deleted pods and service, but I could see the pods,services running again. Its deploying the resources again, so this question came up here for experts answers.

-- arunp
kubernetes
kubernetes-pod

4 Answers

7/12/2019

I deleted the resources in the below order and it worked for me.

To get all the resources.

kubectl get pods,services,deployments,jobs,daemonset

Delete the resources like below:

kubectl delete deployments <deployment>
kubectl delete services <services>
kubectl delete pods <pods>
kubectl delete daemonset <daemonset>
-- arunp
Source: StackOverflow

7/13/2019

Just in case you are running them in default namespace and there are many of them ,and you don't want to spend time on deleting them one by one:

kubectl delete deployments --all
kubectl delete services --all
kubectl delete pods --all
kubectl delete daemonset --all
-- Ijaz Ahmad Khan
Source: StackOverflow

11/12/2019

I follow a different method to clean up resources allocated for an application.

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-75b7bfdb6b-k76tl   1/1     Running   0          9d


$ kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           9d

So now instead of deleting the objects/resources individually for an application, use delete all which removes all the resources associated with the application.

This way Kubernetes cleanup the resources rather than we decide the order of cleanup.

$ kubectl delete all -l app=nginx
pod "nginx-75b7bfdb6b-k76tl" deleted
service "nginx-service" deleted
deployment.apps "nginx" deleted
replicaset.apps "nginx-75b7bfdb6b" deleted

Hope this helps!

-- Here_2_learn
Source: StackOverflow

7/13/2019

If you just delete the namespace, it will delete everything in it, you don’t need to manually clean up everything in it. I did a web search for “Kubernetes delete namespace” and found this in the official Kubernetes docs: https://kubernetes.io/docs/tasks/administer-cluster/namespaces/#deleting-a-namespace

Regarding your question about pods coming back: when you create a deployment, it creates pods. When you delete the deployment, it will automatically delete pods it created. If you manually delete the pods that the deployment automatically created, it will bring them back because the desired number of replicas as specified in your deployment is still a positive number.

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. If you want to delete the namespace as well, just delete the namespace and it will automatically also delete everything in the namespace for you, you don’t need to bother also individually deleting those things.

-- Amit Kumar Gupta
Source: StackOverflow