gpt4 book ai didi

bash - 命名路径的 kubectl jsonpath 表达式

转载 作者:行者123 更新时间:2023-12-02 11:50:06 25 4
gpt4 key购买 nike

我有 kube 服务运行 2 个命名端口,如下所示:

$ kubectl get service elasticsearch --output json
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
... stuff that really has nothing to do with my question ...
},
"spec": {
"clusterIP": "10.0.0.174",
"ports": [
{
"name": "http",
"nodePort": 31041,
"port": 9200,
"protocol": "TCP",
"targetPort": 9200
},
{
"name": "transport",
"nodePort": 31987,
"port": 9300,
"protocol": "TCP",
"targetPort": 9300
}
],
"selector": {
"component": "elasticsearch"
},
"sessionAffinity": "None",
"type": "NodePort"
},
"status": {
"loadBalancer": {}
}
}

我正在尝试获取仅包含“http”端口的输出:
$ kubectl get service elasticsearch --output jsonpath={.spec.ports[*].nodePort}
31041 31987

除非我在此处的备忘单中添加了测试表达式 http://kubernetes.io/docs/user-guide/kubectl-cheatsheet/对于名称,我收到错误消息
$ kubectl get service elasticsearch --output jsonpath={.spec.ports[?(@.name=="http")].nodePort}
-bash: syntax error near unexpected token `('

最佳答案

()在 bash 中表示某些东西(请参阅 subshell ),因此您的 shell 解释器首先执行此操作并感到困惑。将参数包装到 jsonpath在单引号中,这将解决它:

$ kubectl get service elasticsearch --output jsonpath='{.spec.ports[?(@.name=="http")].nodePort}'

例如:
# This won't work:
$ kubectl get service kubernetes --output jsonpath={.spec.ports[?(@.name=="https")].targetPort}
-bash: syntax error near unexpected token `('

# ... but this will:
$ kubectl get service kubernetes --output jsonpath='{.spec.ports[?(@.name=="https")].targetPort}'
443

关于bash - 命名路径的 kubectl jsonpath 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527367/

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