Services & Networking
Networking in Kubernetes is about connecting pods, enabling service discovery, routing traffic, and securing communication. Unlike traditional networking where you configure IP addresses and routes manually, Kubernetes networking is declarative and automated. Think of it as a smart postal system: you specify where mail should go (Services), and Kubernetes handles the routing, even as pods move around or scale up and down.
The Kubernetes Networking Model
Kubernetes networking follows a simple but powerful model: every pod gets its own IP address and can communicate with every other pod without Network Address Translation (NAT). This flat network model simplifies application design and enables service discovery.
Core Networking Concepts
Pod Networking
Every pod in Kubernetes has its own IP address from the pod CIDR range. Pods can communicate directly with each other using these IPs, but since pod IPs are ephemeral (they change when pods restart), you typically use Services for stable communication.
Services
Services provide stable network endpoints for pods. They solve the fundamental problem that pod IPs are temporary—when a pod restarts, it gets a new IP, but the Service IP and DNS name remain constant. Services also provide load balancing across multiple pod instances.
CNI (Container Network Interface)
CNI plugins implement the actual networking. They configure pod networking, set up routes, and manage IP address allocation. Popular CNI plugins include Calico, Cilium, and Flannel.
DNS & Service Discovery
Kubernetes provides built-in DNS (CoreDNS) that automatically creates DNS records for Services. This enables service discovery by name instead of IP addresses.
Ingress
Ingress provides HTTP/HTTPS routing to Services based on hostnames and paths. It operates at Layer 7 (application layer) and provides features like SSL/TLS termination and path-based routing.
Network Policies
Network Policies act as firewalls for pods, controlling which pods can communicate with each other. They enable micro-segmentation and defense in depth.
Service Meshes
Service meshes provide advanced networking features like mutual TLS, traffic management, observability, and policy enforcement. They work alongside Services to add capabilities like circuit breaking, retries, and distributed tracing.
Networking Layers
Kubernetes networking operates at multiple layers:
Layer 7 (Application) - Service meshes and Ingress handle HTTP/HTTPS routing, SSL termination, and advanced traffic management
Layer 4 (Transport) - Services provide TCP/UDP load balancing and service discovery
Layer 3 (Network) - CNI plugins handle IP addressing, routing, and pod-to-pod communication
Security - Network Policies control which pods can communicate, adding firewall-like rules
Service Discovery Flow
Understanding how services discover each other:
Application makes DNS query - Pod queries for
my-serviceormy-service.namespace.svc.cluster.localCoreDNS resolves - CoreDNS returns the Service IP address
Service routes traffic - Service load balances across backend pods matching its selector
Pod receives request - One of the backend pods handles the request
Service Types
Kubernetes provides different Service types for different access patterns:
ClusterIP - Default type, accessible only within the cluster. Used for internal service-to-service communication.
NodePort - Exposes Service on each node’s IP at a static port. External clients access using <NodeIP>:<NodePort>.
LoadBalancer - Automatically provisions an external load balancer (cloud provider specific). Simplest way to expose Services externally in cloud environments.
Headless - Service without a cluster IP. Returns individual pod IPs via DNS. Used for StatefulSets and custom service discovery.
Ingress & Gateway API
For HTTP/HTTPS traffic, Kubernetes provides two approaches:
Ingress - Traditional approach using Ingress resources and controllers. Provides host-based and path-based routing, SSL/TLS termination.
Gateway API - Modern, more expressive API for service networking. Provides better separation of concerns and more features than Ingress.
Network Policies
Network Policies provide pod-level firewalls, controlling traffic flow:
Network Policies enable:
- Micro-segmentation - Isolate workloads from each other
- Defense in depth - Additional security layer beyond RBAC
- Compliance - Meet regulatory requirements for network isolation
- Zero-trust networking - Default deny, explicit allow
Service Meshes
Service meshes add advanced networking capabilities:
Popular service meshes include Istio, Linkerd, and Consul. They work by injecting sidecar proxies into pods that intercept and manage traffic.
Networking Lifecycle
Understanding how networking components work together:
Common Networking Patterns
Internal Service Communication
Most communication happens within the cluster:
- Services provide stable endpoints
- DNS enables service discovery
- Load balancing distributes traffic
External Access
Exposing applications outside the cluster:
- Ingress - HTTP/HTTPS routing (recommended for web traffic)
- LoadBalancer - Direct external access (simpler, one IP per Service)
- NodePort - Access via node IPs (development or bare metal)
Multi-Cluster Networking
Connecting multiple clusters:
- Service meshes can span clusters
- Gateway API supports multi-cluster
- VPN or cloud networking connects clusters
Best Practices
Use Services, not pod IPs - Always use Services for stable endpoints
Choose the right Service type - ClusterIP for internal, LoadBalancer for external cloud, Ingress for HTTP/HTTPS
Use DNS names - Prefer DNS names over IP addresses for service discovery
Implement Network Policies - Start with default deny, then explicitly allow needed traffic
Use Ingress for HTTP/HTTPS - More cost-effective than LoadBalancer for multiple Services
Consider service meshes - For advanced features like mTLS, traffic management, and observability
Monitor network performance - Track latency, throughput, and error rates
Document network architecture - Keep diagrams and documentation of your networking setup
Test network policies - Verify Network Policies work as expected before production
Plan for scale - Consider network performance and limits as you scale
Topics
Core Networking
- Pod Connectivity - How pods communicate with each other
- CNI Plugins - Container Network Interface implementations
Services
- Services Overview - Service discovery and load balancing
- ClusterIP - Internal Services
- NodePort - External access via node IPs
- LoadBalancer - Cloud load balancers
- Headless Services - Direct pod access
- Endpoints & EndpointSlice - How Services track pods
Ingress & Routing
- Ingress - HTTP/HTTPS routing
- Ingress Resources - Configuring Ingress
- Ingress Controllers - Choosing controllers
- TLS - SSL/TLS configuration
- Gateway API - Modern service networking API
- Ingress vs Gateway API - Comparing approaches
- Routes - HTTPRoute and other route types
Network Security
- Network Policies - Pod-level firewalls
- Network Policy Patterns - Common policy patterns
Service Meshes
- Service Meshes Overview - Advanced networking features
- Service Mesh Concepts - Understanding service meshes
- Istio - Popular service mesh
- Installation - Installing Istio
- mTLS - Mutual TLS configuration
- Traffic Management - Advanced traffic control
- Observability - Metrics, logs, traces
- Linkerd - Lightweight service mesh
- Consul - HashiCorp service mesh
See Also
- Workloads - Applications that Services expose
- Security - Network security and Network Policies
- Observability - Network monitoring and troubleshooting
- Cluster Operations - Network configuration and CNI management