如何访问另一个命名空间中的Pod?

huangapple go评论143阅读模式
英文:

How do I access a pod in another namespace?

问题

我们有两个命名空间,分别是namespace1和namespace2。

以下是namespace1中的服务以及它们的服务暴露情况:

  1. [root@console ~]# oc get svc
  2. 名称 类型 集群IP 外部IP 端口(S) 年龄
  3. config-server ClusterIP 172.30.8.152 <none> 8888/TCP 3小时
  4. eureka-server ClusterIP 172.30.120.74 <none> 8761/TCP 3小时
  5. expedia-rapidapi-service ClusterIP 172.30.236.3 <none> 8233/TCP 3小时
  6. travelcodes-service ClusterIP 172.30.14.36 <none> 8084/TCP 3小时
  7. tti-service ClusterIP 172.30.46.212 <none> 8245/TCP 2小时

我可以使用nslookup在任何Pod中查找"travelcodes-service"的集群IP地址:

  1. / $ nslookup travelcodes-service.contents.svc.cluster.local
  2. 名称: travelcodes-service.contents.svc.cluster.local
  3. 地址 1: 172.30.14.36 travelcodes-service.contents.svc.cluster.local

然而,只有当Pod位于namespace1而不是namespace2时,我才能使用curl访问"travelcodes-service":

  1. curl 172.30.14.36:8084/ping

是否需要暴露任何内容以使namespace2中的Pod能够访问namespace1中的"travelcodes-service"?

英文:

We have 2 namespaces, say namespace1 and namespace2.

The following are the services in namespace1 and the services exposed.

  1. [root@console ~]# oc get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. config-server ClusterIP 172.30.8.152 &lt;none&gt; 8888/TCP 3h
  4. eureka-server ClusterIP 172.30.120.74 &lt;none&gt; 8761/TCP 3h
  5. expedia-rapidapi-service ClusterIP 172.30.236.3 &lt;none&gt; 8233/TCP 3h
  6. travelcodes-service ClusterIP 172.30.14.36 &lt;none&gt; 8084/TCP 3h
  7. tti-service ClusterIP 172.30.46.212 &lt;none&gt; 8245/TCP 2h

I can use nslookup lookup the cluster IP in any pod to the service "travelcodes-service"

  1. / $ nslookup travelcodes-service.contents.svc.cluster.local
  2. Name: travelcodes-service.contents.svc.cluster.local
  3. Address 1: 172.30.14.36 travelcodes-service.contents.svc.cluster.local

However, I can only use curl to access travelcodes-service if the pod is in namespace1 but not namespace2

  1. curl 172.30.14.36:8084/ping

Is there anything I need to expose in order to let a pod in namespace2 to access "travelcodes-service" in namespace1?

答案1

得分: 1

你可以通过以下方式访问<service1>在<namespace1>中的服务:

  1. &lt;service1&gt;.&lt;namespace1&gt;

例如,您可以使用以下URL:

  1. http://&lt;service1&gt;.&lt;namespace1&gt;.svc.cluster.local

更多信息请参考:服务和Pod的DNS

要获取所有命名空间的列表:

  1. oc get ns

以及获取一个命名空间中服务的列表:

  1. oc get services -n &lt;namespace-name&gt;
英文:

You can access the service <service1> in <namespace1> with

  1. &lt;service1&gt;.&lt;namespace1&gt;

For example you can use this url:

  1. http://&lt;service1&gt;.&lt;namespace1&gt;.svc.cluster.local

More on that: DNS for Services and Pods

To get a list of all your namespaces:

  1. oc get ns

And for a list of services in one namespace:

  1. oc get services -n &lt;namespace-name&gt;

答案2

得分: 0

默认情况下,Kubernetes 集群中的服务只能在其自己的命名空间内访问。要允许跨命名空间访问服务,您可以使用 Kubernetes 服务 DNS。
要在 namespace2 中的一个 pod 中启用对 namespace1 中的 "travelcodes-service" 的访问:

  1. namespace1 中创建一个类型为 ExternalName 的服务,该服务指向 "travelcodes-service" 的 DNS 名称。
  2. 授予 namespace2 中的 pod 访问跨命名空间服务的适当权限。可以通过在 namespace2 中创建 RoleRoleBinding 来实现。

完成上述步骤后,namespace2 中的 pod 应该能够使用其 DNS 名称访问 namespace1 中的 "travelcodes-service"。

英文:

By default, services in a Kubernetes cluster are only accessible within their own namespace. To allow access to a service across namespaces, you can use Kubernetes Service DNS.
To enable access to the "travelcodes-service" in namespace1 from a pod in namespace2:

  1. Create a Service in namespace1 of type ExternalName that points to the DNS name of the "travelcodes-service" in namespace1.
  2. Grant appropriate permissions to the pod in namespace2 to access services across namespaces. This can be done by creating a Role and RoleBinding in namespace2.

After completing the above steps, the pod in namespace2 should be able to access the "travelcodes-service" in namespace1 using its DNS name.

huangapple
  • 本文由 发表于 2023年6月27日 17:40:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定