2017-01-07 38 views
0

表達我有2個名爲端口像這樣運行KUBE服務:kubectl jsonpath名爲路徑

$ 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 

除了當我添加測試表達式暗示在這裏作爲這個名字我得到一個錯誤

$ kubectl get service elasticsearch --output jsonpath={.spec.ports[?(@.name=="http")].nodePort} 
-bash: syntax error near unexpected token `(' 

回答

5

()意味着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 
0

對於我來說,這是給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在ports數組中包含名稱字段。在線它工作正常。

而不是使用名稱字段,然後我已嘗試與端口字段,這是類型的整數,它的工作原理。

因此,如果任何一個人面臨同樣的問題,並且如果端口字段是預定義的,那麼他們可以使用它。

kubectl --namespace=app-test get svc proxy --output jsonpath='{.spec.ports[?(@.port==9000)].nodePort}'