命名路径的 kubectl jsonpath 表达式
kubectl jsonpath expression for named path
我有 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
对我来说,它在 windows 机器上给出错误:
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.name=="web")].nodePort}'
> executing jsonpath "'{.spec.ports[?(@.name==web)].nodePort}'":
> unrecognized identifier web
尽管我的 json 包含端口数组中的名称字段。在线它工作正常。
我尝试使用端口字段,而不是使用名称字段,它是整数类型并且有效。
因此,如果有人面临同样的问题,并且如果端口字段是预定义的,那么他们可以使用它。
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.port==9000)].nodePort}'
我在 Powershell 的 Windows 上遇到了这个问题:
Error executing template: unrecognized identifier http2
在 jsonpath 中指定用双引号引起来的字符串值时 - 解决错误的方法有 2 种:
交换单引号和双引号:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath="{.spec.ports[?(@.name=='http2')].port}"
或者转义双引号中封装的单引号:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name==\"http2\")].port}'
我有 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
对我来说,它在 windows 机器上给出错误:
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.name=="web")].nodePort}'
> executing jsonpath "'{.spec.ports[?(@.name==web)].nodePort}'":
> unrecognized identifier web
尽管我的 json 包含端口数组中的名称字段。在线它工作正常。
我尝试使用端口字段,而不是使用名称字段,它是整数类型并且有效。
因此,如果有人面临同样的问题,并且如果端口字段是预定义的,那么他们可以使用它。
kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.port==9000)].nodePort}'
我在 Powershell 的 Windows 上遇到了这个问题:
Error executing template: unrecognized identifier http2
在 jsonpath 中指定用双引号引起来的字符串值时 - 解决错误的方法有 2 种:
交换单引号和双引号:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath="{.spec.ports[?(@.name=='http2')].port}"
或者转义双引号中封装的单引号:
kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name==\"http2\")].port}'