Kubernetes Networking Fundamentals: Complete Guide

Kubernetes networking is notoriously complex. This guide explains how pod-to-pod, pod-to-service, and external-to-cluster networking works, along with CNI plugins and cloud provider integrations.

Kubernetes Networking Model

Kubernetes imposes specific requirements on any networking implementation:

Core Requirements

Networking Layers

┌──────────────────────────────────────────────────────────┐
│ External Traffic (Internet)                               │
└─────────────────────────┬────────────────────────────────┘
                          │ Ingress / LoadBalancer
┌─────────────────────────▼────────────────────────────────┐
│ Service (ClusterIP, NodePort, LoadBalancer)               │
└─────────────────────────┬────────────────────────────────┘
                          │ kube-proxy / iptables / IPVS
┌─────────────────────────▼────────────────────────────────┐
│ Pod Networking (CNI)                                      │
│ Pod-to-Pod communication across nodes                     │
└──────────────────────────────────────────────────────────┘

Pod Networking

How Pods Get IPs

  1. Kubelet creates new pod
  2. CNI plugin called to set up networking
  3. Pod gets IP from cluster CIDR range
  4. Routes configured for pod-to-pod traffic

CNI (Container Network Interface)

Plugins that handle the actual network setup:

CNI Plugin Best For Key Features
AWS VPC CNI EKS Native VPC IPs, security groups per pod
Calico Multi-cloud, on-prem Network policies, BGP, eBPF
Cilium Advanced networking eBPF, L7 policies, observability
Flannel Simple overlay Easy setup, VXLAN/host-gw
Azure CNI AKS VNet integration, NSG support

Kubernetes Services

ClusterIP

Internal-only virtual IP:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ClusterIP
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 8080

NodePort

LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: my-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
  - port: 443
    targetPort: 8443

Ingress

HTTP(S) routing for multiple services:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: api.example.com
    http:
      paths:
      - path: /v1
        pathType: Prefix
        backend:
          service:
            name: api-v1
            port:
              number: 80
      - path: /v2
        pathType: Prefix
        backend:
          service:
            name: api-v2
            port:
              number: 80
  tls:
  - hosts:
    - api.example.com
    secretName: api-tls

Ingress Controllers

Network Policies

Firewall rules for pod-to-pod traffic:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-network-policy
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - protocol: TCP
      port: 8080
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: database
    ports:
    - protocol: TCP
      port: 5432

Key Points

Cloud Provider Integration

AWS EKS Networking

# Pod IP from VPC, not overlay
kubectl get pod -o wide
NAME      IP           NODE
my-pod    10.0.1.50    10.0.1.100  # VPC IP

GKE Networking

AKS Networking

DNS in Kubernetes

CoreDNS

DNS Resolution

# Service DNS name
my-service.my-namespace.svc.cluster.local

# Pod DNS name
10-0-1-50.my-namespace.pod.cluster.local

# Shortened (within namespace)
my-service

Troubleshooting Kubernetes Networking

Common Issues

Debugging Commands

# Check endpoints
kubectl get endpoints my-service

# Test DNS
kubectl run test --rm -it --image=busybox -- nslookup my-service

# Test connectivity
kubectl run test --rm -it --image=nicolaka/netshoot -- curl my-service

# View iptables rules
kubectl get pod -n kube-system -l k8s-app=kube-proxy -o name | \
  xargs -I {} kubectl exec -n kube-system {} -- iptables -t nat -L

Key Takeaways

Need Kubernetes Networking Help?

We design and optimize Kubernetes network architectures. Contact us for a consultation.