Kubernetes
Namespace
Cross-Namespace Access
Kubernetes Services
Kubernetes Configuration

Kubernetes: access a service in another namespace

Codemia Knowledge Hub ·

To reach a service in another namespace, use its namespace-qualified DNS name, such as database-service.backend or database-service.backend.svc.cluster.local. Service names are namespaced, but the services themselves are reachable from any namespace as long as DNS, routing and network policy allow it. Short names do not resolve across namespaces.

Use the service name with the target namespace

Inside the same namespace, a pod can usually reach a service by its short name. Across namespaces, add the namespace:

text
database-service.backend

Kubernetes DNS expands this to the fully qualified service name. The explicit fully qualified form is:

text
database-service.backend.svc.cluster.local

Both point to the service database-service in the backend namespace.

Test it from a pod

Run a temporary pod and resolve the name:

bash
1kubectl run test-client \
2  --rm -it \
3  --image=busybox:1.36 \
4  --restart=Never \
5  -- nslookup database-service.backend.svc.cluster.local

If DNS works, the name resolves to the ClusterIP. Then test the actual port with a tool available in the image, such as wget, nc or curl:

bash
1kubectl run curl-test \
2  --rm -it \
3  --image=curlimages/curl:8.7.1 \
4  --restart=Never \
5  -- curl http://database-service.backend:5432

The protocol depends on the service, but the naming pattern is the same.

What does not cross namespaces automatically

Two common assumptions are wrong:

  • short service names do not resolve across namespaces
  • service environment variables are not a reliable cross-namespace discovery mechanism

DNS is the standard answer. It is explicit and works across namespaces without custom discovery logic.

Network policies can still block access

Name resolution does not guarantee connectivity. With network policies enabled, the destination namespace may block ingress from the source namespace:

yaml
1apiVersion: networking.k8s.io/v1
2kind: NetworkPolicy
3metadata:
4  name: allow-frontend-to-backend
5  namespace: backend
6spec:
7  podSelector:
8    matchLabels:
9      app: database
10  policyTypes:
11    - Ingress
12  ingress:
13    - from:
14        - namespaceSelector:
15            matchLabels:
16              kubernetes.io/metadata.name: frontend

A policy like this is often the real reason a cross-namespace service "does not work" even though DNS resolves. Also confirm that the destination service listens on the expected port and protocol.

You do not need an external service type

NodePort, LoadBalancer or an Ingress are not needed to connect between namespaces inside the cluster. A normal ClusterIP service is the right type for in-cluster, cross-namespace traffic. Exposing an internal dependency externally is a common design mistake; keep service-to-service traffic internal unless you have a real external access requirement.

Put the qualified hostname in config

If an application in frontend depends on a service in backend, write the dependency explicitly:

yaml
env:
  - name: DATABASE_HOST
    value: database-service.backend.svc.cluster.local

This makes the dependency visible and avoids ambiguity if another namespace later defines a service with the same short name.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design