Chapters

Routing

The fundamentals course explained routing tables, longest prefix match and BGP on real routers. In GCP there is no router box (chapter 1.2), but there is still a routing table, and it decides where every packet leaving a VM goes. This chapter covers how that table is built, how GCP picks one route from it, and the patterns you can build with it.

Foundations · chapter 5 of 26 · 14 min read

One route table for the whole VPC

Builds on5. The default gateway12. Routing tables and longest prefix match

A route is a destination range plus a next hop: "packets for this range go there". That part is the same as the fundamentals course. What is different is where the table lives and how you limit it:

#Home router (fundamentals course)AWS VPCGCP VPC
1Where the table livesIn the router boxSeveral route tables, each attached to subnetsOne table for the whole VPC, copied to every host's data plane
2How to make a route apply to only some machinesSeparate router or VLANAttach a different table to their subnetNetwork tags on the route and on the VMs
3"Public" vs "private" subnets-Which table the subnet usesNot a subnet idea at all: done with tags, external IPs and Cloud NAT
4Who creates most routesYou, or DHCPYouGCP, automatically
kade-vpc route table (one for the whole VPC)10.10.1.0/24 → sn-app subnet route · no tags10.10.2.0/24 → sn-data subnet route · no tags0.0.0.0/0 → internet gateway prio 1000 · no tags0.0.0.0/0 → kade-proxy (VM) prio 900tags: via-proxykade-api-1tags: api · sees 3 routeskade-dbno tags · sees 3 routeskade-workertags: via-proxysees all 4 routesonlytagged VMs
  • Your VPC
  • Allowed traffic and replies
  • Firewall and access rules

There is only one table. A tag on a route does not create a second table; it only decides which VMs the route is a candidate for.

Wide by design - scroll it sideways.

That one table lives in the control plane (chapter 1.2). When you add a route, the control plane records it and pushes it to the data plane on every host that runs a kade-vpc VM, in every region. No VM asks a router where to send a packet: its host already holds the whole table. That is also why a change takes a few seconds to reach every host (section 09).

Kadé's route table today

So why can't kade-api reach PayGate (finding 5) if there is a default route? Because a route is only half of the answer. The default internet gateway only accepts traffic from a VM with an external IP, or traffic handled by Cloud NAT. kade-api has neither. The route exists; nothing can use it yet. Chapter 7 fixes that.

The route types

System-generated routes

#RouteWhat it doesCan you change it?
1Subnet routeOne per subnet range (primary, secondary, IPv6). Sends that range inside the VPC. Appears when a subnet is created or expanded, disappears when it is deletedno never edited, and cannot be overridden
2Default route0.0.0.0/0 to the default internet gateway, priority 1000. Created with the networkyes it is an ordinary static route and can be deleted (section 08)
An auto mode network has one subnet route per region, so it starts with dozens of them.

Static routes (custom routes you create)

You give a destination, a next hop, a priority, and optionally tags. The next hop can be:

#Next hopSends matching traffic toKadé's use
1default-internet-gatewayThe internet (a second internet route, often tag-scoped or for a specific range)Routes to Google API ranges (chapter 9)
2An instance (--next-hop-instance)A VM acting as a router, proxy or appliance. It needs IP forwarding on (chapter 3)The scenarios in section 06
3An internal IP (--next-hop-address)The VM that holds that primary internal IPSame as above, by address
4An internal passthrough Network Load Balancer (--next-hop-ilb)A group of appliance VMs behind a load balancer, with health checksAn HA version of the proxy
5A Classic VPN tunnel (--next-hop-vpn-tunnel)Another network across a Classic VPNNot used: HA VPN uses dynamic routes

Dynamic routes

Learned over BGP by a Cloud Router, not typed in. On a VPN or Interconnect, the Cloud Router tells the other side about Kadé's ranges and installs the ranges the other side advertises. When a tunnel goes down, its routes are removed automatically. The VPC's dynamic routing mode (regional or global, chapter 2 section 01) decides whether those routes apply in one region or all of them. This is chapter 15's topic; Kadé's Cloud Router in chapter 7 exists only to run Cloud NAT and learns nothing.

Policy-based routes

A newer, more specialised type. A policy-based route can match on source range, destination range and protocol (not port), and its next hop must be an internal passthrough Network Load Balancer, usually a group of firewall appliances. It is checked before subnet routes, so it is the one tool that can send even VM-to-VM traffic through an appliance. Kadé does not need it.

Special routing paths (hidden)

Some traffic uses paths that are not in your route table at all. You cannot see, delete or override them; only firewall rules can block them.

#RangeUsed byChapter
135.191.0.0/16, 130.211.0.0/22Google Front Ends and health checks talking to your backends11, 12
235.235.240.0/20IAP TCP forwarding (SSH through IAP)6
335.199.192.0/19Cloud DNS forwarding and alternative name servers8
435.199.224.0/19Serverless VPC Access connectors10
This is why the load balancer and IAP keep working even if the default route is deleted.

How GCP picks a route

When a packet leaves a VM, GCP gathers every route that applies to that VM and contains the destination, then removes candidates step by step until one is left. The full list in Google's docs has twelve steps, many about peering and Network Connectivity Center. For a single VPC it comes down to this:

#StepWhat happens
1Special pathsIf the destination is one of the hidden paths in section 03, use it. Done
2Policy-based routesIf one matches (by priority only), use it. Done
3Subnet routesIf the destination is inside any subnet range, use the subnet route. Done. Nothing can beat a subnet route
4Applicable routesKeep static and dynamic routes that contain the destination and apply to this VM (no tags, or a tag the VM has)
5Longest prefixKeep only the most specific destination. Priority is not looked at yet
6Usable next hopsDrop routes whose next hop VM is stopped or deleted, or whose tunnel is down
7Lowest priority numberKeep only routes with the lowest number (0 to 65535, like firewall rules)
8Next hop preferenceInstance and Classic VPN next hops beat dynamic routes, which beat an internal load balancer next hop, which beats the internet gateway
9SendOne route left: send. Several left: share traffic between them (ECMP). None left: drop, with an ICMP "network unreachable"

Route selection, example by example

Each tab is one packet and every route that could carry it. The strip at the top is the selection order from section 04, with the number of routes still in the running under each step. The marked step is the one that decided. Faded rows show the step that removed them and why. Some examples have a "what if" button that changes one thing.

Packet from kade-api-1 (no tags) → 172.16.2.11 · warehouse scanner

Steps 1-2: no special path, no policy-based route

  1. step 3subnet route?3 left
  2. step 4applicable3 left
  3. step 5longest prefixdecides here
  4. step 6next hop alivenot needed
  5. step 7lowest prioritynot needed
  6. step 8next hop typenot needed
  7. step 9send or ECMPnot needed
DestinationSpecificityPriorityNext hopTagsResult
0.0.0.0/0/01000GWinternet gwno tagsout at step 5less specific than /24
172.16.0.0/16/161000VPNvpn tunnel-ano tagsout at step 5less specific than /24
172.16.2.0/24/241000VPNvpn tunnel-bno tagsWins→ vpn tunnel-b

Which ranges contain 172.16.2.11? The inner box is more specific.

0.0.0.0/0 · prio 1000

172.16.0.0/16 · prio 1000

172.16.2.0/24 · prio 1000 ← winner

172.16.2.11

Every box contains the packet. The innermost box is the most specific route. It wins at step 5, whatever the priorities say.

All three routes contain the destination. Step 5 keeps only the /24, so the other two are gone before anyone looks at priority.

Show as config
Packet from kade-api-1 to 172.16.2.11 (warehouse scanner)

candidates:
  0.0.0.0/0      prio 1000  → default-internet-gateway   least specific
  172.16.0.0/16  prio 1000  → vpn tunnel-a
  172.16.2.0/24  prio 1000  → vpn tunnel-b               most specific → WINS

Tag-scoped routes: patterns

With one table per VPC, tags are the only way to make a route apply to some VMs and not others. A route with tags applies only to VMs that have at least one of them; a route with no tags applies to every VM. Tags decide which routes are candidates for a VM. The winner is still picked by the steps in section 04.

Six common patterns follow. Pick a scenario, then a VM, and watch which route its packet gets. The "what if" buttons stop a next hop or remove a tag; that is where most of these patterns surprise people.

What if

Goal: kade-worker's internet traffic goes through kade-proxy; every other VM goes direct.

Packet from
VMs and their tagskade-vpc route tablenext hopkade-api-1tags: apikade-dbno tagskade-workertags: via-proxy0.0.0.0/0 · prio 1000for every VMloses on priorityinternet gatewaystraight out0.0.0.0/0 · prio 900for tag via-proxywinskade-proxyVM · then the internet

kade-worker → 8.8.8.8: kade-proxy. Both apply; priority 900 beats 1000.

The tag decides who sees route 2; priority picks the winner. Only kade-worker has both routes, and 900 beats 1000. kade-proxy needs IP forwarding on (chapter 3) and its own way out: an external IP or Cloud NAT.

Show as config
Route 1:  0.0.0.0/0 → default-internet-gateway   prio 1000   no tags (all VMs)
Route 2:  0.0.0.0/0 → instance kade-proxy        prio 900    tags: [via-proxy]

kade-worker (via-proxy):  both apply, both /0 → 900 beats 1000 → kade-proxy
kade-api-1 (no such tag): route 2 does not apply → route 1 → direct
#ScenarioGoalThe lesson
ASome VMs through a proxykade-worker's internet traffic goes through kade-proxyA tagged route with a lower priority number wins, but only for tagged VMs. If kade-proxy stops, traffic goes direct
BNo internet for some VMskade-db never reaches the internetDelete the untagged default route and add it back with a tag. No route means no path, but every VM that needs the internet must carry the tag
CA blackhole VMkade-db's traffic is sent to a VM that drops itIt leaks when the VM stops: the default route has the same destination and takes over. Use B or a firewall rule
DOnly some destinationsOnly kade-worker's PayGate calls go through kade-inspectSpecificity does the work; the tag only limits who sees the route. If kade-inspect stops, PayGate calls are dropped
EA tagged pair of VPN tunnelsTagged VMs send everything to the office over two tunnelsEqual routes share connections (ECMP). One tunnel down, the other carries everything
FA VM with two tagskade-worker has via-proxy and inspectedTags decide which routes are candidates; the normal rules still pick the winner

Three things the widget does not show. kade-proxy, kade-inspect and blackhole-vm need IP forwarding on (chapter 3), and the first two need their own way out: an external IP or Cloud NAT. Cloud NAT only works for VMs that can see a default route to the internet gateway (section 08), which matters in Scenario B. And HA VPN uses dynamic routes from BGP, which cannot carry tags, so Scenario E works only with Classic VPN (chapter 15).

Try it: which route wins?

This route table is a lab version of kade-vpc with every route from sections 05 and 06 in it. Give the sending VM the via-proxy or inspected tag to bring the tagged routes into play, choose a destination, and switch next hops off to see what takes over. Try 35.191.4.7 too: a packet to a special path never reaches the table at all.

Tags on the sending VM

Next hops that are down

kade-paygate wins at step 5: 192.0.2.0/24 → instance kade-inspect.

  1. step 1special path?8 left
  2. step 2policy route?8 left
  3. step 3subnet route?6 left
  4. step 4applicable2 left
  5. step 5longest prefixdecides here
  6. step 6next hop alivenot needed
  7. step 7lowest prioritynot needed
  8. step 8next hop typenot needed
  9. step 9send or ECMPnot needed
DestinationSpecificityPriorityNext hopTagsResult
10.10.1.0/24/24-SUBNETinside kade-vpcno tagsout at step 3destination is outside this subnet
10.10.2.0/24/24-SUBNETinside kade-vpcno tagsout at step 3destination is outside this subnet
0.0.0.0/0/01000GWinternet gwno tagsout at step 5less specific than /24
0.0.0.0/0/0900VMkade-proxyvia-proxyout at step 4VM lacks tag via-proxy
192.0.2.0/24/241000VMkade-inspectinspectedWins→ instance kade-inspect
172.16.0.0/16/161000VPNvpn tunnel-ano tagsout at step 4does not contain the destination
172.16.0.0/16/161000VPNvpn tunnel-bno tagsout at step 4does not contain the destination
172.16.2.0/24/241000VPNvpn tunnel-bno tagsout at step 4does not contain the destination

Deleting the default route

The default route is only a static route, so it can be deleted. People do this to "make sure nothing can reach the internet". Before doing it, know what depends on it:

#FeatureWithout a route to default-internet-gatewayChapter
1VMs with external IPsbreaks no outgoing path to the internet3
2Cloud NATbreaks public Cloud NAT always needs a route to the internet gateway7
3Private Google Access with the normal API addressesbreaks unless you add routes for the Google API ranges9
4Traffic inside the VPCworks subnet routes are separate4
5Load balancer and health checks, IAP, Cloud DNS forwardingworks special paths do not use the route table6, 8, 11
6External passthrough Network Load Balancer backendsworks they have their own special path11
"No default route" does not mean "no internet exposure": load balancer traffic still reaches backends. Firewall rules are what actually control exposure.

Things to know before changing routes

#FactWhat it means in practice
1Static routes cannot be editedTo change one, create the new route first, check it, then delete the old one
2Changes spread out over a few secondsRoute changes reach every data plane "eventually". Several changes sent together may apply in any order
3An instance next hop needs IP forwardingWithout canIpForward the VM drops forwarded packets (chapter 3)
4GCP does not check the applianceIt only checks the next hop VM is running, not that its software works. Use an internal load balancer next hop with health checks for anything important
5A route is not permissionThe firewall still decides whether the packet is allowed (chapter 5)
6Auto mode networksDo not create static routes inside 10.128.0.0/9; a new region's subnet can disable them

Routes and geography

A route belongs to kade-vpc, so it is global: one route applies to matching VMs in every region. What it points at is not always global. The next hop has a scope of its own (chapter 1.1), and that decides what a zone failure does to the route.

control plane · one route table0.0.0.0/0 → kade-proxy · tag via-proxyasia-southeast1asia-south1zone adata planehas the routezone bdata planehas the routezone cdata planehas the routezone adata planehas the routekade-proxynext hop · zonaltagged VMvia-proxyno tagged VMstagged VMvia-proxyacross regions: slower, and billed per GB
  • Google's: the control plane, regions, zones and data planes
  • The proxy route and the VMs it applies to

Every host holds the route, but the route leads to one VM in one zone. A tagged VM in another region crosses regions to reach it.

Wide by design - scroll it sideways.

#PieceScopeIf a zone fails
1A route, of any typeGlobalNothing happens to it. It lives in the control plane and on every host, not in one zone
2Next hop: the default internet gatewayGlobalNothing to fail on Kadé's side
3Next hop: an instance, such as kade-proxyZonalIf zone a fails, the path through kade-proxy fails with it
4Next hop: an internal passthrough Network Load BalancerRegionalBackends in two zones and health checks keep the path up
5Cloud Router and its dynamic routesRegionalThe routing mode decides whether its learned routes apply in its own region only or in every region (section 03)

Two rules follow:

  • One next hop VM is one zone. For anything that matters, use a load balancer next hop (row 4), the same lesson as chapter 1.1's outage.
  • Keep the next hop near the traffic. A tagged VM in asia-south1 would send its internet traffic to Singapore and only then out: slower, and billed as traffic between regions (chapter 1.1). One proxy per region, each with its own tag, avoids the detour.

Back to Kadé

Commands in this chapter

Commands 1 to 3 are read-only. Commands 4 and 5 change the project and are shown for the scenarios, not for Kadé today.

List routes in a network

  • What it shows: Every route in kade-vpc: destination, next hop and priority.
  • What to look for: One subnet route per subnet, the default route, and any custom route you do not recognise.
gcloud compute routes list --filter="network~kade-vpc"

Show custom routes with their tags

  • What it shows: Routes with the tags that limit them. Subnet routes have no tags.
  • What to look for: A tagged route, and whether the VMs that should carry the tag actually do.
gcloud compute routes list --filter="network~kade-vpc" \  --format="table(name,destRange,nextHopGateway.basename(),nextHopInstance.basename(),priority,tags.list())"

See a VM's tags

  • What it shows: The network tags on one VM, which decide which tagged routes (and firewall rules) apply to it.
  • What to look for: A missing or misspelled tag. Tags are case-sensitive and silently match nothing when wrong.
gcloud compute instances describe kade-worker --zone=asia-southeast1-a \  --format="value(tags.items)"

Create a tag-scoped route (changes the project)

  • What it shows: Scenario A: VMs tagged via-proxy send internet traffic to kade-proxy.
  • What to look for: The route in command 2 with its tag. Check with the Console's Effective routes view for kade-worker before relying on it.
gcloud compute routes create kade-via-proxy \  --network=kade-vpc --destination-range=0.0.0.0/0 --priority=900 \  --next-hop-instance=kade-proxy --next-hop-instance-zone=asia-southeast1-a \  --tags=via-proxy

Add or remove a tag on a VM (changes the project)

  • What it shows: Puts kade-worker under the tagged route, or takes it back out.
  • What to look for: The change reaches the data plane within seconds. Tags also affect firewall rules, so check both.
gcloud compute instances add-tags kade-worker --zone=asia-southeast1-a --tags=via-proxygcloud compute instances remove-tags kade-worker --zone=asia-southeast1-a --tags=via-proxy

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