[英]How do I get the External IP of a Kubernetes service as a raw value?
I am running an application with GKE.我正在使用 GKE 运行应用程序。 It works fine but I can not figure out how to get the external IP of the service in a machine readable format.
它工作正常,但我不知道如何以机器可读格式获取服务的外部 IP。 So i am searching a gcloud or kubectl command that gives me only the external IP or a url of the format
http://192.168.0.2:80
so that I can cut out the IP.所以我正在搜索 gcloud 或 kubectl 命令,它只给我外部 IP 或格式
http://192.168.0.2:80
的 url,这样我就可以删除 IP。
Maybe not GKE as my clusters are on AWS, but I assume logic will be similar.也许不是 GKE,因为我的集群在 AWS 上,但我认为逻辑会相似。 When you
kubectl get svc
you can select output format and it will show more then just the "normal" get.当您
kubectl get svc
您可以选择输出格式,它会显示比“正常”获取更多的内容。 For me, with ELB based services to het LB hostname it's enough to run ie.对我来说,使用基于 ELB 的服务来处理 LB 主机名就足以运行即。
kubectl -n kube-system get svc cluster-nginx-ingress-controller -o json | jq .status.loadBalancer.ingress.hostname
You can use the jsonpath output type to get the data directly without needing the additional jq
to process the json:可以使用jsonpath输出类型直接获取数据,不需要额外的
jq
来处理json:
kubectl get services --namespace ingress-nginx nginx-ingress-controller --output jsonpath='{.status.loadBalancer.ingress[0].ip}'
(Be sure to replace the namespace and service name, respectively, with yours.) (请务必将命名空间和服务名称分别替换为您的名称。)
In my case ' kubectl get services ' returns array of items, but not just one service.在我的情况下,“ kubectl get services ”返回项目数组,但不仅仅是一项服务。
So then such jsonpath works fine to me:那么这样的 jsonpath 对我来说很好用:
kubectl get services -l component=controller,app=nginx-ingress -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
To get the external-ip on GCP i can use:要在 GCP 上获取外部 IP,我可以使用:
kubectl get services --namespace=<your-namespace> -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
The answers above do not provide the output the user asked.上面的答案没有提供用户要求的输出。 The correct command would be:
kubectl -n $namespace get svc $ingressServiceName -o json | jq -r .status.loadBalancer.ingress[].hostname
正确的命令是:
kubectl -n $namespace get svc $ingressServiceName -o json | jq -r .status.loadBalancer.ingress[].hostname
kubectl -n $namespace get svc $ingressServiceName -o json | jq -r .status.loadBalancer.ingress[].hostname
...and yet another way... This will list all the "load-balancer" services ...还有另一种方式...这将列出所有“负载平衡器”服务
kubectl get services --all-namespaces -o json | jq -r '.items[] | { name: .metadata.name, ns: .metadata.namespace, ip: .status.loadBalancer?|.ingress[]?|.ip }'
Depending on the networkPlugin used by your cluster services/pods may be exposed directly on external-ip.根据您的集群服务/pod 使用的网络插件,可能会直接暴露在 external-ip 上。 But this will also find an Ingress controllers run in the cluster.
但这也会发现在集群中运行的 Ingress 控制器。
All previous solutions don't work any more for me (on GCP).所有以前的解决方案对我都不再适用(在 GCP 上)。
To get the IP:获取IP:
kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
To get the host-name:要获取主机名:
kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.spec.rules[0].host}"
Type类型
minikube tunnel
or要么
kubectl cluster-info
You can get the public exposed IP of your relevant service.您可以通过IP公开您的相关服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.