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.

graph TB A[Pod Network Model] --> B[Every Pod Gets IP] A --> C[Pods Can Reach Each Other] A --> D[No NAT Between Pods] A --> E[Services Provide Stable Endpoints] B --> F[Pod IP: 10.244.1.5] C --> G[Direct Pod-to-Pod Communication] D --> H[Simplified Networking] E --> I[Service IP: 10.96.0.1] style A fill:#e1f5ff style E fill:#e8f5e9

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:

graph TB A[Application Layer] --> B[Service Mesh<br/>mTLS, Traffic Management] B --> C[Ingress<br/>HTTP/HTTPS Routing] C --> D[Services<br/>Service Discovery & Load Balancing] D --> E[Network Policies<br/>Traffic Filtering] E --> F[CNI Plugin<br/>Pod Networking] F --> G[Physical Network<br/>Nodes & Infrastructure] style A fill:#e1f5ff style B fill:#f3e5f5 style C fill:#e8f5e9 style D fill:#fff4e1 style E fill:#ffe1e1 style F fill:#fff4e1

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:

graph LR A[Application Pod] --> B[Needs to Connect to Service] B --> C[DNS Query: my-service] C --> D[CoreDNS] D --> E[Returns Service IP] E --> F[Service Routes to Pod] F --> G[Load Balanced Across Pods] style A fill:#e1f5ff style D fill:#fff4e1 style E fill:#e8f5e9 style G fill:#fff4e1
  1. Application makes DNS query - Pod queries for my-service or my-service.namespace.svc.cluster.local

  2. CoreDNS resolves - CoreDNS returns the Service IP address

  3. Service routes traffic - Service load balances across backend pods matching its selector

  4. Pod receives request - One of the backend pods handles the request

Service Types

Kubernetes provides different Service types for different access patterns:

graph TB A[Service Types] --> B[ClusterIP<br/>Internal Only] A --> C[NodePort<br/>Node IP + Port] A --> D[LoadBalancer<br/>Cloud Load Balancer] A --> E[Headless<br/>Direct Pod Access] B --> F[Service-to-Service] C --> G[External Access via Node] D --> H[External Access via Cloud] E --> I[StatefulSet Discovery] style A fill:#e1f5ff style B fill:#e8f5e9 style C fill:#fff4e1 style D fill:#fff4e1 style E fill:#f3e5f5

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.

graph TB A[HTTP/HTTPS Traffic] --> B{Which API?} B -->|Traditional| C[Ingress] B -->|Modern| D[Gateway API] C --> E[Ingress Controller<br/>NGINX/Traefik/etc] D --> F[Gateway Controller] E --> G[Routes to Services] F --> G style A fill:#e1f5ff style C fill:#fff4e1 style D fill:#e8f5e9 style G fill:#fff4e1

Network Policies

Network Policies provide pod-level firewalls, controlling traffic flow:

graph TB A[Network Policy] --> B[Selects Pods] A --> C[Defines Ingress Rules] A --> D[Defines Egress Rules] B --> E[Which Pods Policy Applies To] C --> F[Who Can Connect To Pods] D --> G[Where Pods Can Connect] style A fill:#e1f5ff style C fill:#e8f5e9 style D fill:#e8f5e9

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:

graph TB A[Service Mesh] --> B[Traffic Management] A --> C[Security] A --> D[Observability] A --> E[Policy Enforcement] B --> B1[Load Balancing] B --> B2[Circuit Breaking] B --> B3[Retries & Timeouts] C --> C1[Mutual TLS] C --> C2[Authentication] D --> D1[Metrics] D --> D2[Traces] D --> D3[Logs] E --> E1[Rate Limiting] E --> E2[Access Control] style A fill:#e1f5ff style B fill:#fff4e1 style C fill:#e8f5e9 style D fill:#f3e5f5 style E fill:#ffe1e1

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:

graph TD A[Cluster Created] --> B[CNI Plugin Installed] B --> C[Pod Network Configured] C --> D[Pods Get IP Addresses] D --> E[Services Created] E --> F[DNS Records Created] F --> G[Applications Communicate] H[External Access Needed?] --> I[Ingress or LoadBalancer] I --> J[Traffic Routes to Services] J --> G K[Security Needed?] --> L[Network Policies] L --> M[Traffic Filtered] M --> G style A fill:#e1f5ff style E fill:#e8f5e9 style G fill:#fff4e1

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

  1. Use Services, not pod IPs - Always use Services for stable endpoints

  2. Choose the right Service type - ClusterIP for internal, LoadBalancer for external cloud, Ingress for HTTP/HTTPS

  3. Use DNS names - Prefer DNS names over IP addresses for service discovery

  4. Implement Network Policies - Start with default deny, then explicitly allow needed traffic

  5. Use Ingress for HTTP/HTTPS - More cost-effective than LoadBalancer for multiple Services

  6. Consider service meshes - For advanced features like mTLS, traffic management, and observability

  7. Monitor network performance - Track latency, throughput, and error rates

  8. Document network architecture - Keep diagrams and documentation of your networking setup

  9. Test network policies - Verify Network Policies work as expected before production

  10. Plan for scale - Consider network performance and limits as you scale

Topics

Core Networking

Services

Ingress & Routing

Network Security

Service Meshes

See Also