英文:
How do I access a pod in another namespace?
问题
我们有两个命名空间,分别是namespace1和namespace2。
以下是namespace1中的服务以及它们的服务暴露情况:
[root@console ~]# oc get svc
名称 类型 集群IP 外部IP 端口(S) 年龄
config-server ClusterIP 172.30.8.152 <none> 8888/TCP 3小时
eureka-server ClusterIP 172.30.120.74 <none> 8761/TCP 3小时
expedia-rapidapi-service ClusterIP 172.30.236.3 <none> 8233/TCP 3小时
travelcodes-service ClusterIP 172.30.14.36 <none> 8084/TCP 3小时
tti-service ClusterIP 172.30.46.212 <none> 8245/TCP 2小时
我可以使用nslookup在任何Pod中查找"travelcodes-service"的集群IP地址:
/ $ nslookup travelcodes-service.contents.svc.cluster.local
名称: travelcodes-service.contents.svc.cluster.local
地址 1: 172.30.14.36 travelcodes-service.contents.svc.cluster.local
然而,只有当Pod位于namespace1而不是namespace2时,我才能使用curl访问"travelcodes-service":
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.
[root@console ~]# oc get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
config-server ClusterIP 172.30.8.152 <none> 8888/TCP 3h
eureka-server ClusterIP 172.30.120.74 <none> 8761/TCP 3h
expedia-rapidapi-service ClusterIP 172.30.236.3 <none> 8233/TCP 3h
travelcodes-service ClusterIP 172.30.14.36 <none> 8084/TCP 3h
tti-service ClusterIP 172.30.46.212 <none> 8245/TCP 2h
I can use nslookup lookup the cluster IP in any pod to the service "travelcodes-service"
/ $ nslookup travelcodes-service.contents.svc.cluster.local
Name: travelcodes-service.contents.svc.cluster.local
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
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>中的服务:
<service1>.<namespace1>
例如,您可以使用以下URL:
http://<service1>.<namespace1>.svc.cluster.local
更多信息请参考:服务和Pod的DNS
要获取所有命名空间的列表:
oc get ns
以及获取一个命名空间中服务的列表:
oc get services -n <namespace-name>
英文:
You can access the service <service1> in <namespace1> with
<service1>.<namespace1>
For example you can use this url:
http://<service1>.<namespace1>.svc.cluster.local
More on that: DNS for Services and Pods
To get a list of all your namespaces:
oc get ns
And for a list of services in one namespace:
oc get services -n <namespace-name>
答案2
得分: 0
默认情况下,Kubernetes 集群中的服务只能在其自己的命名空间内访问。要允许跨命名空间访问服务,您可以使用 Kubernetes 服务 DNS。
要在 namespace2 中的一个 pod 中启用对 namespace1 中的 "travelcodes-service" 的访问:
- 在
namespace1中创建一个类型为 ExternalName 的服务,该服务指向 "travelcodes-service" 的 DNS 名称。 - 授予
namespace2中的 pod 访问跨命名空间服务的适当权限。可以通过在namespace2中创建Role和RoleBinding来实现。
完成上述步骤后,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:
- Create a Service in namespace1 of type ExternalName that points to the DNS name of the "travelcodes-service" in
namespace1. - Grant appropriate permissions to the pod in
namespace2to access services across namespaces. This can be done by creating aRole and RoleBindinginnamespace2.
After completing the above steps, the pod in namespace2 should be able to access the "travelcodes-service" in namespace1 using its DNS name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论