指定仅使用CLI显示资源的Kubernetes名称空间

发布于 2025-01-28 06:42:12 字数 795 浏览 1 评论 0原文

是否可以使用多个指定namepaces的Kubernetes(K8S)资源使用 唯一的 CLI(kubectl)?

换句话说,在k8s群集中给定两个名称空间

kubectl get namespaces \
--output=go-template \
--template='{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}'

#=>

. . .
$SOME_NAMESPACE
. . .
$ANOTHER_NAMESPACE
. . .

仅从get资源(例如pods使用 唯一的> kubectl

提供- 全名空间标志和使用- field-selector- selector标志将 not not < /strong>工作,因为两个标志仅接受===!=运算符。

Is it possible to display Kubernetes (K8s) resources from multiple specified namespaces with only the CLI (kubectl)?

In other words, given two namespaces in a K8s cluster:

kubectl get namespaces \
--output=go-template \
--template='{{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}'

#=>

. . .
$SOME_NAMESPACE
. . .
$ANOTHER_NAMESPACE
. . .

would it be possible to get resources (such as pods) from only those two namespaces ($SOME_NAMESPACE and $ANOTHER_NAMESPACE) using only kubectl?

Supplying the --all-namespaces flag and filtering using either the --field-selector or --selector flags will not work, because both flags accept only =, == and != operators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2025-02-04 06:42:12

您可以使用go-template打印属于两个名称空间的POD的名称,以下是从test> test-1test打印POD的示例-2名称空间。

kubectl get pod -A -o go-template='{{range .items}}{{if or (eq .metadata.namespace "test-1") (eq .metadata.namespace "test-2") }}{{printf "%s %s\n" .metadata.namespace .metadata.name}}{{end}}{{end}}'

You may use go-template to print the name of the pods belonging to the two namespaces, following is an example of printing pods from the test-1 and test-2 namespace.

kubectl get pod -A -o go-template='{{range .items}}{{if or (eq .metadata.namespace "test-1") (eq .metadata.namespace "test-2") }}{{printf "%s %s\n" .metadata.namespace .metadata.name}}{{end}}{{end}}'
远昼 2025-02-04 06:42:12

根据的讨论当前不可用可以指定多个名称空间:单独使用- 单独使用标志,或者使用过滤标志(例如- selector)。

can 目前,使用诸如bash brace扩展的解决方法,如建议此处,或用命令- All-Namespaces flag flag flag awk :

kubectl get pods \
--all-namespaces \
| awk \
-v SOME_NAMESPACE=$SOME_NAMESPACE \
-v ANOTHER_NAMESPACE=$ANOTHER_NAMESPACE \
'$1 == SOME_NAMESPACE || $1 == ANOTHER_NAMESPACE'

According to the discussion that has already occurred in this GitHub issue, it does not currently seem possible to specify multiple namespaces: either with just the --namespace flag alone or using a filtering flag such as --selector.

You can, for now, use workarounds such as Bash brace expansion, as suggested here, or feeding the output of the get command with the --all-namespaces flag into awk:

kubectl get pods \
--all-namespaces \
| awk \
-v SOME_NAMESPACE=$SOME_NAMESPACE \
-v ANOTHER_NAMESPACE=$ANOTHER_NAMESPACE \
'$1 == SOME_NAMESPACE || $1 == ANOTHER_NAMESPACE'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文