Chapters

Kadé on GCP

These notes continue the fundamentals course. Same company, same addresses, but now we open the Google Cloud project itself and look at how its network really works. Come back to this page whenever a name or an address looks unfamiliar.

Start here · Reference, read this first · 15 min read

Where the fundamentals course left off

Kadé is the small online grocery shop in Colombo. Customers use a mobile app and a website, and both call one backend at api.kade.lk. You are Kadé's backend engineer. From now on you are also the only person looking after its Google Cloud setup.

The fundamentals course used GCP as the place where Kadé's servers live, and looked at it mostly from the outside: packets arriving, a load balancer in front, a gateway at .1. By the end, this much was built:

#PieceWhat it isBuilt in
1kade-vpcKadé's network in GCP, with subnets sn-app 10.10.1.0/24 and sn-data 10.10.2.0/24Fundamentals ch 3
2kade-api-1, kade-api-2Two VMs running the Node app, no external IPsFundamentals ch 33
3kade-dbOne VM running PostgreSQL, 10.10.2.5:5432Fundamentals ch 4
4Load balancerExternal Application Load Balancer at 34.120.88.10. api.kade.lk points hereFundamentals ch 33
5Cloud DNSHosts the kade.lk zoneFundamentals ch 22
6Two firewall rulesPing from your home and office IPs; health checks from Google's load balancer rangesFundamentals ch 13, 33

Some things were only planned there, never built: Cloud NAT with a fixed IP for the payment provider (Fundamentals ch 10), a VPN to the office (Fundamentals ch 0), a rule that lets only kade-api reach the database (ch 0), and SSH through IAP instead of an open port 22 (ch 29).

These notes pick up at that point. Every chapter starts with a real problem in Kadé's project and ends with it fixed, and every new idea is tied back to the fundamentals course chapter it builds on.

One correction first: a VPC has no address range

Builds on3. Networks, hosts and CIDR5. The default gateway

The fundamentals course map said kade-vpc 10.10.0.0/16. That was a useful shortcut, but it is not how GCP works. In GCP, a VPC network has no address range of its own. Only subnets have ranges. A home router is set up with one LAN range; a VPC is not.

So 10.10.0.0/16 is Kadé's own plan: the block we promise to take every subnet range from. GCP does not know about it and does not enforce it. That has three results:

#What this meansExampleChapter
1A subnet can use any private range, even one far outside our planGCP would accept a new subnet 192.168.50.0/24 in kade-vpc2
2Two VPCs can use the same ranges, and nothing warns you until you try to connect themA staging VPC that also uses 10.10.1.0/24 cannot be peered with kade-vpc14
3The plan must be written down somewhere, because GCP will not hold it for youSection 06 of this page is that record0, 17

The project today

This is what you find the first time you open the project and look properly. Red numbers are problems; section 04 lists them.

outside GCPShoppersapp + websiteYou (home)public IP 203.0.113.45Kadé officepublic IP 198.51.100.20PayGateallows only 34.87.200.7Cloud DNSapi → 34.120.88.10Load balancer34.120.88.10 (anycast)Google front endsHTTP :8080 to both VMsproject kade-prod · region asia-southeast1kade-vpc · custom mode · no range of its own8no logs9VMs use the default service accountsn-app · 10.10.1.0/24 · gateway .1kade-api-110.10.1.10 · zone atag api · no external IPkade-api-210.10.1.11 · zone btag api · no external IPsn-data · 10.10.2.0/24kade-db (VM, PostgreSQL)10.10.2.5:5432 · zone b · backups by hand7SQL to hardcoded IP6firewall rules (priority 1000; names start with kade-)nameportsfrom2allow-sshtcp:220.0.0.0/03allow-webtcp:80,443,80800.0.0.0/04allow-internalall10.10.0.0/16allow-lbtcp:8080Google LB rangesallow-icmpicmphome + office1default network · auto mode · 10.128.0.0/9a /20 subnet in every region · no VMs todayrules open ssh, rdp and icmp to 0.0.0.0/0,and allow everything from 10.128.0.0/9SSH via a temporaryexternal IP✕5no way out:no external IP, no NAT
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

Traffic from shoppers takes the right path: DNS, then the load balancer, then kade-api on port 8080. Everything around that path is either too open or missing. Solid lines work today; red dashed lines are problems.

Wide by design - scroll it sideways.

What is wrong today

Nothing here is unusual. This is what a project looks like when it was set up quickly with default settings, and it is close to what many real projects look like on their first audit.

#What we foundWhy it is a problemFixed in
1The default network still existsGCP creates it with every new project. It has a subnet in every region and rules that open SSH, RDP and ping to the whole internet. No VM uses it today, but a VM created with default settings lands in itCh 2, 17
2kade-allow-ssh: port 22 from 0.0.0.0/0 to every VMAny VM that gets an external IP can be reached by every SSH bot on the internetCh 6.1
3kade-allow-web: 80, 443, 8080 from 0.0.0.0/0 to every VMLeft over from when kade-api had its own external IP. It does nothing today, but the day any VM gets an external IP, the app is open without the load balancer in frontCh 5.1
4kade-allow-internal: every port from 10.10.0.0/16 to every VMAny VM in kade-vpc can reach kade-db on any port, not only kade-api on 5432Ch 5.1
5kade-api has no way to reach the internetNo external IP and no Cloud NAT, so apt update hangs and calls to PayGate fail. The workaround so far is to add a temporary external IP, which together with rules 2 and 3 opens the VM to the internetCh 7
6kade-api finds the database by a hardcoded IP, 10.10.2.5Moving or replacing the database means changing app config everywhereCh 8
7The database is a VM that you patch and back up by handEasy to forget, and it has to stay reachable for admin workCh 9
8Nothing is logged: no firewall rule logging, no VPC flow logsWhen something is blocked or attacked, there is no record of itCh 16
9All VMs run as the default Compute Engine service accountFirewall rules cannot tell the VMs apart by identity (chapter 5). The IAM side of this is outside these notesCh 5.1

Where Kadé is going

This is the project after the last chapter. Each piece carries the chapter where it arrives. Dashed boxes belong to Google's side of the network, or to another project.

outside GCPShoppersapp + websiteYou (home)203.0.113.45PayGateallows only 34.87.200.7Kadé office172.16.0.0/16CloudflareDNS for kade.lk · WAFch 13Load balancer34.120.88.10accepts only Cloudflarech 11-13IAPfrom 35.235.240.0/20ch 6kade-stagingproject · 10.20.0.0/16ch 14project kade-prod · org policies on (ch 17)Cloud Run: kade-apiingress: internal + LBegress: into sn-runch 10kade-vpc · custom mode · plan 10.10.0.0/16sn-app · 10.10.1.0/24kade-worker10.10.1.20 · ch 10sn-run · 10.10.3.0/24Cloud Run's outgoingtraffic uses IPs herech 10Cloud NATfixed IP 34.87.200.7ch 7Private DNS zonedb.kade.internalch 8Firewallrules target service accounts · only needed portsch 5, 16private services range 10.10.32.0/20Google's side, joinedby peeringCloud SQL: kade-sqlprivate IP 10.10.32.3ch 9HA VPN + Cloud RouterBGP · ASN 64512ch 15Logs onflow, firewall, NATch 16
  • Run by Google
  • Your VPC
  • Allowed traffic and replies
  • Firewall and access rules

Shoppers now reach Kadé only through Cloudflare and the load balancer. Nothing in kade-vpc has an external IP. Admin access goes through IAP, outgoing calls through Cloud NAT, and the database is reachable only on a private IP. The default network is gone.

Wide by design - scroll it sideways.

The same change, piece by piece:

#PieceTodayTargetChapter
1default networkExists, with open rulesDeleted; an org policy stops new projects from getting one2, 17
2FirewallThree broad rules for every VMRules aimed at service accounts, only the ports each one needs5
3Admin accessTemporary external IP, port 22 open to allIAP; port 22 only from 35.235.240.0/206
4Outgoing callsNone, or a temporary external IPCloud NAT with a fixed IP, 34.87.200.7, on PayGate's allow list7
5Finding the databaseHardcoded 10.10.2.5db.kade.internal in a private DNS zone8
6Databasekade-db VMCloud SQL kade-sql, private IP only9
7The APITwo VMsCloud Run kade-api, reachable only through the load balancer10
8Background jobsInside the API VMsOne VM, kade-worker, no external IP10
9Front doorLoad balancer onlyCloudflare, then the load balancer, which accepts only Cloudflare11-13
10StagingNoneProject kade-staging, 10.20.0.0/1614
11OfficeOver the public internetHA VPN and Cloud Router (BGP) into kade-vpc15
12LogsNoneVPC flow logs, firewall rule logs, NAT logs16

The address plan

Builds on2. Private ranges3. CIDR

Because GCP will not hold the plan for us (section 02), this is the record. Every range Kadé uses is decided here once, and no two ranges overlap. Overlaps are cheap to avoid now and very hard to fix later, when networks need to be joined (chapters 14 and 15).

10.10.0.0/16 · Kadé's block in kade-vpc · one cell = one /20 (4,096 addresses).0.0/20subnets.32.0/20free for later.192.0/20.240.0/20private services (ch 9).200.0/24 kept for PSC (ch 9)zoom into the first cell, 10.10.0.0/20 · one cell = one /24 (256 addresses).0.1.2.3.4.15unusedsn-appsn-datasn-run (ch 10)free for new subnets

Solid teal: subnets that exist. Dashed teal: planned subnet. Dashed plum: handed to Google for managed services. Yellow: reserved. Grey: free.

Wide by design - scroll it sideways.

Every address used in these notes. Rows marked "Fundamentals" were already used there; the rest are new.

#PlaceThingAddressFirst used
1HomeHome LAN · your laptop192.168.1.0/24laptop 192.168.1.23Fundamentals
2HomeHome router, public side203.0.113.45Fundamentals
3OfficeOffice LAN (staff .1.0/24, warehouse .2.0/24)172.16.0.0/16Fundamentals
4OfficeOffice router, public side198.51.100.20Fundamentals
5GCPKadé's plan for kade-vpc (not a GCP setting)10.10.0.0/16Ch 0
6GCPSubnet sn-app · gateway10.10.1.0/24gateway 10.10.1.1Fundamentals
7GCPkade-api-1, kade-api-2 (tag api)10.10.1.10 · 10.10.1.11Fundamentals
8GCPSubnet sn-data · kade-db10.10.2.0/24kade-db 10.10.2.5:5432, removed in ch 9Fundamentals
9GCPLoad balancer front end34.120.88.10global, anycastFundamentals
10GCPCloud NAT fixed outgoing IP (on PayGate's allow list)34.87.200.7Ch 7
11GCPPrivate services range, for Cloud SQL10.10.32.0/20Ch 9
12GCPCloud SQL kade-sql, private IP10.10.32.3Ch 9
13GCPKept for Private Service Connect endpoints10.10.200.0/24Ch 9
14GCPSubnet sn-run, for Cloud Run's outgoing traffic10.10.3.0/24Ch 10
15GCPkade-worker VM10.10.1.20Ch 10
16GCPkade-staging project's block10.20.0.0/16Ch 14
17VPNBGP ASNs: Cloud Router · office router64512 · 65010Ch 15
All addresses are examples. 203.0.113.x and 198.51.100.x are documentation ranges. The 34.x addresses belong to Google in real life; here they stand in for Kadé's.

Google's own addresses you will meet

These are real, fixed ranges that Google uses. They show up in firewall rules and logs, so it is worth knowing them on sight.

#RangeWhat it isWhere you meet itChapter
1169.254.169.254Metadata server, on every VMDNS, time, VM details, service account tokens1, 8
210.128.0.0/9Ranges used by auto mode networks like defaultdefault-allow-internal2
335.191.0.0/16 · 130.211.0.0/22Google front ends and health checkersSource IPs on kade-api; kade-allow-lb11, 12
435.235.240.0/20IAP TCP forwardingSource IP of SSH through IAP6
5199.36.153.8/30private.googleapis.comReaching Google APIs without the internet9
6199.36.153.4/30restricted.googleapis.comThe same, limited to services that support VPC Service Controls9, 17

Names used in every chapter

Resources get the same names in every diagram, command and table. Commands in these notes are written for these names, so you can read them as if the project were real.

#KindNameFirst used
1Project · region · zoneskade-prod · asia-southeast1 (Singapore) · -a, -bCh 0
1bOrganization · folderkade.lk · productionCh 5.2
2VPC network · subnetskade-vpc · sn-app, sn-data, later sn-runCh 0, 10
3VMskade-api-1, kade-api-2, kade-db, later kade-workerCh 0, 10
4Service accounts (used as firewall targets)sa-kade-api, sa-kade-db, sa-kade-workerCh 5.1
5Cloud Router · Cloud NAT · its IPkade-router · kade-nat · kade-nat-ipCh 7
6DNS zones: public · privatekade-lk · kade-internalCh 8
7Private services range · Cloud SQLkade-psa-range · kade-sqlCh 9
8Cloud Run servicekade-apiCh 10
9Load balancer partskade-lb-ip, kade-https-rule, kade-https-proxy, kade-url-map, kade-api-backend, kade-hcCh 11, 12
10Cloud Armor policykade-edge-policyCh 12, 13
11Staging projectkade-stagingCh 14
12HA VPN · its Cloud Router · office sidekade-havpn · kade-vpn-router · kade-office-gwCh 15

Try it: where does this address belong?

Builds on12. Longest prefix match

Type any IPv4 address. The tool checks it against every range on this page and lists all the ranges it falls in, most specific first. This is the same longest-prefix idea that routing uses, and chapter 4 uses it again for GCP routes.

Best match: kade-api-1 (/32).

RangeWhat it isFirst used
10.10.1.10/32kade-api-1VM in sn-app, zone aCh 0
10.10.1.0/24sn-appSubnet for app VMsCh 0
10.10.0.0/16Kadé plan for kade-vpcNot a GCP setting, only our planCh 0
10.0.0.0/8Private rangeAny network may reuse itFundamentals ch 2
0.0.0.0/0The whole internetMatches every address: the default routeFundamentals ch 5

5 ranges contain 10.10.1.10. The longest prefix, at the top, is the most specific answer.

How to read these notes

The layout is the same as the fundamentals course: numbered sections and diagrams, with a list of every command used at the end of each chapter. Colours mean something in every diagram:

#ColourMeansExamples
1TealYour VPC: subnets, internal IPs, routes, NATsn-app, kade-api-1, Cloud NAT
2PlumRun by Google, outside your VPCLoad balancer front ends, Cloud Run, Cloud SQL, IAP
3AmberFirewall and access ruleskade-allow-ssh, firewall policies
4InkNetworks outside GCPShoppers, home, office, Cloudflare, PayGate
5GreenAllowed traffic and repliesA rule that matches and allows
6RedProblems and blocked trafficNumbered findings, dropped packets

The fundamentals course used colour for layers (TCP amber, IP green, and so on). Here the layers matter less than who owns a piece and whether traffic is allowed, so the colours change meaning. Where a chapter shows packet layers again, it says so in the caption.

Besides the boxes you know from the fundamentals course (definitions and notes), these notes add a few new ones:

Builds on10. NAT

A "Builds on" line sits under a heading and points to the fundamentals chapter the section builds on. If the idea feels new, read that chapter first.

The road ahead

Each chapter fixes one part of section 04 or adds one piece from section 05. The last column is what to re-read in the fundamentals course if it has faded.

#ChapterKadé's problemRecall first (fundamentals course)
0Kadé on GCP this pageKnow the starting pointScenario page
1.1Regions, zones and Google's networkIf zone b goes dark tonight, does the shop stay up?Ch 12, 33
1.2The mental model: Andromeda and no layer 2Where did the router and the switch go?Ch 5, 9, 18, 32
2VPC networks and subnetsThe default network is still thereCh 3
3IP addressingAn external IP changes after a restartCh 2, 3, 10
4RoutingA packet went a way nobody expectedCh 5, 12
5.1Firewall 1: VPC firewall rulesFindings 3, 4 and 9Ch 16, 31
5.2Firewall 2: firewall policiesKeep the fixes from being undone; share rules across projectsNone
6.1Admin access 1: IAP TCP forwardingPort 22 is open to everyone (finding 2)Ch 29
6.2Admin access 2: OS Login and no external IPsOld SSH keys still work on every VMCh 29
7Egress and Cloud NATNo way out; PayGate needs one fixed IPCh 10
8DNS inside GCPThe database is a hardcoded IPCh 22, 23
9Private access to Google servicesMove the database to Cloud SQL without a public IPCh 7, 10
10Serverless networkingMove the API to Cloud Run and keep the fixed IPCh 10, 33
11Load balancing 1: the family and how to chooseWhich of the many load balancers is ours, and whyCh 33
12.1Load balancing 2: inside the HTTPS load balancerHealth checks fail; the shopper's IP is lostCh 25, 33
12.2Load balancing 3: VMs behind the load balancerA VM service that cannot scale, heal or update itselfCh 33
13Cloudflare and GCPTraffic can skip Cloudflare and hit the load balancerCh 22, 25, 33
14A second VPC, brieflyKadé adds a staging project: connect it or not?Ch 6
15Hybrid: VPN and InterconnectThe office needs a private path to GCPCh 12, 18
16.1Observability 1: what each log can tell youNobody can see what happened to a requestCh 33
16.2Observability 2: monitoring, alerting and troubleshootingOutages are reported by shoppers firstCh 13
17Governance: keeping it fixedFixes keep drifting back; too many OwnersNone
18.1Capstone 1: a photo, through a VM to the databaseFollow one request through everything, then break itCh 34
18.2Capstone 2: a payment, through Cloud Run to PayGateThe other backend and outgoing traffic; the whole network on one pageCh 34

Commands in this chapter

All read-only. Replace kade-prod with your own project ID to run them on a real project.

Set the default project

  • What it shows: Sets kade-prod as the project for every later command.
  • What to look for: Nothing to check. It only saves typing --project each time.
gcloud config set project kade-prod

List the VPC networks

  • What it shows: Every VPC network in the project, with its mode (auto or custom).
  • What to look for: A default network that nobody meant to keep, and empty IPV4_RANGE columns: a VPC has no range of its own.
gcloud compute networks list

List the subnets of kade-vpc

  • What it shows: Each subnet with its region and IP range.
  • What to look for: Ranges that fit the address plan in section 06, and no overlaps.
gcloud compute networks subnets list --network=kade-vpc

List firewall rules with sources and targets

  • What it shows: Every firewall rule with where traffic may come from, which ports it opens, and which VMs it applies to.
  • What to look for: 0.0.0.0/0 next to admin ports like 22 or 3389, and rules with no target tags, which apply to every VM in the network.
gcloud compute firewall-rules list \  --format="table(name,network,sourceRanges.list(),    allowed[].map().firewall_rule().list(),targetTags.list())"

List the VMs

  • What it shows: Every VM with its zone, internal IP and external IP.
  • What to look for: Any external IP that should not be there. In Kadé's project today there are none.
gcloud compute instances list

List reserved static IPs

  • What it shows: IP addresses reserved in the project, internal and external.
  • What to look for: The load balancer IP 34.120.88.10; from chapter 7, the NAT IP 34.87.200.7.
gcloud compute addresses list

List DNS records in the public zone

  • What it shows: The records Cloud DNS serves for kade.lk.
  • What to look for: api.kade.lk pointing at 34.120.88.10.
gcloud dns record-sets list --zone=kade-lk

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