简体   繁体   English

如何获取 Kube.netes 服务的外部 IP 作为原始值?

[英]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.

相关问题 Kubernetes LoadBalancer 服务未获取 ELB 外部 IP 地址 - Kubernetes LoadBalancer service not getting ELB external IP address 使用一致的IP地址从Kube.netes访问外部服务 - Access external service from Kubernetes with consistent IP address 无法访问 Kube.netes Service IP 地址的工作负载 - Unable to access workload at the IP address of the Kubernetes Service 如何使用 JSON 服务帐户密钥在 GO 中获取 Kubernetes 客户端集? - How can I get a Kubernetes clientset in GO using a JSON service account key? 如何在我的 GCP 组织中获取所有外部 IP 地址? - How can I get all External IP Addresses in my GCP organization? 如何在 MS Graph API 中从 Java SDK 获取附件资源的原始内容? - How do I get the raw content of attachment resources from Java SDK in MS Graph API? 如何在运行时从 Go 代码获取 pod 外部 IP - How can I get pod external IP from Go code at runtime 如何使用 ingress-nginx controller 在 Google Kube.netes Engine (GKE) 上向外部公开 UDP 服务? - How do I expose a UDP service externally on Google Kubernetes Engine (GKE) using the ingress-nginx controller? 虽然解决了外部 ip,但网站在 kube.netes GKE 中返回连接超时 - Though external ip is resolved, the website returns connection timedout in kubernetes GKE 如何让任务作为 ECS 服务的一部分运行? - How do I get the tasks running as part of an ECS service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM