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


当前回答

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

你可以使用下面提到的:

kubectl delete namespace k8sdemo-app

其他回答

kubectl delete daemonsets,replicasets,services,deployments,pods,rc,ingress --all --all-namespaces

也摆脱了烦人的复制控制器。

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

上面已经暗示过,但我只是想指出“——all-namespaces”的快捷方式是“-A”,大写的a . HTH someone。我已经打开了一个PR,将这个有用的提示添加到官方Kubernetes小抄表中。

Kubectl bulk (bulk-action on krew)插件可能对您有用,它为您提供了选定资源的批量操作。删除pod的命令

 ' kubectl bulk pods -n namespace delete '

你可以在这里查看细节

一行命令删除所有命名空间中的所有pod。

kubectl get ns -o=custom-columns=Namespace:.metadata.name --no-headers | xargs -n1 kubectl delete pods --all -n