Linking Pods from Different Namespaces with a Kubernetes Service

29 Views Asked by At

I have two identical Pods running in different namespaces, let's call them namespace testa and namespace testb. Additionally, I have a Service deployed in namespace testb. My goal is to configure this Service in namespace testb so that it can route traffic to both Pods in namespaces testa and testb. How can I achieve this cross-namespace communication using Kubernetes Services?

I tried things like headless service

apiVersion: v1
kind: Pod
metadata:
  name: pod-a
  namespace: testa
  labels:
    app: myapp
spec:
  containers:
  - name: my-container
    image: nginx
apiVersion: v1
kind: Pod
metadata:
  name: pod-b
  namespace: testb
  labels:
    app: myapp
spec:
  containers:
  - name: my-container
    image: nginx
apiVersion: v1
kind: Service
metadata:
  name: cross-namespace-service
  namespace: testb
spec:
  clusterIP: None
  selector:
    app: myapp

But it didn't worked. I can see only pod-b linked with this service as the service is created in testb namespace. I also tried using EndpointSlice and Endpoints as well but it isn't working as expected

what I need is when I look for the Pods associated with the service present in namespace testb I should be able to see both pod-a and pod-b listed. Is this even possible. I don't want to use thing like ingress for this experiment

0

There are 0 best solutions below