0
0
Kubernetesdevops~5 mins

Cross-namespace communication in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, applications running in different parts of a Kubernetes cluster need to talk to each other. Cross-namespace communication lets pods in one namespace connect to services in another namespace safely and clearly.
When a frontend app in one namespace needs to get data from a backend service in another namespace.
When you want to separate environments like development and production but still allow some services to communicate.
When multiple teams manage different namespaces but share common services like databases.
When you want to organize your cluster by namespaces but keep some services accessible across them.
Commands
Create a namespace called 'frontend' to isolate frontend resources.
Terminal
kubectl create namespace frontend
Expected OutputExpected
namespace/frontend created
Create a namespace called 'backend' to isolate backend resources.
Terminal
kubectl create namespace backend
Expected OutputExpected
namespace/backend created
Start a simple pod named 'backend-pod' running nginx in the 'backend' namespace.
Terminal
kubectl run backend-pod --image=nginx --restart=Never -n backend
Expected OutputExpected
pod/backend-pod created
-n - Specifies the namespace where the pod will be created
Create a service named 'backend-service' in the 'backend' namespace to expose the backend pod on port 80.
Terminal
kubectl expose pod backend-pod --port=80 --target-port=80 --name=backend-service -n backend
Expected OutputExpected
service/backend-service exposed
-n - Specifies the namespace for the service
Start a pod named 'frontend-pod' in the 'frontend' namespace that will run a sleep command to stay alive for testing.
Terminal
kubectl run frontend-pod --image=busybox --restart=Never -n frontend --command -- sleep 3600
Expected OutputExpected
pod/frontend-pod created
-n - Specifies the namespace for the pod
From the frontend pod, try to access the backend service using its full DNS name including the backend namespace.
Terminal
kubectl exec -n frontend frontend-pod -- wget -qO- http://backend-service.backend.svc.cluster.local
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
-n - Specifies the namespace of the pod to run the command in
Key Concept

Pods can reach services in other namespaces by using the full DNS name including the target namespace.

Common Mistakes
Trying to access a service in another namespace using only the service name without the namespace suffix.
Kubernetes DNS requires the full service name with namespace to resolve services outside the current namespace.
Use the full DNS name like service-name.namespace.svc.cluster.local to reach services in other namespaces.
Not creating the service in the target namespace before trying to access it.
Without a service, pods cannot find or connect to the backend pods easily.
Always create a service in the backend namespace to expose the pods before accessing them.
Summary
Create separate namespaces for frontend and backend to organize resources.
Deploy pods and expose backend pods with a service in the backend namespace.
Access backend services from frontend pods using the full DNS name including the backend namespace.