使用单一语法重新启动多个非常具体的 pods

reboot multiple and very specific pods using a single syntax

root@x:~# kubectl get pods -A -o wide| grep nic
a      k-e-f-v1-k-e-nic-s-r8tjn           1/1     Running     1          5d11h   192.168.99.1     master.k                <none>           <none>
a      k-e-f-v1-k-e-nic-s-w6tk8           1/1     Running     0          5d11h   192.168.99.231   e-519-19121100100009   <none>           <none>
a      k-e-f-v1-k-e-nic-s-z8pmq           1/1     Running     0          5d11h   192.168.99.127   e-519-19121100100008   <none>           <none>

想根据以上结果重启命名空间 a 中除 master 之外的所有 pods。 有没有一种语法可以做到这一点?

那些 pods 是否有任何可用于识别它们的特定标签?如果到目前为止还没有标记它们,我会建议您这样做,因为 grep-ing 对它们名称中的特定字符串进行标记既不是很方便也不是优雅的解决方案。并且可以肯定的是,如果不使用 grep.

等外部工具,您不能使用纯 kubectl 命令在其名称中包含特定字符串的 select pods

As to selecting all pods (either in a specific --namespace or in --all-namespaces), 运行ning on all nodes except the specific第一,通过在 --field-selector:

中使用 negation 可以很容易地完成

下面的 命令 将在除 master 以外的任何节点上列出 --all-namespaces、运行ning 中的 Pods。此外,它将仅列出标有键 app 和值 nginx:

Pods
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName!=master --selector=app=nginx

如果要删除这样的 Pods, 运行:

kubectl delete pods --all-namespaces --field-selector spec.nodeName!=master --selector=app=nginx

如您所知,删除 of Podsrestarting/rebooting基本相同。如果那些 Pods 得到管理,例如通过 Deployment 它们将在删除后简单地重新创建。

如果您真的必须使用 grep 来搜索 Pods 名称中的特定字符串,您可以使用相当简单的脚本来删除此类特定 Pods:

kubectl get pods --all-namespaces -o name --field-selector spec.nodeName!=master | grep nic | xargs kubectl delete

但是如您所见,上述命令比使用 --selector 标志仅过滤掉具有特定标签的 Pods 的单个 kubectl delete 复杂得多。这个使用 grepxargs、两个管道和两个单独的 kubectl 命令 运行s.