Services and Networking

Introduction

One of my early frustrations with Kubernetes was understanding how applications actually communicate with each other. I had pods running, but how do they find each other when they're constantly being created and destroyed with different IP addresses? How do external users access my services? Why do I need LoadBalancers, Ingress controllers, and Network Policies?

The networking model in Kubernetes initially seemed complex, but once I understood the core concepts—Services provide stable endpoints, Ingress manages external access, and Network Policies enforce security—everything clicked into place. Through deploying microservices architectures and troubleshooting connectivity issues in production, I learned that Kubernetes networking is actually elegant in its design, even if it takes some time to master.

In this article, I'll share what I've learned about Kubernetes networking, from basic Service types to advanced Ingress configurations and Network Policies, with practical examples from real deployments.

Table of Contents

Kubernetes Networking Model

Kubernetes networking is built on several key principles that make it work seamlessly.

Networking Principles

spinner

Core Requirements:

  1. Pod-to-Pod Communication: All pods can communicate with each other without NAT

  2. Node-to-Pod Communication: Nodes can communicate with all pods without NAT

  3. Pod IP Address: Each pod gets its own unique IP address

  4. No Port Conflicts: Containers share the pod's network namespace

CNI Plugins

Container Network Interface (CNI) plugins implement the Kubernetes network model:

  • Calico: L3 networking with BGP, Network Policy support

  • Flannel: Simple overlay network, easy to set up

  • Cilium: eBPF-based, advanced observability

  • Weave: Mesh network, encrypts traffic

  • Canal: Flannel + Calico Network Policies

Service Types and Use Cases

Services provide stable networking for ephemeral pods.

Service Type Decision Tree

spinner

ClusterIP Services

ClusterIP is the default Service type, exposing the service on an internal IP address within the cluster.

Basic ClusterIP Service

Session Affinity

Multi-Port Services

Named Ports

NodePort Services

NodePort exposes the service on each node's IP at a static port.

NodePort Service Example

NodePort with Custom Range

NodePort Best Practices

LoadBalancer Services

LoadBalancer creates an external load balancer (in supported cloud environments).

LoadBalancer Service Example

AWS Load Balancer Controller

MetalLB for Bare Metal

ExternalName Services

ExternalName services map a service to a DNS name.

ExternalName Example

External Service with Endpoints

For external services that need more control than ExternalName:

Headless Services

Headless services don't have a ClusterIP and return pod IPs directly.

Headless Service Example

Ingress Controllers

Ingress provides HTTP/HTTPS routing to services based on rules.

Ingress Architecture

spinner

Installing NGINX Ingress Controller

Alternative Ingress Controllers

Traefik:

HAProxy:

Contour:

Ingress Resources

Ingress resources define routing rules.

Basic Ingress

Advanced Ingress with Multiple Hosts

Path-Based Routing

TLS/SSL Configuration

Rate Limiting and Auth

DNS and Service Discovery

Kubernetes provides automatic DNS for services and pods.

DNS Records

CoreDNS Configuration

Testing DNS Resolution

Custom DNS Configuration

Network Policies

Network Policies control traffic between pods and services.

Default Deny All Traffic

Allow Specific Traffic

Database Network Policy

IP Block Rules

Troubleshooting Network Issues

Debugging Network Connectivity

Check Service Endpoints

Verify Network Policies

CNI Plugin Issues

Service Communication Test Script

What I Learned

Mastering Kubernetes networking transformed my ability to build reliable, secure microservices:

Services Are the Foundation: Understanding that Services provide stable endpoints for ephemeral pods was my first "aha" moment. Pods come and go, but Services remain constant. Design with Services from the start.

Choose the Right Service Type: ClusterIP for internal services, LoadBalancer for cloud environments, NodePort sparingly (mainly for debugging), and Ingress for HTTP/HTTPS routing. Each has its place.

Ingress Controllers Are Powerful: Initially, I underestimated Ingress controllers. They're not just reverse proxies—they provide TLS termination, path-based routing, rate limiting, authentication, and much more. Learn your Ingress controller's annotations.

Network Policies Are Security: Don't run production without Network Policies. Start with default-deny and explicitly allow required traffic. This defense-in-depth approach has caught many potential issues.

DNS Just Works: Kubernetes DNS is incredibly reliable. Trust it, use Service names instead of IPs, and leverage the automatic DNS records for service discovery.

Debugging Takes Practice: Network issues can be frustrating, but systematic debugging—checking endpoints, testing DNS, verifying policies, examining CNI logs—always leads to the answer.

External Access Needs Planning: How external users access your services depends heavily on your environment (cloud vs on-premise) and requirements (HTTP vs TCP, TLS termination, etc.). Design your ingress strategy early.

Kubernetes networking seems complex at first, but it's built on solid principles that make sense once you understand them. With Services, Ingress, and Network Policies, you can build secure, scalable applications that communicate reliably. In the next articles, we'll explore how to configure these applications and add persistent storage.

Last updated