Kubernetes 如何显示装载特定configmap/secret的所有部署/守护程序?
有时,我想探索装载特定configmap/secret的所有部署/守护程序Kubernetes 如何显示装载特定configmap/secret的所有部署/守护程序?,kubernetes,kubectl,Kubernetes,Kubectl,有时,我想探索装载特定configmap/secret的所有部署/守护程序 有什么方法可以通过kubectl实现这一点吗?您需要jq来执行如此复杂的查询。 给你: kubectl get -o json deploy,daemonset | jq '[.items[] | . as $parent | .spec.template.spec.volumes[]? | select(.configMap != null) | {kind:$parent.kind, name:$parent.met
有什么方法可以通过
kubectl
实现这一点吗?您需要jq
来执行如此复杂的查询。
给你:
kubectl get -o json deploy,daemonset | jq '[.items[] | . as $parent | .spec.template.spec.volumes[]? | select(.configMap != null) | {kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name}]'
jq
命令被解构:
[ // output as array
.items[] // iterate over root key 'items'
|
. as $parent // store the current entry as $parent to refer to it later
|
.spec.template.spec.volumes[]? // iterate over its volumes (the ? to prevent error if there is no volume
|
select(.configMap != null) // select only those volumes with configMap key
|
{kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name} // now construct the output using $parent's kind and name and the configMap's name
]
示例输出:
[
{
"kind": "Deployment",
"name": "telemetry-agent",
"configMap": "telemetry-config-map"
},
{
"kind": "DaemonSet",
"name": "fluent-bit",
"configMap": "fluent-bit"
},
{
"kind": "DaemonSet",
"name": "telegraf",
"configMap": "telegraf"
}
]
注意:如果要查找特定的configMap,只需替换
select()
子句.configMap!=null
到.configMap.name==“特定的configMap”
。另外,如果您想从所有名称空间进行查询,可以将--all namespaces
添加到kubectl get
命令中,您需要使用jq
来执行如此复杂的查询。
给你:
kubectl get -o json deploy,daemonset | jq '[.items[] | . as $parent | .spec.template.spec.volumes[]? | select(.configMap != null) | {kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name}]'
jq
命令被解构:
[ // output as array
.items[] // iterate over root key 'items'
|
. as $parent // store the current entry as $parent to refer to it later
|
.spec.template.spec.volumes[]? // iterate over its volumes (the ? to prevent error if there is no volume
|
select(.configMap != null) // select only those volumes with configMap key
|
{kind:$parent.kind, name:$parent.metadata.name, configMap:.configMap.name} // now construct the output using $parent's kind and name and the configMap's name
]
示例输出:
[
{
"kind": "Deployment",
"name": "telemetry-agent",
"configMap": "telemetry-config-map"
},
{
"kind": "DaemonSet",
"name": "fluent-bit",
"configMap": "fluent-bit"
},
{
"kind": "DaemonSet",
"name": "telegraf",
"configMap": "telegraf"
}
]
注意:如果要查找特定的configMap,只需替换select()
子句.configMap!=null
到.configMap.name==“特定的configMap”
。另外,如果您想从所有名称空间进行查询,可以将--all namespace
添加到kubectl get
命令中