Home > Mobile >  Kubernetes Service for selecting pods in two namespaces
Kubernetes Service for selecting pods in two namespaces

Time:01-13

I have a service "A" deployed in "X" namespace. "Z" service from "P" namespace, calls it on
svc-a.x.svc.cluster.local
I have to deploy staging of service "A" in Y namespace as well and I want to register these IPs under
svc-a.x.svc.cluster.local
Is there any way to do it? I want to the main service to select pods from different namespaces.

CodePudding user response:

You can try using a Service without selectors with an EndPointSlice which refers to a Service from each namespace.

Create svc-a in namespace X which selects / points to pods in namespace X. The Service will be available at svc-a.x.svc.cluster.local.

Create svc-a in namespace Y which selects / points to pods in namespace Y. The Service will be available at svc-a.y.svc.cluster.local.

Create a svc-a in namespace Z without selectors.

apiVersion: v1
kind: Service
metadata:
  name: svc-a
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

The Service will be available at svc-a.z.svc.cluster.local.

Create an EndpointSlice in namespace Z with svc-a.x.svc.cluster.local and svc-a.y.svc.cluster.local as endpoints and attach it to svc-a:

apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
  name: svc-a
  labels:
    kubernetes.io/service-name: svc-a
addressType: FQDN
ports:
  - name: http
    protocol: TCP
    port: 80
endpoints:
  - addresses:
      - "svc-a.x.svc.cluster.local"
      - "svc-a.y.svc.cluster.local"

So now you'll have svc-a.z.svc.cluster.local available in any namespace pointing to backends in both the X and Y namespaces.

CodePudding user response:

You can use the ExternalName service type for this purpose. You can create new service which selects the pods from 'Y' namespace and use that service in 'A' by using the externalName.

Example: Assume new service name is 'svc-b' which is in Y namespace you can modify your 'A' service something like this

apiVersion: v1
kind: Service
metadata:
  name: svc-a
  namespace: X
spec:
  type: ExternalName
  externalName: svc-b.y.svc.cluster.local
  ports:
  - port: 80

Refer to this SO which helped in solving a similar issue.

  • Related