使用 kubectl get ... -o 模板时获取地图的第一个元素
Get first element of a map when using kubectl get ... -o template
我有一项服务,我想从其规范中获取 IP,使用 -o go-template
我可以这样做:
kubectl get service webapp1-loadbalancer-svc -o go-template='{{(index .status.loadBalancer.ingress 0).ip}}'
这个 returns 负载均衡器中第一个入口的 IP,这就是我想要的。
但是,我不想使用 -o go-template
,而是想使用 -o template
。我尝试了多个命令,但无法执行。我工作的最接近的事情是:
kubectl get service webapp1-loadbalancer-svc -o template={{.status.loadBalancer.ingress}}
但是这个returns地图[map[ip:172.17.0.28]]
,不仅仅是IP。我尝试在同一命令中获取 IP 的所有操作在执行模板时都返回错误。
有没有一种方法可以使用一个 kubectl
命令从地图中获取 IP,使用 -o template
而不是 -o go-template
?
Kubectl supports JSONPath template.
使用 JsonPath,您可以检索服务集群 IP 或其他详细信息,如下所示。
ubuntu@k8s-master:~$ kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx 1/1 Running 0 129m 192.168.85.226 k8s-node01 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d2h <none>
service/nginx ClusterIP 10.96.184.196 <none> 80/TCP 11m run=nginx
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.clusterIP}'
10.96.184.196
使用 JsonPath 也适用于 LoadBlancer 类型。希望这是您想要使用的。
$ kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx 1/1 Running 0 133m 192.168.85.226 k8s-node01 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d2h <none>
service/nginx LoadBalancer 10.100.165.17 <pending> 80:30852/TCP 5s run=nginx
ubuntu@k8s-master:~$
10.100.165.17ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.type}'
LoadBalancer
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.clusterIP}'
10.100.165.17
所以 当你提取 PORT 时它也会 return 如下图
$ kubectl get service nginx -o jsonpath='{.spec.ports}'
[map[nodePort:30852 port:80 protocol:TCP targetPort:80]]
您可以如下提取nodePort、port和targetPort
$ kubectl get service nginx -o jsonpath='{.spec.ports[].targetPort}'
80
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.ports[].nodePort}'
30852
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.ports[].port}'
80
希望以上示例能帮助您解决问题
我想你在使用jsonpath的时候应该可以通过这个命令来完成。
kubectl get service webapp1-loadbalancer-svc -o jsonpath={{.status.loadBalancer.ingress[].ip}}
我有一项服务,我想从其规范中获取 IP,使用 -o go-template
我可以这样做:
kubectl get service webapp1-loadbalancer-svc -o go-template='{{(index .status.loadBalancer.ingress 0).ip}}'
这个 returns 负载均衡器中第一个入口的 IP,这就是我想要的。
但是,我不想使用 -o go-template
,而是想使用 -o template
。我尝试了多个命令,但无法执行。我工作的最接近的事情是:
kubectl get service webapp1-loadbalancer-svc -o template={{.status.loadBalancer.ingress}}
但是这个returns地图[map[ip:172.17.0.28]]
,不仅仅是IP。我尝试在同一命令中获取 IP 的所有操作在执行模板时都返回错误。
有没有一种方法可以使用一个 kubectl
命令从地图中获取 IP,使用 -o template
而不是 -o go-template
?
Kubectl supports JSONPath template.
使用 JsonPath,您可以检索服务集群 IP 或其他详细信息,如下所示。
ubuntu@k8s-master:~$ kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx 1/1 Running 0 129m 192.168.85.226 k8s-node01 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d2h <none>
service/nginx ClusterIP 10.96.184.196 <none> 80/TCP 11m run=nginx
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.clusterIP}'
10.96.184.196
使用 JsonPath 也适用于 LoadBlancer 类型。希望这是您想要使用的。
$ kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx 1/1 Running 0 133m 192.168.85.226 k8s-node01 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d2h <none>
service/nginx LoadBalancer 10.100.165.17 <pending> 80:30852/TCP 5s run=nginx
ubuntu@k8s-master:~$
10.100.165.17ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.type}'
LoadBalancer
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.clusterIP}'
10.100.165.17
所以 当你提取 PORT 时它也会 return 如下图
$ kubectl get service nginx -o jsonpath='{.spec.ports}'
[map[nodePort:30852 port:80 protocol:TCP targetPort:80]]
您可以如下提取nodePort、port和targetPort
$ kubectl get service nginx -o jsonpath='{.spec.ports[].targetPort}'
80
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.ports[].nodePort}'
30852
ubuntu@k8s-master:~$ kubectl get service nginx -o jsonpath='{.spec.ports[].port}'
80
希望以上示例能帮助您解决问题
我想你在使用jsonpath的时候应该可以通过这个命令来完成。
kubectl get service webapp1-loadbalancer-svc -o jsonpath={{.status.loadBalancer.ingress[].ip}}