Part 6 · Real networks: infrastructure, security and operations

Cloud and container networking: VPCs, Docker and Kubernetes

lesson 16 / 1910 minpublished updated
Course contents
On this page
  1. VPC: your private network in the cloud
  2. Linux networking: the foundation of containers
  3. Kubernetes networking

In the cloud and in containers, the network doesn’t disappear; it becomes software built from parts you already know. A VPC is a virtual network with subnets, route tables and firewalls. A Docker container is a network namespace wired up with a veth pair, a bridge and NAT, and Kubernetes adds pod IPs, virtual Service IPs and network policies on top of that.

VPC: your private network in the cloud

All the clouds (AWS, GCP, Azure and the rest) share similar concepts; the names used here are AWS’s.

InternetInternet GatewayVPC 10.0.0.0/16AZ apublic 10.0.0.0/24load balancer nodeNAT Gateway0.0.0.0/0 -> IGWprivate 10.0.10.0/24app instancesdatabase0.0.0.0/0 -> NAT GWno inbound from InternetAZ bpublic 10.0.1.0/24load balancer nodeNAT Gateway0.0.0.0/0 -> IGWprivate 10.0.11.0/24app instancesdatabase0.0.0.0/0 -> NAT GWno inbound from Internetsecurity groups on every instance (stateful)
A VPC with public and private subnets in two Availability Zones, an Internet Gateway and a NAT Gateway.
  • VPC: a virtual private network with a CIDR block (for example, 10.0.0.0/16). Choose the CIDR so it doesn’t overlap with your office network, your other VPCs or your VPNs; changing it later is painful.
  • Subnet: a slice of the VPC in one Availability Zone. Whether a subnet is “public” or “private” depends only on its route table.
    • Public subnet: the route for 0.0.0.0/0 points to an Internet Gateway (IGW). Resources with a public IP connect to the Internet directly (load balancers, bastions).
    • Private subnet: the route for 0.0.0.0/0 points to a NAT Gateway in a public subnet. Outbound connections to the Internet work; inbound connections from the Internet don’t (apps and databases).
  • Security group: a stateful firewall on each instance or network interface. Rules can reference other security groups (“the database accepts connections only from the app’s SG”). NACL: a stateless firewall on the subnet.
  • Connecting networks: VPC peering (one-to-one, not transitive), Transit Gateway (a central hub), PrivateLink or VPC endpoints (reach a service such as S3 without crossing the Internet), site-to-site VPN over IPsec, and dedicated physical connections (Direct Connect, ExpressRoute, Interconnect).
  • Load balancers (ALB for L7, NLB for L4), private DNS (private hosted zones), and flow logs for debugging and security.
  • Cost: data transfer between AZs, egress to the Internet, and NAT Gateways usually cost more than the compute itself. Your network design shows up directly on the bill.

Linux networking: the foundation of containers

As far as the network is concerned, a container is a network namespace, the same thing you’ve been building in this course’s labs:

  • Docker creates a namespace for each container and connects it, with a veth pair, to a bridge called docker0 (or to the bridge of a user-defined network).
  • Outbound Internet access is a MASQUERADE rule in iptables (SNAT). Publishing a port (-p 8080:80) is a DNAT rule.
  • Docker’s internal DNS listens on 127.0.0.11 inside every container.
  • --network host means no separate namespace; --network none means loopback only.
docker run -d --name web nginx
PID=$(docker inspect -f '{{.State.Pid}}' web)
# eth0 inside the container: one end of a veth pair, with a private IP from docker0's subnet
sudo nsenter -t $PID -n ip -br addr
lo               UNKNOWN        127.0.0.1/8
eth0@if32        UP             172.17.0.2/16
ip -br link | grep -E 'docker0|veth'      # the host side: docker0 bridge + the other veth end
docker0          UP             0e:1c:4f:31:ee:b0 <BROADCAST,MULTICAST,UP,LOWER_UP>
veth7f7e087@if2  UP             3e:43:ed:03:ca:ef <BROADCAST,MULTICAST,UP,LOWER_UP>
sudo iptables -t nat -S | grep -E 'MASQUERADE|DOCKER'    # the NAT rules Docker added
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER

This is exactly what you built by hand in the routing lab and the DNAT lab: a veth pair (one end is eth0 inside the container’s namespace, the other end is attached to the docker0 bridge), a private IP (172.17.0.2), and a MASQUERADE rule for getting out to the Internet. There’s no magic in “container networking”: it’s the same network namespaces, veth pairs, bridges and NAT.

docker rm -f web      # clean up

Kubernetes networking

  • The ground rule: every pod gets its own IP, and all pods can reach each other without NAT. A CNI plugin implements this, in one of several ways:
    • Overlay (VXLAN or GENEVE; Flannel, Calico in VXLAN mode): works on top of any network, at the cost of encapsulation overhead and a smaller MTU.
    • Routed / BGP (Calico): pod routes are announced to the network’s routers over BGP. No overhead.
    • Cloud-native (AWS VPC CNI): pods get real IPs straight from the VPC’s subnet.
    • eBPF (Cilium): packets are processed inside the kernel by eBPF programs. It replaces kube-proxy and iptables, and adds L7 NetworkPolicy and excellent observability.
  • Service: a virtual IP (the ClusterIP) that kube-proxy DNATs to one of the healthy pods, using iptables, IPVS or nftables rules (or Cilium does it with eBPF). A LoadBalancer Service in the cloud creates a real NLB.
  • Ingress and the Gateway API: HTTP entry into the cluster, at layer 7.
  • NetworkPolicy: a firewall between pods, based on labels.
  • Service mesh (Istio, Linkerd): a proxy (usually Envoy, or a lightweight proxy) running alongside the workloads on every node. It adds automatic mTLS, retries, timeouts, traffic splitting and telemetry, with no code changes.

Running all of this in practice is a subject of its own; for now, the goal is to know which network layer each piece works at.