How do I access a pod in another namespace?

1.3k Views Asked by At

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?

2

There are 2 best solutions below

0
jsanchez On

You can access the service in 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
NubDev On

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.