提问者:小点点

如何在kubernetes中找到服务的url?


我在本地docker桌面上有一个本地kubernetes集群。

这是我做kubectl描述服务时我的kubernetes服务的样子

Name:              helloworldsvc
Namespace:         test
Labels:            app=helloworldsvc
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"helloworldsvc"},"name":"helloworldsvc","namespace":"test...
Selector:          app=helloworldapp
Type:              ClusterIP
IP:                10.108.182.240
Port:              http  9111/TCP
TargetPort:        80/TCP
Endpoints:         10.1.0.28:80
Session Affinity:  None
Events:            <none>

此服务指向使用Web应用程序的部署。

我的问题如何找到这个服务的网址?我已经试过http://localhost:9111/,没有工作。

我验证了此服务指向的pod已启动并正在运行。


共3个答案

匿名用户

服务URL格式如下:

<service-name>.<namespace>.svc.cluster.local:<service-port>

在您的情况下,它是:

helloworldsvc.test.svc.cluster.local:9111

匿名用户

获取服务名称:kubectl获取服务-n test

URLkubernetes服务的是service-name. namesspace.svc.club.local:service-port,其中group.local是kubernetes集群名称。

获取集群名称:kubectl config get-context ts|awk{'print 2$'}

URL服务在您的情况下将helloworld svc. test.svc.club.local:9111

您尝试的方式将无法在您的localhost上使其可用,您需要在nodeport或使用port-转发或使用kubectl代理上提供服务。

但是,如果您不想要节点端口并检查容器内是否一切正常,则按照以下步骤进入容器内是否有shell。

kubectl exec-it容器名-n its-namesspace-name sh

然后做一个

curllocalhost:80curl helloworld svc. test.svc.club.local:9111curl10.1.0.28:80

但是这两个curl命令只能在库伯内特斯pod中工作,而不能在你的localhost机器上工作。

要在您的主机上访问kubectl端口转发svc/helloworldsvc 80:9111-n test

匿名用户

您创建的服务是ClusterIP类型,只能从集群内部访问。您有两种方法可以从桌面访问它:

>

  • 创建一个nodeport类型的服务,然后通过nodeip: nodeport访问它

    使用Kubectl端口转发,然后通过localhost访问它:转发端口