<p>How does cross namespace communication across pods in kubernetes? Say webserver &amp; application pod is in namespace A and DB in namespace B. I have created External Name as well but still doesn't work.</p>
<p>Can we have multiple selectors in the deployments.yaml</p>
<pre><code>frontend-service

apiVersion: v1
kind: Service
metadata:
  name: mongo-express-service
  namespace: db
spec:
  type: NodePort
  selector:
    app: mongo-express
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
</code></pre>
<pre><code>DB-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  namespace: db
spec:
  type: ExternalName
  externalName: mongo-express-service.frontend.svc.cluster.local
  selector:
    app: mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017
</code></pre>
<pre><code>$ kubectl get svc -n db
NAME                    TYPE           CLUSTER-IP     EXTERNAL-IP                                        PORT(S)          AGE
mongo-express-service   NodePort       10.103.8.140   &lt;none&gt;                                             8081:32468/TCP   5h20m
mongodb-service         ExternalName   &lt;none&gt;         mongo-express-service.frontend.svc.cluster.local   27017/TCP        5h19m
</code></pre>
<pre><code>$ kubectl get svc -n frontend
NAME                    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mongo-express-service   NodePort   10.102.174.70   &lt;none&gt;        8081:30928/TCP   5h20m
</code></pre>

bhai
<p>you should create a network policy:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: simple-policy
  namespace: db
spec:
  podSelector:
    matchLabels:
      app: mongo-db
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: frontend
    - podSelector:
        matchLabels:
          app: mongo-express
    ports:
    - protocol: TCP
      port: 27017
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          name: frontend
    - podSelector:
        matchLabels:
          app: mongodb-express
    ports:
    - protocol: TCP
      port: 27017
</code></pre>
<p>you can slim it down by the restrictions you like whether is the entire namespace, just two pods and with a specific port or all of them.</p>
<p>check out more in the <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/" rel="nofollow noreferrer">docs</a></p>
