Chapters

VPC networks and subnets

Chapter 1.2 showed that a subnet is not a place but a range of addresses and a set of rules. This chapter looks at the two objects you actually create, the VPC network and its subnets: what each one decides, what you can change later, and what you cannot.

Foundations · chapter 3 of 26 · 15 min read

A VPC is a scope

Builds on6. Two networks, one router

The easiest way to think about a VPC network is as a boundary. Everything inside it shares four things, and nothing outside it gets them:

#The VPC decidesThe question it answersChapter
1Which routes applyWhere can a packet be sent?4
2Which firewall rules applyWhich packets are allowed?5
3Which IP ranges are handed out (through its subnets)What addresses can be given to things?2, 3
4Which resources can reach each other on internal IPsWho is inside?2

Two VPCs in the same project are two separate worlds. There is no router between them unless you add one (peering or VPN, chapter 14). A VM in default cannot reach kade-db on 10.10.2.5, because default has no route to it. The two VPCs could even use the same ranges and never notice.

project kade-prodVPC kade-vpc (custom mode)routes · firewall rules · subnetskade-api-110.10.1.10 · sn-appkade-db10.10.2.5 · sn-datathese two can reach each other (if the firewall allows)VPC default (auto mode)its own routes · its own firewall rules · its own subnetstest-vm10.148.0.2 (if someone made one)✕no route to 10.10.2.5: a different world
  • Your VPC
  • Allowed traffic and replies

A VPC is the outer wall. Subnets divide the inside, but they are not walls: by default any VM can route to any subnet in the same VPC, and only firewall rules stop it.

Wide by design - scroll it sideways.

A VPC is also a boundary on the map, and this is where GCP differs most from AWS. A GCP VPC is global and a subnet is regional (chapter 1.1). Here are Kadé's two web servers, drawn both ways:

AWS: a VPC is regional, a subnet is zonalVPC · one region, SingaporeAZ aAZ bAZ csubnet 10.10.1.0/24kade-api-1this zone onlysubnet 10.10.3.0/24kade-api-2this zone onlya VM here needsa third subnetMumbaia second region needsa second VPC, joinedby peering or atransit gatewayGCP: kade-vpc is global, a subnet is regionalkade-vpc · global, one network for every regionregion asia-southeast1 · Singaporezone azone bzone ckade-api-1kade-api-2sn-app 10.10.1.0/24every zone in the regionasia-south1 · Mumbaia second region needsonly a new subnet inthe same kade-vpc
  • Google's: the region and its zones
  • What you create: the VPC, its subnets and VMs

In GCP, kade-api-1 and kade-api-2 run in different zones and still share sn-app. Adding a region adds a subnet, not a second network.

Wide by design - scroll it sideways.

#AWSGCP
1A VPC lives inOne regionEvery region (global)
2A subnet lives inOne zone (an Availability Zone)One region, across all its zones
3VMs in two zones needTwo subnetsOne subnet
4A second region needsA second VPC, joined by peering or a transit gatewayA new subnet in the same VPC
5Traffic between regionsGoes through the peering or transit gatewayStays inside the VPC, on Google's backbone (chapter 1.1)

A VPC network itself has only a few settings. Most of what matters lives on its subnets:

#VPC settingKadé's valueChange later?Chapter
1Namekade-vpcno2
2Subnet creation modecustomauto → custom only2
3MTU1460yes, with VMs stopped1
4Dynamic routing moderegionalyes4, 15
5Internal IPv6 range (a /48 ULA)noneadd once, never change3
6An IPv4 rangedoes not exist (chapter 0)-0

Auto mode and custom mode

When you create a VPC, you choose who creates its subnets:

Auto mode

GCP creates one /20 subnet in every region, from a fixed list inside 10.128.0.0/9. When Google opens a new region, a new subnet appears in your network by itself.

Custom mode

GCP creates nothing. Every subnet, region and range is a decision you make. This is what Google recommends for production, and what kade-vpc uses.

The auto mode ranges are the same in every auto mode network in the world, and Google publishes them:

#RegionAuto mode subnetGateway
1us-central110.128.0.0/2010.128.0.1
2europe-west110.132.0.0/2010.132.0.1
3us-west110.138.0.0/2010.138.0.1
4asia-east110.140.0.0/2010.140.0.1
5asia-southeast1 (Kadé's region)10.148.0.0/2010.148.0.1
6asia-south110.160.0.0/2010.160.0.1

That convenience has a cost:

#Problem with auto modeWhy it matters
1The ranges are fixed and publicAnyone who gets onto one VM already knows the whole internal address plan, without running a single command
2Every auto mode network uses the same rangesTwo auto mode networks can never be joined with peering or VPN: their ranges overlap by design
310.128.0.0/9 is a common private rangeAn office or another cloud may already use part of it, which blocks a VPN later
4Subnets appear in regions you never useMore ranges to keep track of, and new ones arrive without anyone deciding
5No IPv6Auto mode networks cannot have subnets with IPv6 ranges

Two more facts. You can add your own subnets to an auto mode network, as long as they are outside 10.128.0.0/9. And you can convert an auto mode network to custom mode at any time: the existing subnets stay exactly as they are, and no new ones appear. The conversion only goes one way.

The default network

Every new project gets a network called default, unless an organization policy stops it. It is an auto mode network plus four firewall rules. Chapter 0 already listed them; here is what each one really allows:

#RuleSourceAllowsReal riskUsual action
1default-allow-internal10.128.0.0/9every TCP and UDP port, and ICMPEvery VM can reach every port on every other VM: no separation at allReplace with rules for what really talks to what (chapter 5)
2default-allow-ssh0.0.0.0/0tcp:22Any VM with an external IP is open to every SSH bot on the internetLimit to IAP's range 35.235.240.0/20 (chapter 6)
3default-allow-rdp0.0.0.0/0tcp:3389The same for Windows remote desktopDelete if there are no Windows VMs
4default-allow-icmp0.0.0.0/0icmpSmall: lets anyone confirm a VM existsOften left, or limited to known IPs
All four have priority 65534 and apply to every VM in the network. The two built-in rules (block all incoming, allow all outgoing) sit underneath them at 65535.

What to do depends on whether anything uses it:

#SituationWhat to do
1Nothing uses it (Kadé)Delete its four firewall rules, then delete the network. Its auto subnets go with it
2VMs or other resources already live in itYou cannot delete a network while anything uses it. Convert it to custom mode so no new subnets appear, delete the auto subnets in regions you do not use, and replace the four rules with narrow ones
3For every future projectSet the organization policy compute.skipDefaultNetworkCreation so new projects never get one (chapter 17)

What a subnet is made of

A subnet is a regional object inside one VPC. These are its properties, and the column that matters most is the one that says whether you can change it later:

#PropertyWhat it isChange later?Notes
1nameThe subnet's namenoUnique per project per region. To rename, create a new one and delete the old one once nothing uses it
2regionThe region it lives innoCovers every zone in that region (chapter 1.1)
3networkThe VPC it belongs tonoA subnet cannot move to another VPC
4ipCidrRangeThe primary IPv4 rangegrow onlyCan be expanded, never shrunk or moved. Must not overlap any other range in the VPC. Loses 4 addresses (section 05). Smallest is /29
5secondaryIpRangesExtra named rangesadd / removeOnly for alias IPs (section 07). Removable only when nothing uses them
6gatewayAddressThe subnet's gatewayread-onlyAlways the second address of the primary range, the .1
7purposeWhat the subnet is formostly noPRIVATE for VMs; other values are for Google-managed things (section 06)
8roleActive or backupyesOnly for proxy-only subnets: ACTIVE or BACKUP
9stackTypeIPv4 only, dual-stack, or IPv6 onlylimitedIPv4-only can become dual-stack. Dual-stack can go back to IPv4-only only if its IPv6 is external. IPv6-only never changes
10ipv6AccessTypeInternal or external IPv6noOnly when the subnet has IPv6
11privateIpGoogleAccessReach Google APIs without an external IPyesOff by default (chapter 9)
12logConfig (flow logs)Record samples of trafficyesOff by default; the sampling rate is the main cost setting (chapter 16)

The four addresses you cannot use

Builds on3. Network and broadcast addresses5. When the gateway is the problem

A normal network loses two addresses: the network address and the broadcast address. A GCP subnet loses four, the first two and the last two of its primary range. In sn-app:

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  • 4 reserved: .0 .1 .254 .255
  • in use: kade-api-1 .10, kade-api-2 .11
  • free: 250 more

sn-app, 10.10.1.0/24: 256 addresses, one cell each. Hover a cell to see its full address.

#Address in sn-appNameWhy it is kept
110.10.1.0Network addressNames the subnet itself, so it cannot be given to a machine
210.10.1.1Default gatewayThe address the data plane answers for (chapter 1.2). Not a real router
310.10.1.254Second-to-lastKept by Google for possible future use; it does nothing today
410.10.1.255Broadcast addressKept for compatibility, even though broadcast does not work in a VPC

This matters most for small subnets. Secondary ranges do not lose any addresses:

#SizeAddressesUsable in a primary rangeUsable in a secondary range
1/29 (the smallest allowed)848
2/28161216
3/24 (sn-app)256252256
4/20 (an auto mode subnet)4,0964,0924,096

Not every subnet is for VMs: purpose

Some Google-managed products need a block of addresses inside your VPC for their own machines. You give them a subnet with a special purpose. You cannot put VMs in these subnets, and in most cases the purpose cannot be changed later.

#PurposeWho uses itChapter
1PRIVATE (default)Your VMs, internal load balancer IPs, and similar. Shown as "None" in the console2
2REGIONAL_MANAGED_PROXYProxy-only subnet: the Envoy proxies behind regional and internal Application Load Balancers. Needs a role of ACTIVE or BACKUP11
3GLOBAL_MANAGED_PROXYProxy-only subnet for cross-region internal load balancers11
4PRIVATE_SERVICE_CONNECTUsed when you publish your own service to other VPCs through Private Service Connect9
5PRIVATE_NATSource addresses for Private NAT, which translates between private networks7
6PEER_MIGRATIONA temporary subnet for moving a service from peering to Private Service Connect-

Kadé's load balancer is a global external Application Load Balancer. Its proxies are Google's front ends, which live outside kade-vpc, so it does not need a proxy-only subnet. That changes only if Kadé ever adds a regional or internal Application Load Balancer.

Secondary ranges and alias IPs

A VM's network card normally has one internal IP from the subnet's primary range. It can also carry extra addresses, called alias IP ranges. An alias range can be a single address (/32) or a block, and it can come from the primary range or from a secondary range of the same subnet.

subnet sn-appprimary range10.10.1.0/24 · loses 4 addressessecondary range "apps-containers"10.10.8.0/24 · all 256 usableexample only; not part of Kadé's plankade-api-1 · network card nic0primary IP 10.10.1.10 from the primary rangealias IP 10.10.1.100/32 from the primary rangealias IP range 10.10.8.0/28 from "apps-containers"

GCP creates routes for both ranges automatically, so the rest of the VPC can reach every alias address, and the data plane delivers them all to the same VM.

Wide by design - scroll it sideways.

#UseHow alias IPs help
1Several containers or services on one VM, each with its own IPGive the VM an alias block; the apps inside use addresses from it
2A "floating" IP that can move between two VMsMove the alias from one VM to the other through GCP. This is the replacement for gratuitous ARP (chapter 1.2 section 10)
3Keep a service address separate from the VM's own addressClients use the alias; the VM can be replaced and the alias moved to the new one

Rules for secondary ranges: each needs a name, the smallest is /29, none of their addresses are reserved, they must not overlap any other range in the VPC, and you can remove one only when nothing uses it. Chapter 3 shows how to assign and move alias IPs.

Growing a subnet: expand only

A subnet's primary range can be made bigger by lowering the number after the slash. It can never be made smaller, moved, or put back, so a mistake here is permanent.

The part that surprises people: the bigger range is not "the old range plus more at the end". It is the larger aligned block that contains the old one. For sn-app, /23 means 10.10.0.0/23, which grows backwards into 10.10.0.0/24. That works only because Kadé kept 10.10.0.0/24 free (chapter 0 section 06).

Builds on3. CIDR blocks

Try it. Pick a subnet and a new size, and see whether GCP would allow it and whether it breaks Kadé's plan:

Subnet
New size
Old range
10.10.1.0/24
New range
10.10.0.0/23
Addresses
512 (508 usable)
Grows
backwards and forwards from the old range

Safe: GCP allows it and it stays inside the plan.

#RuleWhat it means
1Only bigger, never smallerExpansion cannot be undone. Grow in small steps; you can always grow again
2No overlapsThe new range must not overlap any other subnet range, secondary range, or private services range in the VPC, or any subnet in a peered VPC. GCP refuses the change
3No interruptionExpansion takes a few minutes and traffic inside the subnet keeps flowing
4Firewall rules do not followA rule that names the old range (for example 10.10.1.0/24) does not cover the new addresses. Update those rules. Kadé's rules use 10.10.0.0/16, which still covers them
5Auto mode subnets stop at /16Anything bigger would collide with another region's auto range
6Proxy-only subnets cannot be expandedCreate a new one instead (chapter 11)

Planning ranges

Builds on2. Private ranges

Because ranges can only grow and can never overlap anything you might connect to, the plan is the one part of a VPC that is expensive to get wrong. These are the rules Kadé's plan in chapter 0 follows:

#RuleWhyIn Kadé's plan
1Never overlap anything you may ever connectPeering and VPN refuse or misroute overlapping ranges, and fixing it means renumberingProd 10.10.0.0/16, staging 10.20.0.0/16, office 172.16.0.0/16, home 192.168.1.0/24
2Keep one block per environmentFirewall rules and routes can then name one range for all of prodAll of prod inside 10.10.0.0/16
3Leave free space next to each subnetSo it can expand later10.10.0.0/24 and .4-.15 kept free
4Size by purpose, not by habitVM subnets rarely need more than a /24; services like Cloud SQL need their own block/24 subnets, a /20 for private services
5Avoid 10.128.0.0/9Auto mode networks use it; overlapping blocks peering or VPN with themNot used
6Avoid 172.17.0.0/16Docker's default bridge network uses it inside VMsNot used
7One subnet per region per job, not one per zoneA subnet already covers every zone of its region. Splitting by zone only wastes ranges and firewall rulessn-app holds kade-api-1 (zone a) and kade-api-2 (zone b)
8Write the plan downGCP does not store it (chapter 0)Chapter 0 section 06

Subnet ranges do not have to be RFC 1918. GCP also accepts 100.64.0.0/10, some other special ranges, and even public ranges used privately. It refuses a few ranges outright, including 169.254.0.0/16 (link-local), 127.0.0.0/8, the private Google API ranges 199.36.153.4/30 and 199.36.153.8/30, and Google's own public addresses. Stick to RFC 1918 unless you have run out.

Subnet switches covered later

Three subnet settings belong to later chapters but are worth knowing now:

#SettingWhat it doesChapter
1Private Google AccessLets VMs without an external IP reach Google APIs (Cloud Storage, Artifact Registry, and so on). It works with the normal default route and normal DNS; the special private.googleapis.com and restricted.googleapis.com addresses are optional extras9
2Flow logsRecords samples of connections to and from VMs in the subnet. Sampling rate, interval and metadata control the cost16
3IPv6 (dual-stack)Adds a /64 IPv6 range to the subnet, internal or external. Kadé stays IPv4 only for now3

Back to Kadé

Kadé's default network has no VMs, so it can go. The order matters: a network cannot be deleted while firewall rules still point at it.

Commands in this chapter

Commands 1 to 5 are read-only. Commands 6 to 8 change the project; run them only after the checks.

List networks and their mode

  • What it shows: Every VPC network in the project and whether it is auto or custom mode.
  • What to look for: A default network, or any AUTO network used for production.
gcloud compute networks list

List subnets in one network

  • What it shows: Each subnet with its region and primary range.
  • What to look for: Ranges that match the plan in chapter 0, and no overlaps.
gcloud compute networks subnets list --network=kade-vpc

See how full each subnet is

  • What it shows: Free and used addresses per subnet. The used count includes the 4 reserved addresses.
  • What to look for: A subnet close to full, which would need expanding.
gcloud compute networks subnets list --network=kade-vpc --view=WITH_UTILIZATION

Describe one subnet

  • What it shows: All of the subnet's properties: gateway, purpose, stack type, Private Google Access, flow logs.
  • What to look for: purpose: PRIVATE, and a missing logConfig block, which means flow logs are off.
gcloud compute networks subnets describe sn-app --region=asia-southeast1

Check nothing uses the default network

  • What it shows: VMs whose network card is attached to default.
  • What to look for: An empty list. If anything shows up, convert the network to custom mode instead of deleting it (section 03).
gcloud compute instances list --filter="networkInterfaces.network~/default$"

Delete the default network's rules (changes the project)

  • What it shows: Removes the four firewall rules that belong to default.
  • What to look for: gcloud asks for confirmation and lists the four rules. Check the names before saying yes.
gcloud compute firewall-rules delete default-allow-internal \  default-allow-ssh default-allow-rdp default-allow-icmp

Delete the default network (changes the project)

  • What it shows: Deletes the network and all its automatic subnets.
  • What to look for: If it fails with "resource is in use", something still references the network. The error names it.
gcloud compute networks delete default

Convert an auto mode network to custom mode (changes the project)

  • What it shows: The alternative to deleting, for a default network that is in use. Existing subnets stay; no new ones appear.
  • What to look for: SUBNET_MODE changes from AUTO to CUSTOM in command 1. This cannot be undone.
gcloud compute networks update default --switch-to-custom-subnet-mode

Spotted a mistake, or something unclear? Suggest an edit on GitHub

See it broken

Harbour is a platform built wrong on purpose. This defect is this chapter's idea, left undone: what it breaks, how you would find it, and the change that closes it.

  1. D12
    No proxy-only subnet

    lowcloses in phase 1, Groundwork

    Goes with Not every subnet is for VMs: purpose