Is it possible to create a Kubernetes service and pod in different namespaces, for example, having myweb-svc pointing to the actual running myweb-pod, while myweb-svc and myweb-pod are in different namespaces?
-
https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors – zerkms Feb 20 '23 at 08:25
-
Request routing will not take place for service and pod being in different namespace with plain k8s service and pods object. – Nataraj Medayhal Feb 20 '23 at 08:25
2 Answers
YAML manifest to create both the pod and the service in their respective namespaces. You need to specify the ‘namespace’ field in the ‘metadata’ section of both the ‘pod’ and ‘service’ objects to specify the namespace in which they should be created.
Also, if you want to point your Service to a Service in a different namespace or on another cluster you can use service without a pod selector.
Refer to this link on Understanding kubernetes Object for more information.

- 2,123
- 3
- 11
Kubernetes API objects that are connected together at the API layer generally need to be in the same namespace. So a Service can only connect to Pods in its own namespace; if a Pod references a ConfigMap or a Secret or a PersistentVolumeClaim, those need to be in the same namespace as well.

- 130,717
- 29
- 175
- 215