154

I have been trying to find a way to define a service in one namespace that links to a Pod running in another namespace. I know that containers in a Pod running in namespaceA can access serviceX defined in namespaceB by referencing it in the cluster DNS as serviceX.namespaceB.svc.cluster.local, but I would rather not have the code inside the container need to know about the location of serviceX. That is, I want the code to just lookup serviceX and then be able to access it.

The Kubernetes documentation suggests that this is possible. It says that one of the reasons that you would define a service without a selector is that You want to point your service to a service in another Namespace or on another cluster.

That suggests to me that I should:

  1. Define a serviceX service in namespaceA, without a selector (since the POD I want to select isn't in namespaceA).
  2. Define a service (which I also called serviceX) in namespaceB, and then
  3. Define an Endpoints object in namespaceA to point to serviceX in namespaceB.

It is this third step that I have not been able to accomplish.

First, I tried defining the Endpoints object this way:

kind: Endpoints
apiVersion: v1
metadata:
  name: serviceX
  namespace: namespaceA
subsets:
  - addresses:
      - targetRef:
          kind: Service
          namespace: namespaceB
          name: serviceX
          apiVersion: v1
    ports:
      - name: http
        port: 3000

That seemed the logical approach, and obviously what the targetRef was for. But, this led to an error saying that the ip field in the addresses array was mandatory. So, my next try was to assign a fixed ClusterIP address to serviceX in namespaceB, and put that in the IP field (note that the service_cluster_ip_range is configured as 192.168.0.0/16, and 192.168.1.1 was assigned as the ClusterIP for serviceX in namespaceB; serviceX in namespaceA was auto assigned a different ClusterIP on the 192.168.0.0/16 subnet):

kind: Endpoints
apiVersion: v1
metadata:
  name: serviceX
  namespace: namespaceA
subsets:
  - addresses:
        - ip: 192.168.1.1
          targetRef:
            kind: Service
            namespace: namespaceB
            name: serviceX
            apiVersion: v1
    ports:
      - name: http
        port: 3000

That was accepted, but accesses to serviceX in namespaceA did not get forwarded to the Pod in namespaceB - they timed out. Looking at the iptables setup, it looks like it would have had to do NAT pre-routing twice to accomplish that.

The only thing I did find that worked - but is not a satisfactory solution - is to lookup the actual IP address of the Pod providing serviceX in namespaceB and put that address in the Endpoints object in namespaceA. That isn't satisfactory, of course, because the Pod IP address may change over time. That's the problem service IPs are there to solve.

So, is there a way to meet what seems to be the promise of the documentation that I can point a service in one namespace to a service running in a different namespace?

A commenter questioned why you would want to do this - here is a use case that makes sense to me, at least:

Say you have a multi-tenant system, which also includes a common data-access function that can be shared between tenants. Now imagine that there are different flavors of this data-access function with common APIs, but different performance characteristics. Some tenants get access to one of them, other tenants have access to another one.

Each tenant's pods run in their own namespaces, but each one needs to access one of these common data-access services, which will necessarily be in another namespace (since it is accessed by multiple tenants). But, you wouldn't want the tenant to have to change their code if their subscription changes to access the higher-performing service.

A potential solution (the cleanest one I can think of, if only it worked) is to include a service definition in each tenant's namespace for the data-access service, with each one configured for the appropriate endpoint. This service definition would be configured to point to the proper data-access service each tenant is entitled to use.

burubum
  • 540
  • 1
  • 5
  • 15
David McKinley
  • 1,833
  • 2
  • 9
  • 10
  • the point of namespaces is to isolate, so i think if you need to go across namespaces you need to know at least where it is located! – MrE May 16 '16 at 23:10
  • So, what does the documentation mean when it suggests you can direct a service defined in one namespace to access a service in a different namespace by not defining a selector - and by implication defining an endpoint? There are certainly valid use cases for this - one of which I added to the question. Is the documentation just misleading, or is there a way to do it that I have not yet figured out? – David McKinley May 17 '16 at 02:15
  • i'm not sure, sorry. what i know is that i access services in multiple namespaces using their fqdn. I do this especially with vpn, since i have 1 vpn pod and i connect through all services from it. however you need to know the namespace and provide fqdn. i would suggest you ask on the slack channel. – MrE May 17 '16 at 02:22
  • Using fqdn is the solution I'm currently using. My use case would be better served, though, (now added to question) if that wasn't necessary. – David McKinley May 17 '16 at 02:28
  • I also wonder what the documentation is referring too, however I can use fqdn as a satisfactory solution for my use case. – Vincent De Smet Aug 01 '16 at 07:03
  • I need to do the exact opposite! Prevent namespace traversal. – Jonathan Sep 29 '16 at 04:59
  • I found a working video : https://youtu.be/TikEgvwhdJ8 – anish Sep 04 '19 at 04:39

4 Answers4

305

I stumbled over the same issue and found a nice solution which does not need any static ip configuration:

You can access a service via it's DNS name (as mentioned by you): servicename.namespace.svc.cluster.local

You can use that DNS name to reference it in another namespace via a local service:

kind: Service
apiVersion: v1
metadata:
  name: service-y
  namespace: namespace-a
spec:
  type: ExternalName
  externalName: service-x.namespace-b.svc.cluster.local
  ports:
  - port: 80
Paul
  • 3,533
  • 1
  • 10
  • 14
  • 2
    This is a great solution! I'm not sure if the "ExternalName" type was available for services when I originally asked the question, but it is supported now, and neatly solves the problem. Thanks, Paul. – David McKinley Jun 03 '17 at 16:43
  • 1
    Does this work? i doubt. can anyone confirm if this really worked, does not work for me. – debianmaster Sep 17 '17 at 05:28
  • 4
    Yes it does. It works for one pod to talk to a service in another namespace, but not for an ingress loadbalancer. – Paul Sep 20 '17 at 07:10
  • because of [fix kubernetes in-cluster CNAME lookup](https://github.com/coredns/coredns/pull/2040), old version maybe not work. – 赵浩翔 Nov 16 '18 at 03:21
  • 1
    Would/should this work for services in the kube-system namespace too? – Nabheet Jan 15 '19 at 06:21
  • Due to [this](https://github.com/coredns/coredns/issues/2038) bug it does not work in kubernetes 1.11.5. – siom May 28 '19 at 12:49
  • *Note:* this solution only worked for us in case where the names of service-x and service-y were equal! – schrobe Nov 25 '19 at 10:40
  • Does this solution work with kubernetes discovery service? https://stackoverflow.com/questions/59157447/kubernetes-services-discovery-cross-namespace?noredirect=1#comment104542442_59157447 – guilhermecgs Dec 03 '19 at 14:50
20

It is so simple to do it

if you want to use it as host and want to resolve it

If you are using ambassador to any other API gateway for service located in another namespace it's always suggested to use :

            Use : <service name>
            Use : <service.name>.<namespace name>
            Not : <service.name>.<namespace name>.svc.cluster.local

it will be like : servicename.namespacename.svc.cluster.local

this will send request to a particular service inside the namespace you have mention.

example:

kind: Service
apiVersion: v1
metadata:
  name: service
spec:
  type: ExternalName
  externalName: <servicename>.<namespace>.svc.cluster.local

Here replace the <servicename> and <namespace> with the appropriate value.

In Kubernetes, namespaces are used to create virtual environment but all are connect with each other.

WSMathias9
  • 478
  • 5
  • 10
Harsh Manvar
  • 7,682
  • 2
  • 22
  • 48
  • 7
    Could you explain how this answer is any different from Paul's provided almost 2 years earlier? – Oliver Jul 09 '19 at 18:03
  • 2
    @Oliver there is no difference but i have just specified what to replace service name and namespace at which particular place. while he has used namespace-a so looks confusing to me. – Harsh Manvar Jul 10 '19 at 03:32
  • 11
    A handy trick on SO is to add a comment on the answer and make the necessary clarification. – Oliver Jul 12 '19 at 04:50
  • 4
    I would call this as the best solution because `.svc.cluster.local` is by default supported for resolving the service internally. – DrKNa Jun 02 '20 at 09:39
11

To access services in two different namespaces you can use url like this:

HTTP://<your-service-name>.<namespace-with-that-service>.svc.cluster.local

To list out all your namespaces you can use:

kubectl get namespace

And for service in that namespace you can simply use:

kubectl get services -n <namespace-name>

this will help you.

desmondev
  • 37
  • 1
  • 9
Devesh
  • 619
  • 8
  • 9
0

You can achieve this by deploying something at a higher layer than namespaced Services, like the service loadbalancer https://github.com/kubernetes/contrib/tree/master/service-loadbalancer. If you want to restrict it to a single namespace, use "--namespace=ns" argument (it defaults to all namespaces: https://github.com/kubernetes/contrib/blob/master/service-loadbalancer/service_loadbalancer.go#L715). This works well for L7, but is a little messy for L4.

Prashanth B
  • 3,981
  • 17
  • 13