Kubernetes how to access a service from another namespace
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Kubernetes, services are cluster-scoped network endpoints even though their names are namespaced. That means a pod in one namespace can access a service in another namespace as long as DNS, routing, and network policy allow it. The main trick is using the correct service DNS name instead of assuming short names resolve across namespaces automatically.
Use the Service Name with the Target Namespace
Within the same namespace, a pod can often reach a service by short name alone. Across namespaces, you should include the namespace in the DNS name.
That usually works because Kubernetes DNS expands it to the fully qualified service name.
If you want the explicit fully qualified form, use:
Both point to the service named database-service in the backend namespace.
Test It from a Pod
A quick way to verify cross-namespace service discovery is running a temporary pod and resolving the name.
If DNS is working, the service name should resolve to the ClusterIP. Then test the actual port with a tool appropriate to the image, such as wget, nc, or curl.
The exact protocol depends on the service, but the service naming pattern stays the same.
Understand What Does Not Cross Namespaces Automatically
Two common assumptions are wrong:
- short service names do not automatically resolve across namespaces
- environment variables for services are not a reliable cross-namespace discovery mechanism
DNS is the standard solution here. It is explicit and works across namespaces without inventing custom discovery logic.
Network Policies Can Still Block Access
Name resolution alone does not guarantee connectivity. If network policies are enabled, the destination namespace may block ingress from the source namespace.
This kind of policy is often the real reason a cross-namespace service "does not work" even though DNS resolution succeeds.
Services Do Not Need to Be External for This
You do not need NodePort, LoadBalancer, or an Ingress just to connect between namespaces inside the cluster. A normal ClusterIP service is exactly the right service type for in-cluster, cross-namespace traffic.
That is a common design mistake. Internal service-to-service traffic should stay internal unless there is a real external access requirement.
Prefer Explicit Hostnames in Config
If an application in namespace frontend depends on a service in namespace backend, write that dependency explicitly in configuration.
That makes the dependency visible and avoids accidental ambiguity if another namespace later defines a service with the same short name.
Common Pitfalls
- Using only the short service name and expecting it to resolve across namespaces.
- Assuming a DNS success means network policy is not involved.
- Exposing an internal dependency through
NodePortorLoadBalancerwhenClusterIPwould be correct. - Relying on service environment variables instead of standard DNS-based discovery.
- Forgetting that the destination service must still listen on the expected port and protocol after DNS resolution succeeds.
Summary
- Access a service in another namespace by using
service.namespaceor the full cluster DNS name. - Cross-namespace service access works with ordinary
ClusterIPservices. - Test both DNS resolution and actual connectivity from a pod.
- Check network policies if name resolution works but traffic still fails.
- Prefer explicit namespace-qualified service names in application configuration.
Related reading
- kubernetes How to deattach from an attached pod
- Kubernetes how to debug CrashLoopBackOff
- Kubernetes How to delete PODs based on age/creation time
- Kubernetes How to ensure one pod gets scheduled on each worker node?
- Kubernetes how to load balance EXTERNAL persistent tcp connections?
- Kubernetes how to make Deployment to update image
- Kubernetes how to make Deployment to update image
- Kubernetes How to pass pipe character in readiness probe command

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.