gpt4 book ai didi

regex - 从命名空间名称中具有正则表达式模式匹配的节点获取 k8s pod

转载 作者:行者123 更新时间:2023-12-05 02:08:24 25 4
gpt4 key购买 nike

团队,

我能够使用其命名空间获取在节点上运行的所有 pod,但我的命名空间是动态生成的,并且它们最终会随着字符而变化。有没有一种方法可以包含一个正则表达式/模式,我可以在 kubectl 命令中使用它来从所有匹配的命名空间中提取所有 pod?

kubectl get pods -n team-1-user1 --field-selector=spec.nodeName=node1,status.phase=Running

实际输出1:有效

NAMESPACE                                     NAME                                                                   READY   STATUS    RESTARTS   AGE
team-1-user1 calico-node-9j5k2 1/1 Running 2 104d
team-1-user1 kube-proxy-ht7ch 1/1 Running 2 130d

我想在下方为所有以“team-”开头的命名空间拉取 pod。

kubectl get pods -n team-* --field-selector=spec.nodeName=node1,status.phase=Running

实际输出2:失败

No resources found in team-workflow-2134-asf-324-d.yaml namespace.

预期结果:想要这个..

NAMESPACE                                     NAME                                                                   READY   STATUS    RESTARTS   AGE
team-1-user1 calico-node-9j5k2 1/1 Running 2 104d
team-1-user1 kube-proxy-ht7ch 1/1 Running 2 130d

team-2-user1 calico-node-9j5k2 1/1 Running 2 1d
team-2-user1 kube-proxy-ht7ch 1/1 Running 2 10d

最佳答案

您可以将 kubectl get pods 的输出通过管道传输到 awk 并匹配相同的正则表达式:

kubectl get pods --all-namespaces --no-headers |  awk '{if ($1 ~ "team-") print $0}'

这是相同的示例输出,在 kube- 命名空间中搜索 pod:

❯❯❯ kubectl get pods --all-namespaces --no-headers |  awk '{if ($1 ~ "kube-") print $0}'
kube-system coredns-6955765f44-27wxs 1/1 Running 0 107s
kube-system coredns-6955765f44-ztgq8 1/1 Running 0 106s
kube-system etcd-minikube 1/1 Running 0 109s
kube-system kube-addon-manager-minikube 1/1 Running 0 108s

关于regex - 从命名空间名称中具有正则表达式模式匹配的节点获取 k8s pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60978474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com