How does Kubernetes handles network communication between pods and why this is important?
Quick Answer
Kubernetes uses a virtual network for pods, allowing them to communicate as if they were on the same local network. This means services running in different pods can easily interoperate without needing complex networking configurations.
Detailed Answer
Think of your home Wi-Fi network where all devices can talk to each other seamlessly. In Kubernetes, this is similar — pods are like devices on a virtual local area network (LAN). Each pod gets its own IP address and can communicate with any other pod or external services as if they were directly connected via the same router.
Kubernetes achieves this through a mechanism called Service Discovery. When a new pod is created, Kubernetes assigns it an IP address within the cluster's virtual network. Pods can then use these addresses to contact each other, just like how you might ping your neighbor’s Wi-Fi-enabled device from yours. This internal communication is crucial for services that need to interact with one another, such as microservices architecture where different components depend on each other.
Internally, Kubernetes uses a combination of tools and configurations to manage this network. The API Server handles the creation and deletion of pods, and the Kubelet makes sure containers are running correctly within those pods. The CNI (Container Network Interface) plugin manages IP address assignment and routing within the cluster. When you create a Service object in Kubernetes, it essentially creates a load balancer that routes traffic to the correct set of pods based on their labels.
At scale, engineers need to make sure network policies are properly configured to control how pods can communicate with each other and external services. Misconfigurations here can lead to security issues or performance bottlenecks. For instance, overly permissive rules might expose internal services to the internet without proper safeguards.
A non-obvious gotcha is that network traffic between pods often doesn't use standard DNS names but instead uses IP addresses directly due to latency and performance considerations. This means you need to carefully manage your network policies to ensure security while maintaining efficiency.
Code Example
# Example of creating a simple Service for pod communication
apiVersion: v1
kind: Service
metadata:
name: user-auth-service
description: A service that provides authentication and authorisation for the application.
spec:
selector:
app: user-auth
ports:
- protocol: TCP
port: 80
targetPort: 8080Interview Tip
A junior engineer typically says pods communicate over the cluster network, but for a senior role, the interviewer is actually looking for understanding of the flat network model where every pod gets a unique IP, how CNI plugins like Calico or Cilium implement this, how Services abstract pod IPs behind a stable ClusterIP using iptables or eBPF rules, and how DNS resolution via CoreDNS enables service discovery by name. Mentioning NetworkPolicy for traffic control and the difference between east-west and north-south traffic shows networking depth.