PART 1: CORE THEORY
1. The Kubernetes Networking Model
Kubernetes networking is built on four fundamental rules that every Pod must satisfy.
The Four Rules
| Rule | Meaning |
| 1. Pod-to-Pod | Any Pod can reach any other Pod without NAT |
| 2. Pod-to-Service | Pods can reach Services via stable ClusterIP |
| 3. Node-to-Pod | Nodes can reach Pods without NAT |
| 4. External-to-Service | External clients reach Services via NodePort/LoadBalancer/Ingress |
The Networking Stack
text
┌─────────────────────────────────────────────────────────────┐
│ KUBERNETES NETWORKING STACK │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Pods (10.244.0.0/16) │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Pod A │ │ Pod B │ │ │
│ │ │ 10.244.1.5 │ │ 10.244.2.8 │ │ │
│ │ └──────┬──────┘ └──────┬──────┘ │ │
│ └─────────┼────────────────┼──────────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ CNI Plugin (Calico, Cilium, Flannel, VPC CNI) │ │
│ │ • IP assignment │ │
│ │ • veth pair creation │ │
│ │ • Cross-node routing │ │
│ │ • Network policy enforcement │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ kube-proxy (Service load balancing) │ │
│ │ • iptables / IPVS / nftables │ │
│ │ • ClusterIP → Pod IP mapping │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Node Network (eth0, VPC, underlay) │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
The Pod Network CIDR
Every cluster has a Pod CIDR (e.g., 10.244.0.0/16) from which Pod IPs are allocated. Each node gets a slice (e.g., 10.244.1.0/24).
Important: The Pod CIDR must not overlap with:
- The Node CIDR (VPC subnet)
- The Service CIDR (ClusterIP range)
- Any on-premises network you connect to
2. CNI (Container Network Interface)
CNI is the plugin interface that Kubernetes uses to configure Pod networking.
What a CNI Plugin Does
| Function | Description |
| IPAM | Assigns IP addresses to Pods |
| veth pairs | Creates virtual Ethernet pairs |
| Routing | Programs cross-node routes |
| Policy | Enforces NetworkPolicy (optional) |
The CNI Lifecycle
text
1. kubelet creates a Pod sandbox (network namespace)
2. kubelet invokes the CNI plugin with ADD command
3. CNI plugin:
a. Allocates an IP from the Pod CIDR
b. Creates a veth pair (one end in Pod, one on host)
c. Configures routes so the Pod can reach other Pods
d. Returns the IP to kubelet
4. kubelet starts the container in the sandbox
CNI Plugin Categories
| Category | Examples | Approach |
| Overlay | Flannel (VXLAN) | Encapsulates pod traffic |
| Routing | Calico (BGP) | Programs routes natively |
| eBPF | Cilium | Kernel-level networking |
| Cloud-native | AWS VPC CNI, Azure CNI | Uses cloud VPC IPs |
| Simple | Bridge, host-local | Basic connectivity |
CNI Requirements
| Requirement | Why |
| Pod CIDR | IP allocation range |
| Kernel modules | VXLAN, eBPF, etc. |
| Node permissions | Network namespace access |
| CNI config | /etc/cni/net.d/ |
3. Service Networking
A Service provides a stable virtual IP (ClusterIP) that load-balances to a set of Pods.
The Four Service Types
| Type | Accessibility | Use Case |
| ClusterIP | Internal only | Service-to-service |
| NodePort | Via Node IP + port | Development, on-prem |
| LoadBalancer | Cloud LB | Production external |
| ExternalName | DNS alias | External services |
ClusterIP (Default)
yaml
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- port: 80
targetPort: 8080
port: Service port (what clients connect to)targetPort: Pod port (where traffic is forwarded)
ClusterIP range: Typically 10.96.0.0/12 (configurable).
NodePort
yaml
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
nodePort: 30080
Range: 30000–32767 (default).
Access: <NodeIP>:30080.
LoadBalancer
yaml
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
Provisions a cloud LB (AWS ELB/ALB/NLB, GCP LB, Azure LB).
ExternalName
yaml
spec:
type: ExternalName
externalName: db.example.com
DNS-only CNAME. No proxying.
How Service Load Balancing Works
text
Client → ClusterIP (10.96.0.10) → kube-proxy → Pod IP (10.244.1.5)
│
├── iptables DNAT
├── IPVS virtual server
└── eBPF map lookup
Service DNS
Every Service gets a DNS name:
text
<service>.<namespace>.svc.cluster.local
Examples:
web.default.svc.cluster.local(FQDN)web.default(same cluster)web(same namespace)
Endpoints and EndpointSlices
The Service's backing Pods are tracked in EndpointSlice objects.
bash
kubectl get endpoints web
kubectl get endpointslices -l kubernetes.io/service-name=web
Empty endpoints = no Pods match the selector. This is the #1 cause of Service failures.
4. kube-proxy
kube-proxy is the component that implements Service load balancing on each node.
kube-proxy Modes
| Mode | Mechanism | Scalability |
| iptables | iptables DNAT rules | Poor (linear with services) |
| IPVS | IPVS virtual servers | Good (hash-based) |
| nftables | nftables rules | Modern (replaces iptables) |
iptables Mode
Every Service creates iptables rules. As services grow, rules grow linearly:
text
10 services: 100 rules
100 services: 1,000 rules
1,000 services: 10,000 rules
Problem: Every packet traverses the chain. Performance degrades.
IPVS Mode
IPVS uses hash tables with O(1) lookup. Performance is stable regardless of service count.
IPVS advantages:
- Multiple load-balancing algorithms (round-robin, least connections, etc.)
- Better scalability
- Server health checking
- Connection retries
Performance difference: The main difference between IPVS and iptables is in TCP connection establishment. For short connections, IPVS is significantly faster. As Service and Endpoint counts increase, iptables performance drops while IPVS remains stable.
nftables Mode
nftables is the modern replacement for iptables. It's more efficient and has a cleaner syntax.
Kubernetes 1.31+ supports nftables mode in kube-proxy.
kube-proxy Components
| Component | Purpose |
| Service watcher | Watches Service/Endpoint changes |
| Rule programmer | Writes iptables/IPVS rules |
| Health checker | (IPVS only) Probes backend health |
| Metrics exporter | Exposes Prometheus metrics |
The kube-proxy Performance Problem
Even IPVS requires netfilter hooks and conntrack overhead. At high scale, this causes:
- CPU overhead from packet processing
- conntrack table pressure
- The UDP conntrack race condition (5-second DNS timeout)
Solution: eBPF-based kube-proxy replacement (Cilium).
5. CoreDNS
CoreDNS is the cluster DNS server. Every Pod's /etc/resolv.conf points to the CoreDNS ClusterIP.
How CoreDNS Works
text
Pod → /etc/resolv.conf (nameserver 10.96.0.10)
│
▼
CoreDNS (kube-dns Service)
│
├── For cluster.local → returns Service/Endpoint IPs
├── For external domains → forwards to upstream DNS
└── Caches responses
CoreDNS Configuration
Stored in the coredns ConfigMap in kube-system:
text
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
Key CoreDNS Plugins
| Plugin | Purpose |
| kubernetes | Serves cluster.local records |
| forward | Forwards external queries |
| cache | Caches responses |
| health | Health endpoint |
| prometheus | Metrics |
| loop | Detects forwarding loops |
| reload | Auto-reload config |
Pod DNS Configuration
text
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
The ndots:5 problem: Any name with fewer than 5 dots triggers search-domain expansion.
For api.stripe.com (2 dots < 5):
api.stripe.com.default.svc.cluster.local→ NXDOMAINapi.stripe.com.svc.cluster.local→ NXDOMAINapi.stripe.com.cluster.local→ NXDOMAINapi.stripe.com.→ Success
One logical lookup = 4 wire queries.
NodeLocal DNSCache
NodeLocal DNSCache runs a DNS cache on every node as a DaemonSet.
| Benefit | Explanation |
| Bypasses conntrack | UDP → TCP conversion |
| Eliminates 5s timeout | No UDP conntrack race |
| Sub-ms cache hits | Never leaves the node |
| Reduces CoreDNS load | 80–95% fewer queries |
Architecture:
text
Pod → 169.254.20.10 (NodeLocal DNSCache) → TCP → CoreDNS
│
└── Cache hit: answer locally
└── Cache miss: forward to CoreDNS over TCP
1:1 copy of the class Google Doc, code in code blocks.