如何删除跨所有命名空间的Kubernetes资源?

huangapple go评论58阅读模式
英文:

How to delete kubernetes resources across all namespaces?

问题

我想知道是否有一种方法可以跨所有命名空间在Kubernetes中删除特定资源?我想一次性删除所有类型为LoadBalancer的服务,并在流水线中自动化这个过程。我已经构建了一个xargs kubectl命令,它会获取所有命名空间中LoadBalancer服务的名称,并将输出提供给kubectl delete命令。现在我只需要在所有命名空间中循环执行它。

如果我去掉--all-namespaces标志,然后运行--dry-run=client标志,那么我将在所有命名空间上获得所有我想要删除的服务的干预删除。K8s是否有一种方法可以让您按名称在所有命名空间中删除资源?

有什么想法?

更新:
这是使用--dry-run标志运行命令的输出,它获取了我想要删除的所有服务的名称,并自动将它们提供给kubectl delete命令

kubectl get service -A -o json | jq '.items[] | select (.spec.type=="LoadBalancer")' | jq '.metadata.name' | xargs kubectl delete services --dry-run=client
service "foo-example-service-1" deleted (dry run)
service "bar-example-service-2" deleted (dry run)
service "baz-example-service-3" deleted (dry run)
service "nlb-sample-service" deleted (dry run)

唯一缺少的部分是我需要在所有命名空间中执行删除,以删除所有指定的服务,我只想删除类型为LoadBalancer的服务,而不是其他类型的服务,如ClusterIP或NodePort等,因此必须提供特定的名称。

英文:

I want to know if there is a way to delete specific resources in kubernetes across all namespaces? I want to delete all the services as type of LoadBalancer at once, and have this automated in a pipeline. I already built a xargs kubectl command which will get the name of LoadBalancer services across all namespaces and feed the output to the kubectl delete command. I just need to loop across all namespaces now.

kubectl get service -A -o json | jq '.items[] | select (.spec.type=="LoadBalancer")' | jq '.metadata.name' | xargs kubectl delete services --all-namespaces
error: a resource cannot be retrieved by name across all namespaces

If I remove the --all-namespaces flag and run --dry-run=client flag instead, then I get a dry-run deletion of all the services I want getting deleted on all namespaces. Is there a way k8s lets you delete resources by name across all namespaces?

Any ideas?

UPDATE:
This is the output of running the command using --dry-run flag, it gets the name of all the services I want to delete and automatically feeds them to the kubectl delete command

kubectl get service -A -o json | jq '.items[] | select (.spec.type=="LoadBalancer")' | jq '.metadata.name' | xargs kubectl delete services --dry-run=client
service "foo-example-service-1" deleted (dry run)
service "bar-example-service-2" deleted (dry run)
service "baz-example-service-3" deleted (dry run)
service "nlb-sample-service" deleted (dry run)

The only part missing is that I need to do the deletion across all namespaces to delete all the specified services, I only want to delete services of type LoadBalancer and not other types of services like ClusterIP or NodePort or anything so the specific names must be provided.

答案1

得分: 0

你可以使用以下命令来实现:

kubectl delete services $(kubectl get services --all-namespaces \
-o jsonpath='{range .items[?(@.spec.type=="LoadBalancer")]}{.metadata.name} -n {.metadata.namespace}\n{end}')

kubectl get 命令的输出提供了一个列表,其中包含了 <service_name> -n <namespace_name> 字符串,这些字符串被用于 kubectl delete 命令来删除指定命名空间中的服务。

英文:

You can achieve that using jsonpath with the following command:

kubectl delete services $(kubectl get services --all-namespaces \
-o jsonpath='{range .items[?(@.spec.type=="LoadBalancer")]}{.metadata.name}{" -n "}{.metadata.namespace}{"\n"}{end}')

The output of the kubectl get command gives a list of <service_name> -n <namespace_name> strings that are used by the kubectl delete command to delete services in the specified namespaces.

huangapple
  • 本文由 发表于 2023年2月16日 03:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定