使用模式匹配或Wilcard删除Kubenetes POD

当我在下面使用时,它会在从命令行匹配模式后删除正在运行的pod

kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}' | xargs kubectl delete -n bi-dev pod

但是,当我将此命令用作.bash_profile中的别名时,它不会执行。 这就是我的定义方式:

 alias kdpgroup="kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}'|  kubectl delete -n bi-dev pod"

按以下方式执行此操作时,在命令行中出现以下错误:

~ $ kdpgroup
error: resource(s) were provided,but no name,label selector,or --all flag specified

当我在.bash_profile中定义它时,我得到了:

~ $ . ./.bash_profile
-bash: alias: }| xargs  kubectl delete -n bi-dev pod: not found
~ $

我是否缺少使用模式匹配或Wilcard删除pod的东西?

谢谢

wang532948001 回答:使用模式匹配或Wilcard删除Kubenetes POD

正如问题所问,这是关于使用通配符。让我举例说明使用通配符删除 Pod。

删除包含“application”一词的 Pod

<namespace> 替换为您要从中删除 Pod 的命名空间。

kubectl get pods -n <namespace> --no-headers=true | awk '/application/{print $1}'| xargs  kubectl delete -n <namespace> pod

这将给出如下响应。它将打印出已删除的 pod。

pod "sre-application-7fb4f5bff9-8crgx" deleted
pod "sre-application-7fb4f5bff9-ftzfd" deleted
pod "sre-application-7fb4f5bff9-rrkt2" deleted

删除包含“应用程序”或“服务”的 Pod

<namespace> 替换为您要从中删除 Pod 的命名空间。

kubectl get pods -n <namespace> --no-headers=true | awk '/application|service/{print $1}'| xargs  kubectl delete -n <namespace> pod

这将给出如下响应。它将打印出已删除的 pod。

pod "sre-application-7fb4f5bff9-8crgx" deleted
pod "sre-application-7fb4f5bff9-ftzfd" deleted
pod "sre-service-7fb4f5bff9-rrkt2" deleted
,

您只需要在awk命令中转义'$ 1'变量:

alias kdpgroup="kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print \$1}'|  kubectl delete -n bi-dev pod"

我知道转义很无聊,如果要避免转义,可以将其用作您的.bash_profile中的函数:

kdpgroup() {
    kubectl get pods -n default --no-headers=true | awk '{print $1}' | xargs kubectl delete pod -n default
}
,
  

我是否缺少使用模式匹配或Wilcard删除POD的东西?

使用Kubernetes时,更常见的是使用 labels selectors 。例如。如果您部署了应用程序,通常会在Pod上设置标签,例如app=my-app,然后您可以通过以下方式获取广告连播: kubectl get pods -l app=my-app

使用此方法,例如,可以更轻松地删除您感兴趣的广告连播。

kubectl delete pods -l app=my-app

或带有名称空间

kubectl delete pods -l app=my-app -n default

有关Kubernetes Labels and Selectors的更多信息

本文链接:https://www.f2er.com/2864932.html

大家都在问