0
0
KubernetesHow-ToBeginner · 4 min read

How Pods Communicate in Kubernetes: Networking Basics

In Kubernetes, pods communicate with each other using an internal network where each pod gets its own IP address. Communication happens directly via these IPs or through Services that provide stable endpoints and load balancing within the cluster.
📐

Syntax

Kubernetes networking allows pods to communicate using their IP addresses or through Services. The key parts are:

  • Pod IP: Unique IP assigned to each pod.
  • Service: A stable network endpoint that groups pods and load balances traffic.
  • Cluster DNS: Resolves service names to IPs for easy access.
yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
💻

Example

This example shows two pods communicating via a Service. Pod A sends a request to Pod B using the Service's DNS name.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-a
  labels:
    app: client
spec:
  containers:
  - name: client
    image: busybox
    command: ['sh', '-c', 'wget -qO- http://my-service']
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-b
  labels:
    app: my-app
spec:
  containers:
  - name: server
    image: nginx
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
Output
Connecting to my-service... <html>...nginx default page...</html>
⚠️

Common Pitfalls

Common mistakes when pods communicate include:

  • Using pod IPs directly, which can change if pods restart.
  • Not using Services, causing unstable connections.
  • Ignoring network policies that block traffic.

Always use Services and DNS names for reliable communication.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-client
spec:
  containers:
  - name: client
    image: busybox
    command: ['sh', '-c', 'wget -qO- http://10.244.1.5']  # Direct pod IP, not recommended

# Correct way:
# Use Service DNS name instead:
# command: ['sh', '-c', 'wget -qO- http://my-service']
📊

Quick Reference

ConceptDescription
Pod IPUnique IP for each pod, changes on restart
ServiceStable endpoint to access pods by label selector
Cluster DNSResolves service names to IPs inside cluster
Network PoliciesRules to allow or block pod communication

Key Takeaways

Pods communicate using unique IPs assigned within the cluster network.
Use Services to provide stable, load-balanced access to pods.
Access pods via Service DNS names, not direct pod IPs.
Network policies can restrict pod communication and must be configured carefully.
Kubernetes cluster DNS resolves service names for easy pod-to-pod communication.