从 kubernetes 节点中删除所有内容

Delete all the contents from a kubernetes node

如何删除一个kubernetes节点的所有内容?
内容包括deployments、replica sets等,我试过单独删除deplyoments。但是 kubernetes 再次重新创建所有 pods。
有什么方法可以删除节点中存在的所有副本集吗?

Kubenertes 提供命名空间对象用于隔离和关注点分离。因此,建议在自定义命名空间中应用所有 k8s 资源对象(Deployment、ReplicaSet、Pods、Services 和其他)。

现在如果你想删除所有相关和相关的k8s资源,你只需要删除将删除所有这些资源的命名空间。

kubectl create namespace custom-namespace
kubectl create -f deployment.yaml --namespace=custom-namespace
kubectl delete namespaces custom-namespace

我已附上 link 以供进一步研究。

Namespaces

如果您要测试东西,最简单的方法是

kubectl delete deployment --all

尽管如果您使用的是 minikube,最简单的方法可能是删除机器并使用新节点重新启动

minikube delete
minikube start

如果我们谈论的是生产集群,Kubernetes has a built-in feature to drain a node of the cluster,请安全地从该节点删除所有对象。

You can use kubectl drain to safely evict all of your pods from a node before you perform maintenance on the node. Safe evictions allow the pod’s containers to gracefully terminate and will respect the PodDisruptionBudgets you have specified.

Note: By default kubectl drain will ignore certain system pods on the node that cannot be killed; see the kubectl drain documentation for more details.

When kubectl drain returns successfully, that indicates that all of the pods (except the ones excluded as described in the previous paragraph) have been safely evicted (respecting the desired graceful termination period, and without violating any application-level disruption SLOs). It is then safe to bring down the node by powering down its physical machine or, if running on a cloud platform, deleting its virtual machine.

首先,确定要排空的节点的名称。您可以使用

列出集群中的所有节点
kubectl get nodes

接下来,告诉 Kubernetes 清空节点:

kubectl drain <node name>

一旦它returns(没有给出错误),你可以关闭节点(或者等价地,如果在云平台上,删除支持节点的虚拟机)。 drain 等待正常终止。在命令完成之前,您不应该在机器上操作。

如果在维护操作期间将节点留在集群中,则需要运行

kubectl uncordon <node name>

然后告诉 Kubernetes 它可以恢复调度新的 pods 到节点上。

请注意,如果有任何 pods 不是由 ReplicationController、ReplicaSet、DaemonSet、StatefulSet 或 Job 管理的,那么 drain 不会删除任何 pods 除非你使用 --force, as mentioned in the docs.

kubectl drain <node name> --force

我尝试了很多变体来从教程中删除旧的 pods,包括所有内容

最终对我有用的是:

kubectl delete replicaset --all

一次一个地删除它们似乎没有用;只有使用 --all 标志,所有 pods 都被删除而没有重新创建。

minikube delete --all 

如果你使用的是 minikube

它会让你启动一个新的干净集群。


如果你 运行 在 Kubernetes 上:

kubectl delete pods,deployments -A --all

它将从所有命名空间中删除它,您可以在同一命令中添加更多对象。