我正在尝试删除/移除在我的环境中运行的所有 pod。当我发出
docker ps
我得到以下输出。这是示例屏幕截图。可以看到都是K8s。我想删除所有的 pod /删除它们。
我尝试了以下所有方法,但它们一次又一次地出现
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
除上述之外,我还尝试将 docker stop
和 docker rm
与容器 ID 一起使用,但它们要么重生。我想清除所有这些。我想从头开始
我已注销并重新登录多次,但我仍然看到这些项目。
你能帮我删除所有 pod 吗?我希望“docker ps”的输出没有任何与 kubernetes 相关的项目,如上所示?
最佳答案
sudo kubectl get deployments # returns no resources found
Deployments
不是 Kubernetes 中唯一可以管理您的 Pod 的 Controller 。还有很多:StatefulSet
, ReplicaSet
等(查看Controllers doc了解详情)
简而言之,Controller 负责确保它管理的所有 Pod 都在运行,并在必要时创建它们 - 当您删除所有 Pod 时,关联的 Controller 将意识到它们丢失并简单地重新创建它们以确保它匹配它的规范。
如果要有效删除所有Pod,应该删除所有相关的Controller(或者更新它们,将它们的replica设置为0),例如:
# 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
编辑:如 P Emkambaram 所述答案是您还可以使用 kubectl delete namespace mynamespace
删除整个命名空间(当然不要删除 kube-system
),但它也可能会删除命名空间中的其他组件,例如服务
重要提示:
- 在
kube-system
命名空间中删除与集群本身的内部管道相关的 Pod 或对象时,您还应该小心。 - 您不应该通过使用
docker
命令删除其底层容器来直接删除 Kubernetes 组件,这可能会产生意想不到的效果,请改用kubectl
。
关于docker - 无法删除 Kubernetes 中的所有 pod - 清除/重启 Kubernetes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867829/