Services

Resources

Company

/

Subnet

Subnet

A subnet is a contiguous slice of a network's IP address range. In a cloud VPC, every instance, load balancer and Kubernetes node gets its private IP from a subnet, and the subnet's route table decides where its traffic can go. A subnet that routes to the internet directly is called public; one that does not is called private.

How it works

A subnet is written in CIDR notation: a base address and a prefix length. The prefix says how many of the 32 bits of an IPv4 address are fixed, so a /24 leaves 8 bits free, which is 2 to the power 8, or 256 addresses. A /16 leaves 16 bits, which is 65,536 addresses.

For example, a VPC of 10.0.0.0/16 can be cut into 256 subnets of /24, from 10.0.0.0/24 to 10.0.255.0/24. AWS keeps 5 addresses in every subnet: the network address (.0), the VPC router (.1), DNS (.2), one for future use (.3) and the last address (.255). So each /24 has 256 minus 5, or 251, usable IPs. If every node and every pod takes one IP, 10 nodes and 241 pods fill it, and the 242nd pod stays Pending.

The route table, rather than any setting on the subnet, makes it public or private. A public subnet has a route for 0.0.0.0/0 to an internet gateway, so a load balancer there can take traffic from the internet. A private subnet has no such route; its instances reach out through a NAT gateway in a public subnet, and nothing on the internet can open a connection to them.

Why it matters in production

A VPC's primary CIDR cannot grow after it is created, and subnets run out of IPs quietly: with the AWS VPC CNI on Amazon EKS, every pod takes a real VPC address. Size subnets for growth, keep ranges that will be connected (peering, VPN, other VPCs) from overlapping, and when you run short, add a secondary CIDR and carve new subnets from it rather than rebuilding the VPC.

See also

On the One2N blog

References

  1. Amazon VPC User Guide: subnet CIDR blocks

  2. RFC 4632: Classless Inter-domain Routing (CIDR)

Address range diagram of a subnet: a VPC of 10.0.0.0/16 holds 65,536 addresses, cut into 256 subnets of /24. One /24 is zoomed in: 256 addresses, of which .0, .1, .2, .3 and .255 are reserved, then 10 nodes and 241 pods. 256 - 5 = 251 usable. A red empty slot after the range shows pod 242 stuck in Pending. A /24 leaves 32 - 24 = 8 bits, and 2⁸ = 256.