Unable to delete all pods in Kubernetes - Clear/restart Kubernetes

試著忘記壹切 提交于 2020-07-22 03:18:20

问题


I am trying to delete/remove all the pods running in my environment. When I issue

docker ps

I get the below output. This is a sample screenshot. As you can see that they are all K8s. I would like to delete all of the pods/remove them.

I tried all the below approaches but they keep appearing again and again

sudo kubectl delete --all pods --namespace=default/kube-public #returns "no resources found" for both default and kube-public namespaces
sudo kubectl delete --all pods --namespace=kube-system # shows "pod xxx deleted"

sudo kubectl get deployments # returns no resources found

Apart from the above, I also tried using docker stop and docker rm with the container ids but they either respawn. I want to clear all of them. I would like to start from the beginning

I have logged out and logged back in multiple times but still I see those items.

Can you help me to delete all the pods and I expect the output of "docker ps" to not have any kubernetes related items like shown above?


回答1:


sudo kubectl get deployments # returns no resources found

Deployments are not the only Controllers in Kubernetes which may manage your Pods. There are many: StatefulSet, ReplicaSet, etc. (check the Controllers doc for details)

In short, a Controller is responsible of ensuring all Pods it manages are running, and create them if necessary - when you delete all Pods, the associated Controller will realise they are missing and simply re-create them to ensure it matchs its specifications.

If you want to effectively delete all Pods, you should delete all the related Controllers (or update them to set their replica to 0), such as:

# do NOT run this in the kube-system namespace, it may corrupt your cluster
# You can also specify --namespace xxx to delete in a specific namespace
kubectl delete deployment --all # configures ReplicaSets, deleting Deployments should delete ReplicaSet as well as associated Pods
kubectl delete statefulset --all
kubectl delete daemonset --all 
kubectl delete job --all
kubectl delete cronjob --all 
kubectl delete replicationcontroller --all # their should not be any ReplicationController as Deployment should be used
# Then delete what you find

EDIT: as mentionned by P Emkambaram answer's you can also delete the entire namespace with kubectl delete namespace mynamespace (don't delete kube-system of course) but it may also delete other components in the namespace such as Services


Important notes:

  • You should also take care when deleting Pods or object in the kube-system namespace which are related to the internal plumbing of the cluster itself.
  • You should not delete directly Kubernetes components by deleting their underlying containers with docker commands, this may have unexpected effects, use kubectl instead.



回答2:


you should not delete pods from kube-system namespace. all core kubernetes pods run in that namespace. if you mess up those pods then your cluster might not work.

instead delete the namespaces where you run your application workloads. that would clean up all resources from the respective namespace.



来源:https://stackoverflow.com/questions/56867829/unable-to-delete-all-pods-in-kubernetes-clear-restart-kubernetes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!