Notes
An IPv4 address
An IP version four address is thirty two bits, written as four numbers separated by dots. Each number is one octet, eight bits, so it goes from zero to two hundred and fifty five. Ten dot zero dot one dot seventy seven is just these thirty two bits in a readable form. Two to the power thirty two is about four point three billion addresses, which the internet ran out of long ago, and that is why private networks and NAT, next episode, exist.
10 .0 .1 .77 00001010 00000000 00000001 01001101 four octets of 8 bits each from 0 to 255 about 4.3 billion in total (2 to the power 32)
Network and host
An address has two parts. The first bits name the network, the rest name the machine within it. The slash number, the prefix length, says where the split is. Slash twenty four means the first twenty four bits, ten dot zero dot one, are the network, and the last eight bits, seventy seven, are the host. Machines with the same network part talk directly. Anything else goes through a router, which episode four explains.
- first 24 bits: network 10.0.1
- last 8 bits: host .77
- same network = same prefix → talk directly
- different network → send to the router (episode 4)
CIDR sizes
This notation is called CIDR. The size of a network is two to the power of thirty two minus the prefix. We checked each one with Python's ipaddress module. Slash sixteen holds sixty five thousand five hundred and thirty six addresses. Slash twenty four holds two hundred and fifty six. Slash twenty six, sixty four. Slash twenty eight, sixteen. Slash thirty two is a single host. Every extra bit in the prefix halves the network. The netmask is the same idea written as an address.
/16 65,536 addresses /24 256 255.255.255.0 /26 64 255.255.255.192 /28 16 255.255.255.240 /32 1 one host size = 2 ^ (32 - prefix) each +1 halves the network
Is it inside?
Is ten dot zero dot one dot seventy seven inside ten dot zero dot one dot zero slash twenty six? A slash twenty six holds sixty four addresses, so it runs from dot zero to dot sixty three. Seventy seven is outside, and Python agreed: False. This is exactly the check a security group rule or a firewall makes. When a rule does not match the traffic you expect, check the range first.
>>> n = ip_network(
... "10.0.1.0/26")
>>> n[0], n[-1]
10.0.1.0 10.0.1.63
>>> ip_address("10.0.1.77")
... in n
False Plan a VPC
Now plan a cloud network. A VPC gets one large block, say ten dot zero slash sixteen, and you carve it into subnets. Splitting it into four slash eighteens gives four blocks of sixteen thousand three hundred and eighty four addresses each: public, private for apps, private for data, and one spare. The golden rule: never overlap with another VPC or your office network, because one day you will need to connect them, and overlapping ranges cannot be routed.
10.0.0.0/18 public 10.0.64.0/18 private app 10.0.128.0/18 private data 10.0.192.0/18 spare each /18 = 16,384 addresses leave room to grow never overlap with other VPCs or the office network
Reserved addresses
Not every address can be used. Traditionally, the first address names the network and the last one is the broadcast address. AWS reserves five in every subnet: the first four and the last, for the router, DNS and future use. So a slash twenty eight in AWS gives you eleven usable addresses, not sixteen. And with some Kubernetes setups every pod takes a VPC address, so a cluster can drain a small subnet surprisingly fast. Size generously.
- classic network: first = network, last = broadcast
- AWS reserves 5 per subnet: .0 .1 .2 .3 and the last
- a /28 on AWS: 16 - 5 = 11
- Kubernetes pods may each take a VPC address → size subnets generously
Special ranges
A few ranges come up daily. One two seven is loopback, the machine itself. Ten, one seven two dot sixteen and one nine two dot one six eight are private ranges, next episode. One six nine dot two five four is link-local, and one six nine dot two five four dot one six nine dot two five four is the cloud metadata service every VM talks to. One hundred dot sixty four is carrier grade NAT. And zero dot zero dot zero dot zero, in a listen address, means every interface on the machine.
- 127.0.0.0/8 loopback
- 10/8 172.16/12 192.168/16 private
- 169.254.0.0/16 link-local 169.254.169.254 = cloud metadata service
- 100.64.0.0/10 carrier NAT
- 0.0.0.0 "all" or "any address"
Common mistakes
Mistakes. Overlapping ranges between VPCs or with the office. Subnets too small for autoscaling or Kubernetes pods. Forgetting reserved addresses. Guessing whether an address is in a range instead of computing it. And writing zero dot zero dot zero dot zero slash zero, the whole internet, into a rule by habit.
- overlapping VPC or office ranges
- subnets too small for autoscaling or pods
- forgetting reserved addresses
- guessing a range instead of computing it
- 0.0.0.0/0 rules by habit
Say it like this
In the interview: a slash twenty six is two to the power six, sixty four addresses, with netmask two five five dot two five five dot two five five dot one nine two. In AWS five are reserved, so fifty nine are usable. Then go further: for a new VPC I pick a slash sixteen that overlaps nothing, split it into public and private subnets per availability zone, and keep spare space for growth.
- "2 to the 32 minus 26 = 64
- addresses, mask .192. In AWS
- 5 are reserved, so 59 usable.
- For a VPC I take a /16 that does not overlap anything, split
- it per AZ into public and private subnets, and leave
- spare space for growth."