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


当前回答

你可以简单地跑

kubectl delete all --all --all-namespaces

第一个都是指常见的资源类型(pods, replicasets, deployment,…) Kubectl get all == Kubectl get pods,rs,部署,… 第二,all表示选择所选类型的所有资源


注意,all不包括:

非命名空间资源(例如,clusterrolebindings, clusterroles,…) configmaps rolebindings 角色 秘密 ...


为了完美地清理,

你可以使用其他工具(例如Helm, Kustomize,…) 您可以使用名称空间。 您可以在创建资源时使用标签。

其他回答

你可以简单地跑

kubectl delete all --all --all-namespaces

第一个都是指常见的资源类型(pods, replicasets, deployment,…) Kubectl get all == Kubectl get pods,rs,部署,… 第二,all表示选择所选类型的所有资源


注意,all不包括:

非命名空间资源(例如,clusterrolebindings, clusterroles,…) configmaps rolebindings 角色 秘密 ...


为了完美地清理,

你可以使用其他工具(例如Helm, Kustomize,…) 您可以使用名称空间。 您可以在创建资源时使用标签。

你只需要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即可。

我创建了一个python代码删除所有在命名空间

delall.py

import json,sys,os;

obj=json.load(sys.stdin);
for item in obj["items"]:
        os.system("kubectl delete " + item["kind"] + "/" +item["metadata"]["name"] + " -n yournamespace")

然后

kubectl get all -n kong -o json | python delall.py

如果你想删除所有命名空间中的pod,只是为了让它们重新启动,并且你知道其中一些将被重新创建,我喜欢下面的for循环:

for i in $(kubectl get pods -A | awk '{print $1}' | uniq | grep -V NAMESPACE); do kubectl delete --all pods -n $i; done

你可以使用kubectl删除pods -l dev-lead!=carisa或其他标签。