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 decides | The question it answers | Chapter |
|---|---|---|---|
| 1 | Which routes apply | Where can a packet be sent? | 4 |
| 2 | Which firewall rules apply | Which packets are allowed? | 5 |
| 3 | Which IP ranges are handed out (through its subnets) | What addresses can be given to things? | 2, 3 |
| 4 | Which resources can reach each other on internal IPs | Who 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.
- 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:
- 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.
| # | AWS | GCP | |
|---|---|---|---|
| 1 | A VPC lives in | One region | Every region (global) |
| 2 | A subnet lives in | One zone (an Availability Zone) | One region, across all its zones |
| 3 | VMs in two zones need | Two subnets | One subnet |
| 4 | A second region needs | A second VPC, joined by peering or a transit gateway | A new subnet in the same VPC |
| 5 | Traffic between regions | Goes through the peering or transit gateway | Stays 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 setting | Kadé's value | Change later? | Chapter |
|---|---|---|---|---|
| 1 | Name | kade-vpc | no | 2 |
| 2 | Subnet creation mode | custom | auto → custom only | 2 |
| 3 | MTU | 1460 | yes, with VMs stopped | 1 |
| 4 | Dynamic routing mode | regional | yes | 4, 15 |
| 5 | Internal IPv6 range (a /48 ULA) | none | add once, never change | 3 |
| 6 | An IPv4 range | does 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:
| # | Region | Auto mode subnet | Gateway |
|---|---|---|---|
| 1 | us-central1 | 10.128.0.0/20 | 10.128.0.1 |
| 2 | europe-west1 | 10.132.0.0/20 | 10.132.0.1 |
| 3 | us-west1 | 10.138.0.0/20 | 10.138.0.1 |
| 4 | asia-east1 | 10.140.0.0/20 | 10.140.0.1 |
| 5 | asia-southeast1 (Kadé's region) | 10.148.0.0/20 | 10.148.0.1 |
| 6 | asia-south1 | 10.160.0.0/20 | 10.160.0.1 |
That convenience has a cost:
| # | Problem with auto mode | Why it matters |
|---|---|---|
| 1 | The ranges are fixed and public | Anyone who gets onto one VM already knows the whole internal address plan, without running a single command |
| 2 | Every auto mode network uses the same ranges | Two auto mode networks can never be joined with peering or VPN: their ranges overlap by design |
| 3 | 10.128.0.0/9 is a common private range | An office or another cloud may already use part of it, which blocks a VPN later |
| 4 | Subnets appear in regions you never use | More ranges to keep track of, and new ones arrive without anyone deciding |
| 5 | No IPv6 | Auto 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:
| # | Rule | Source | Allows | Real risk | Usual action |
|---|---|---|---|---|---|
| 1 | default-allow-internal | 10.128.0.0/9 | every TCP and UDP port, and ICMP | Every VM can reach every port on every other VM: no separation at all | Replace with rules for what really talks to what (chapter 5) |
| 2 | default-allow-ssh | 0.0.0.0/0 | tcp:22 | Any VM with an external IP is open to every SSH bot on the internet | Limit to IAP's range 35.235.240.0/20 (chapter 6) |
| 3 | default-allow-rdp | 0.0.0.0/0 | tcp:3389 | The same for Windows remote desktop | Delete if there are no Windows VMs |
| 4 | default-allow-icmp | 0.0.0.0/0 | icmp | Small: lets anyone confirm a VM exists | Often left, or limited to known IPs |
What to do depends on whether anything uses it:
| # | Situation | What to do |
|---|---|---|
| 1 | Nothing uses it (Kadé) | Delete its four firewall rules, then delete the network. Its auto subnets go with it |
| 2 | VMs or other resources already live in it | You 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 |
| 3 | For every future project | Set 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:
| # | Property | What it is | Change later? | Notes |
|---|---|---|---|---|
| 1 | name | The subnet's name | no | Unique per project per region. To rename, create a new one and delete the old one once nothing uses it |
| 2 | region | The region it lives in | no | Covers every zone in that region (chapter 1.1) |
| 3 | network | The VPC it belongs to | no | A subnet cannot move to another VPC |
| 4 | ipCidrRange | The primary IPv4 range | grow only | Can be expanded, never shrunk or moved. Must not overlap any other range in the VPC. Loses 4 addresses (section 05). Smallest is /29 |
| 5 | secondaryIpRanges | Extra named ranges | add / remove | Only for alias IPs (section 07). Removable only when nothing uses them |
| 6 | gatewayAddress | The subnet's gateway | read-only | Always the second address of the primary range, the .1 |
| 7 | purpose | What the subnet is for | mostly no | PRIVATE for VMs; other values are for Google-managed things (section 06) |
| 8 | role | Active or backup | yes | Only for proxy-only subnets: ACTIVE or BACKUP |
| 9 | stackType | IPv4 only, dual-stack, or IPv6 only | limited | IPv4-only can become dual-stack. Dual-stack can go back to IPv4-only only if its IPv6 is external. IPv6-only never changes |
| 10 | ipv6AccessType | Internal or external IPv6 | no | Only when the subnet has IPv6 |
| 11 | privateIpGoogleAccess | Reach Google APIs without an external IP | yes | Off by default (chapter 9) |
| 12 | logConfig (flow logs) | Record samples of traffic | yes | Off 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:
- 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-app | Name | Why it is kept |
|---|---|---|---|
| 1 | 10.10.1.0 | Network address | Names the subnet itself, so it cannot be given to a machine |
| 2 | 10.10.1.1 | Default gateway | The address the data plane answers for (chapter 1.2). Not a real router |
| 3 | 10.10.1.254 | Second-to-last | Kept by Google for possible future use; it does nothing today |
| 4 | 10.10.1.255 | Broadcast address | Kept for compatibility, even though broadcast does not work in a VPC |
This matters most for small subnets. Secondary ranges do not lose any addresses:
| # | Size | Addresses | Usable in a primary range | Usable in a secondary range |
|---|---|---|---|---|
| 1 | /29 (the smallest allowed) | 8 | 4 | 8 |
| 2 | /28 | 16 | 12 | 16 |
| 3 | /24 (sn-app) | 256 | 252 | 256 |
| 4 | /20 (an auto mode subnet) | 4,096 | 4,092 | 4,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.
| # | Purpose | Who uses it | Chapter |
|---|---|---|---|
| 1 | PRIVATE (default) | Your VMs, internal load balancer IPs, and similar. Shown as "None" in the console | 2 |
| 2 | REGIONAL_MANAGED_PROXY | Proxy-only subnet: the Envoy proxies behind regional and internal Application Load Balancers. Needs a role of ACTIVE or BACKUP | 11 |
| 3 | GLOBAL_MANAGED_PROXY | Proxy-only subnet for cross-region internal load balancers | 11 |
| 4 | PRIVATE_SERVICE_CONNECT | Used when you publish your own service to other VPCs through Private Service Connect | 9 |
| 5 | PRIVATE_NAT | Source addresses for Private NAT, which translates between private networks | 7 |
| 6 | PEER_MIGRATION | A 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.
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.
| # | Use | How alias IPs help |
|---|---|---|
| 1 | Several containers or services on one VM, each with its own IP | Give the VM an alias block; the apps inside use addresses from it |
| 2 | A "floating" IP that can move between two VMs | Move the alias from one VM to the other through GCP. This is the replacement for gratuitous ARP (chapter 1.2 section 10) |
| 3 | Keep a service address separate from the VM's own address | Clients 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:
- 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.
| # | Rule | What it means |
|---|---|---|
| 1 | Only bigger, never smaller | Expansion cannot be undone. Grow in small steps; you can always grow again |
| 2 | No overlaps | The 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 |
| 3 | No interruption | Expansion takes a few minutes and traffic inside the subnet keeps flowing |
| 4 | Firewall rules do not follow | A 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 |
| 5 | Auto mode subnets stop at /16 | Anything bigger would collide with another region's auto range |
| 6 | Proxy-only subnets cannot be expanded | Create 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:
| # | Rule | Why | In Kadé's plan |
|---|---|---|---|
| 1 | Never overlap anything you may ever connect | Peering and VPN refuse or misroute overlapping ranges, and fixing it means renumbering | Prod 10.10.0.0/16, staging 10.20.0.0/16, office 172.16.0.0/16, home 192.168.1.0/24 |
| 2 | Keep one block per environment | Firewall rules and routes can then name one range for all of prod | All of prod inside 10.10.0.0/16 |
| 3 | Leave free space next to each subnet | So it can expand later | 10.10.0.0/24 and .4-.15 kept free |
| 4 | Size by purpose, not by habit | VM subnets rarely need more than a /24; services like Cloud SQL need their own block | /24 subnets, a /20 for private services |
| 5 | Avoid 10.128.0.0/9 | Auto mode networks use it; overlapping blocks peering or VPN with them | Not used |
| 6 | Avoid 172.17.0.0/16 | Docker's default bridge network uses it inside VMs | Not used |
| 7 | One subnet per region per job, not one per zone | A subnet already covers every zone of its region. Splitting by zone only wastes ranges and firewall rules | sn-app holds kade-api-1 (zone a) and kade-api-2 (zone b) |
| 8 | Write the plan down | GCP 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:
| # | Setting | What it does | Chapter |
|---|---|---|---|
| 1 | Private Google Access | Lets 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 extras | 9 |
| 2 | Flow logs | Records samples of connections to and from VMs in the subnet. Sampling rate, interval and metadata control the cost | 16 |
| 3 | IPv6 (dual-stack) | Adds a /64 IPv6 range to the subnet, internal or external. Kadé stays IPv4 only for now | 3 |
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
defaultnetwork, or anyAUTOnetwork used for production.
gcloud compute networks listList 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-vpcSee 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_UTILIZATIONDescribe 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 missinglogConfigblock, which means flow logs are off.
gcloud compute networks subnets describe sn-app --region=asia-southeast1Check 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-icmpDelete 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 defaultConvert 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_MODEchanges fromAUTOtoCUSTOMin command 1. This cannot be undone.
gcloud compute networks update default --switch-to-custom-subnet-mode