gpt4 book ai didi

kubernetes - 我想使用kubectl的LabelSelector输出 "kubectl get pod"不包括key的结果

转载 作者:行者123 更新时间:2023-12-01 23:27:45 24 4
gpt4 key购买 nike

我正在通过以下 URL 学习 Kubernetes 标签选择器。

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

基于集合的要求

Set-based label requirements allow filtering keys according to a set of values. Three kinds of operators are supported: in, notin and exists (only the key identifier). For example:

! partition

The fourth(This) example selects all resources without a label with key partition; no values ​​are checked.

我的假设是,如果我指定“!test”,所有的 pod 都会被输出,我认为指定“!app”时不会输出任何内容。

我想按照此处描述的示例排除键和输出,但没有获得预期的结果。

My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod -l! app
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !app
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l "!app"
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l "!test"
-bash: !test: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !test
-bash: !test: event not found

执行--show-labels命令的结果如下。

My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod-a 1/1 Running 0 89m app=photo-view,env=stage
nginx-pod-b 1/1 Running 0 89m app=imagetrain,env=test
nginx-pod-c 1/1 Running 0 89m app=prediction,env=test
nginx-pod-d 1/1 Running 0 89m app=photo-view,env=stage
nginx-pod-e 1/1 Running 0 89m app=imagetrain,env=stage
nginx-pod-f 1/1 Running 0 89m app=photo-view,env=prod

如何排除预期的键,例如 app 和 env?


tyzbit给了我答案,我也试过了。我无法在评论中很好地描述结果,所以我将在此处添加。

My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!app'
No resources found in default namespace.
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!test'
NAME READY STATUS RESTARTS AGE
nginx-pod-a 1/1 Running 0 162m
nginx-pod-b 1/1 Running 0 162m
nginx-pod-c 1/1 Running 0 162m
nginx-pod-d 1/1 Running 0 162m
nginx-pod-e 1/1 Running 0 162m
nginx-pod-f 1/1 Running 0 162m

Girdhar Singh Rathore 教我 AWK 和 jsonpath。

(可能是我格式写错了...)

AWK
My@Pc:~$ kubectl get pods --show-labels | awk  '$6 !~/app/ {print ;}'
NAME READY STATUS RESTARTS AGE LABELS

My@Pc:~$ kubectl get pods --show-labels | awk '$6 !~/test/ {print ;}'
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod-a 1/1 Running 0 18m app=photo-view,env=stage
nginx-pod-d 1/1 Running 0 18m app=photo-view,env=stage
nginx-pod-e 1/1 Running 0 18m app=imagetrain,env=stage
nginx-pod-f 1/1 Running 0 18m app=photo-view,env=prod
json路径
My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {"\n"} {end}' | awk '$2 !~/app/ {print $1}'

My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {"\n"} {end}' | awk '$2 !~/test/ {print $1}'
nginx-pod-a
nginx-pod-d
nginx-pod-e
nginx-pod-f

最佳答案

! 正在被你的 shell 解释,使用单引号来防止这种情况。正确的语法是:

kubectl get pods -l '!app'

关于kubernetes - 我想使用kubectl的LabelSelector输出 "kubectl get pod"不包括key的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66937893/

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