Can an application from one namespace see an application from another namespace within the cluster?
sobes.tech AI
Answer from AI
Yes, applications in different namespaces can see each other within a cluster.
In Kubernetes, namespaces are logical isolations that help organize resources. They are not a network isolation by default.
Interaction between namespaces is possible through DNS services in Kubernetes.
An application in namespace-a can access a service named my-service in namespace-b via its fully qualified domain name (FQDN):
<service-name>.<namespace-name>.svc.cluster.local
Example: my-service.namespace-b.svc.cluster.local
You can also use a shortened name if DNS in the cluster is configured accordingly, but FQDN is always more reliable for cross-namespace interactions.
# Example Service in namespace-b
apiVersion: v1
kind: Service
metadata:
name: my-service # Service name
namespace: namespace-b # Service namespace
spec:
selector:
app: my-app # Selector for the pods the service targets
ports:
- protocol: TCP
port: 80
targetPort: 8080
# Example Pod in namespace-a accessing a service in namespace-b
apiVersion: v1
kind: Pod
metadata:
name: consumer-pod
namespace: namespace-a # Namespace of the consumer pod
spec:
containers:
- name: consumer-container
image: my-consumer-image
command: ["curl", "my-service.namespace-b.svc.cluster.local"] # Service address in another namespace
It is important to note that network security policies (Network Policies) can be used to restrict interactions between namespaces. By default, in the absence of such policies, interaction is allowed.
Network policies define groups of pods that can interact with each other based on their namespaces and labels.