Docker & Kubernetes networking
Containers added another layer of networking on top of servers. Nana's networking video tells it as a story in phases (physical servers, then virtual machines, then cloud, then Docker, then Kubernetes), and each phase adds one idea you need to know.
From servers to VMs to containers
With physical servers, each machine had its own network card and IP. Virtual machines put several machines on one physical server; the hypervisor gives each VM a virtual network card and connects them through a virtual switch. Containers go one step further: many isolated apps share one operating system, and Docker gives each container its own small virtual network setup.
Docker networking
- Bridge network (the default): Docker creates a private virtual network on the host, usually
172.17.0.0/16, and each container gets its own IP on it. Containers on the same user-created network can reach each other by container name, because Docker runs a small DNS for them. That's why a web container can connect todb:5432. - Port mapping makes a container reachable from outside the host:
docker run -p 8080:80 nginxsends traffic arriving on the host's port 8080 to port 80 inside the container. Without-p, nothing outside the host can reach it. - Host network (
--network host): the container uses the host's network directly, with no isolation and no mapping. - Overlay network: stretches one virtual network across several hosts, so containers on different machines can talk as if they were side by side (used by Docker Swarm).
$ docker network create appnet
$ docker run -d --name db --network appnet postgres
$ docker run -d --name web --network appnet -p 8080:3000 myapp
# web reaches the database at "db:5432"; your browser reaches web at localhost:8080
-p are reachable from outside the host.Kubernetes networking
Kubernetes runs containers in pods across many machines (nodes), and its networking follows a few simple rules:
- Every pod gets its own IP, unique in the whole cluster, and any pod can reach any other pod directly, with no NAT or port mapping. The plugin that sets this up is the CNI (Container Network Interface), for example Calico or Flannel.
- Pods are temporary: when one is replaced it gets a new IP. So you never connect to a pod's IP directly. Instead you use a Service: a stable virtual IP and DNS name (like
my-api.default.svc.cluster.local, or simplymy-api) that load-balances across all the healthy pods behind it. - kube-proxy runs on every node and programs the rules that make a Service's IP forward to the right pods.
- Service types: ClusterIP (default, reachable only inside the cluster), NodePort (opens the same port on every node), and LoadBalancer (asks the cloud for a real external load balancer).
- An Ingress handles incoming HTTP/HTTPS from outside and routes it to Services by hostname or path (
api.demee.ccto the API service,/to the frontend). It needs an ingress controller, usually Nginx, running in the cluster to do the actual routing. - By default every pod can talk to every other pod. NetworkPolicies are firewall rules between pods, for example "only the API pods may connect to the database pods".
Big setups sometimes add a service mesh (Istio, Linkerd): a small proxy next to every pod that handles encryption between services, retries and traffic metrics without changing the app.