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 nam
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
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.
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.
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.