Kubernetes Networking

Kubernetes Network Model

Kubernetes implements a flat network model where every pod can communicate with every other pod without Network Address Translation (NAT). This simplifies networking but requires proper configuration.

Why this matters: Container networking is complex. Kubernetes abstracts this complexity by providing a consistent network model, enabling predictable service-to-service communication regardless of where pods run.


Network Architecture

graph TD A["Pod Network"] -->B["Every Pod
Unique IP"] B -->C["Pod-to-Pod
Direct Communication"] A -->D["Service Network
Virtual IPs"] D -->E["Service DNS
Name Resolution"] C -->C1["No NAT
Default"] style B fill:#e1f5ff style D fill:#fff3e0

Pod Networking

Pod IP Assignment

Each pod receives a unique IP address across the cluster. Containers within a pod share the network namespace (localhost communication).

# View pod IPs kubectl get pods -o wide # Shows INTERNAL-IP for each pod

Container Networking in Pod

Containers in same pod communicate via localhost:

apiVersion: v1 kind: Pod metadata: name: multi-container spec: containers: - name: app image: myapp:latest ports: - containerPort: 8080 - name: sidecar image: sidecar:latest ports: - containerPort: 9000

From app container: curl localhost:9000 reaches sidecar.


Service Networking

Services provide stable DNS names and IP addresses that load balance across pods.

ClusterIP Service

Internal service with stable cluster IP:

apiVersion: v1 kind: Service metadata: name: api spec: selector: app: api ports: - port: 80 targetPort: 8080 clusterIP: 10.0.0.10 # Stable IP

DNS resolves to ClusterIP:

# Query DNS from pod nslookup api # Address: 10.0.0.10 # Traffic to api:80 load balances across api pods on port 8080

Endpoint Tracking

Services automatically track pod IPs via Endpoints:

kubectl get endpoints api # NAME ENDPOINTS # api 10.0.1.5:8080, 10.0.2.3:8080, 10.0.3.1:8080 # Endpoints update automatically when pods are added/removed

Network Policies

By default, all pods can communicate with all pods. NetworkPolicies enforce ingress/egress restrictions.

Why NetworkPolicies matter: Production security requires isolation. NetworkPolicies prevent unauthorized pod-to-pod communication.

Deny All Ingress

apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all spec: podSelector: {} # All pods policyTypes: - Ingress

No ingress traffic allowed to any pod.

Allow Specific Ingress

apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-api-traffic spec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: app: web ports: - protocol: TCP port: 8080

Only web pods can send traffic to api pods on port 8080.

Egress Control

apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-dns-egress spec: podSelector: matchLabels: app: app policyTypes: - Egress egress: - to: - podSelector: matchLabels: app: dns ports: - protocol: UDP port: 53

Allow app pods to query DNS only.


CNI Plugins

Kubernetes doesn't implement networking itself. Container Network Interface (CNI) plugins implement pod networking.

Plugin Features Use Case
Calico Network policies, performance Secure, high-performance
Flannel Simple overlay network Development, simplicity
Weave Encryption, DNS Secure multi-cluster
Cilium eBPF-based, advanced policies High-security, performance

Why CNI plugins: Different deployments have different networking requirements (performance, security, complexity).


Service Mesh (Advanced Networking)

Service meshes (Istio, Linkerd) provide advanced networking features: traffic management, security policies, observability.

graph LR A["Application Pods"] -->B["Service Mesh
Sidecar Proxies"] B -->C["Centralized Control
Traffic Management"] C -->D["Features:
Routing, Security
Metrics, Tracing"] style C fill:#e1f5ff style D fill:#fff3e0

Network Troubleshooting

Connectivity Tests

# From within pod, test DNS resolution kubectl exec -it POD_NAME -- nslookup SERVICE_NAME # Test connectivity kubectl exec -it POD_NAME -- curl SERVICE_NAME:PORT # View DNS logs kubectl logs -n kube-system -l k8s-app=kube-dns

Network Policy Debugging

# View applied policies kubectl get networkpolicies # Describe policy kubectl describe networkpolicy POLICY_NAME # Test connectivity after applying policy kubectl run test --image=busybox -it -- sh # From inside: wget api:80

Common Pitfalls


Key Takeaways

Next Steps: Implement NetworkPolicies for pod isolation, test DNS resolution, verify endpoint tracking behavior.