在查看文档时,有一个API调用可以删除单个pod,但是否有一种方法可以删除所有名称空间中的所有pod ?


当前回答

你只需要sed这样做:

kubectl get pods --no-headers=true --all-namespaces |sed -r 's/(\S+)\s+(\S+).*/kubectl --namespace \1 delete pod \2/e'

解释道:

use command kubectl get pods --all-namespaces to get the list of all pods in all namespaces. use --no-headers=true option to hide the headers. use s command of sed to fetch the first two words, which represent namespace and pod's name respectively, then assemble the delete command using them. the final delete command is just like: kubectl --namespace kube-system delete pod heapster-eq3yw. use the e modifier of s command to execute the command assembled above, which will do the actual delete works.

为了避免在kube-system namespace中删除pods,只需要在sed命令前添加grep -v kube-system排除kube-system namespace即可。

其他回答

如果您已经有了重新创建的pod,请考虑先删除所有部署

kubectl delete -n *NAMESPACE deployment *DEPLOYMENT

只需将NAMSPACE和DEPLOYMENT替换为相应的NAMSPACE和DEPLOYMENT,就可以通过以下命令获得所有部署信息

kubectl get deployments --all-namespaces

没有命令可以完全按照你的要求去做。

以下是一些势均力敌的比赛。

在运行这些命令之前一定要小心。如果使用多个集群,请确保连接到正确的集群。考虑运行。Kubectl配置视图优先。

你可以用这个命令删除一个命名空间中的所有pod:

kubectl delete --all pods --namespace=foo

您还可以删除命名空间中的所有部署,这将删除与该命名空间对应的部署附加的所有pods

kubectl delete --all deployments --namespace=foo

你可以用这个命令删除所有命名空间和每个命名空间中的每个对象(但不能删除非命名空间的对象,如节点和一些事件):

kubectl delete --all namespaces

但是,后一个命令可能不是您想要的,因为它将删除kube-system名称空间中的内容,这将使您的集群不可用。

该命令将删除除kube-system之外的所有命名空间,这可能很有用:

for each in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system);
do
  kubectl delete ns $each
done

K8s完全在命名空间的基础上工作。如果要释放与指定命名空间相关的所有资源。

你可以使用下面提到的:

kubectl delete namespace k8sdemo-app

下面是一个单行程序,可以使用grep进行扩展以按名称过滤。

kubectl get pods -o jsonpath="{.items[*].metadata.name}" | \
tr " " "\n" | \
xargs -i -P 0 kubectl delete pods {}

我尝试了这里列出的答案中的命令,但豆荚卡在终止状态。 我发现下面的命令可以删除特定名称空间中的所有pods,如果处于终止状态或无法删除它,那么可以强制删除pods。

kubectl delete pods --all --grace-period=0 --force --namespace namespace

希望对别人有用。