Chapters

A second VPC, briefly

Most projects live happily in one VPC. When a second one appears, usually for staging, there are only a few ideas you must know so that nothing earlier in this course surprises you. This chapter covers exactly those, and points at the rest.

Growing out · chapter 20 of 26 · 8 min read

First question: connect them at all?

Builds on6. Two networks

Two VPCs are two separate worlds (chapter 2 section 01). The honest default for staging is to leave it that way: a separate project with its own VPC, its own small database, and its own Cloud NAT, connected to nothing. It cannot break production, and production data cannot leak into it.

project kade-prodkade-vpc · plan 10.10.0.0/16Cloud Run kade-apivia sn-runCloud SQL kade-sql10.10.32.3Cloud NAT · 34.87.200.7on PayGate's live allow-listproject kade-stagingstaging-vpc · plan 10.20.0.0/16Cloud Run kade-apistaging buildCloud SQL (small)test data onlyCloud NAT · its own IPon PayGate's sandbox allow-list✕nolink
  • Your VPC
  • Allowed traffic and replies

Separate and unconnected: the simplest and safest shape. The only shared things are the folder's hierarchical firewall policy and organization policies (chapters 5.2 and 17), which apply to both projects automatically.

Wide by design - scroll it sideways.

#Connect them when…Keep them apart when…
1Something in one VPC must call a private IP in the other (a shared internal service, a shared database)Each environment has its own copy of everything it needs. Kadé's case
2One team must run the network for many projects (central firewall, central IP plan)There is one person running the network anyway
3Several projects must share one VPN or Interconnect to the officeStaging does not need the office network
Even when kept apart, give the second VPC a range that does not overlap the first (chapter 0 already reserves 10.20.0.0/16). Then connecting them later stays possible.

Three ways to connect

If two networks do need to talk, GCP gives three shapes. They solve different problems:

1 · Not connectedthe default for staging2 · VPC peeringtwo VPCs route to each other3 · Shared VPCone VPC, many projects use it4 · Private Service Connectone endpoint, one servicekade-vpc 10.10/16staging-vpc 10.20/16✕nothing shared, nothing to breakworkloadsworkloadskade-vpc 10.10/16staging-vpc 10.20/16peerworkloadsworkloadssubnet routes exchanged · ranges must not overlaphost project kade-net · one VPCsn-prodsn-stagingproject prodproject stagingstaging-vpckade-vpcendpoint 10.20.2.50one service onlynothing else reachable · overlap allowed
#VPC peeringShared VPCPrivate Service Connect
1The shapeTwo separate VPCs, joined so they can route to each otherOne VPC, owned by a host project, used by several service projectsOne endpoint IP in your VPC that reaches one service in another
2What is connectedEvery subnet on both sidesNothing needs connecting: it is a single networkOnly that one service
3Who runs the networkEach side runs its own (firewall, routes)The host project's network team, for everyoneEach side runs its own
4Ranges may overlap?noNot applicable: one planyes
5Needs an organization?No; works across projects and organizationsYesNo
6Typical useTwo teams' VPCs that need some private trafficA company where one platform team owns networkingReaching one shared service, or a vendor's service (chapter 9)

VPC peering, and its two rules

Peering joins two VPCs at the routing level. Each side must create its half of the peering; when both halves exist, each VPC learns the other's subnet routes (chapter 4) and traffic flows on internal IPs, over Google's network, with no gateway or VPN. A few things are not shared, and they surprise people:

#Not shared by peeringSo you must…Chapter
1Firewall rulesAllow the other VPC's range in each VPC's own rules5.1
2Custom routes (static, dynamic from a VPN)Turn on export on one side and import on the other4, 9
3DNS: private zones and VM namesAdd a peering zone (DNS peering)8
4Anything the other VPC itself reaches through a peeringNothing helps: rule 2 below9

Two rules decide whether peering works at all. Each tab shows one:

kade-vpc (project kade-prod)sn-app 10.10.1.0/24 · kade-worker 10.10.1.20sn-run 10.10.3.0/24staging-vpc (project kade-staging)sn-app 10.20.1.0/24 · test VM 10.20.1.10sn-run 10.20.3.0/24peering (both halves)routes exchanged

What each side learns, and what it does not

  • subnet routes10.20.1.0/24, 10.20.3.0/24 appear in kade-vpc (and the reverse)
  • firewall ruleseach VPC still needs a rule allowing the other's range
  • DNSprivate zones and VM names stay local unless a peering zone is added
  • custom routesonly with export on one side and import on the other

Both projects create their half of the peering. Once both exist, each VPC learns the other's subnet routes, so 10.10.1.20 can reach 10.20.1.10 directly over Google's network. Each side still needs a firewall rule allowing the other's range, and DNS names are not shared unless you add a peering zone.

Show as config
kade-vpc:     peering prod-to-staging  → kade-staging/staging-vpc
staging-vpc:  peering staging-to-prod  → kade-prod/kade-vpc
both halves exist → ACTIVE → subnet routes exchanged

Shared VPC

Shared VPC turns the question around: instead of joining two networks, many projects use one network. A host project owns the VPC, its subnets, routes and firewall rules. Service projects attach to it and create their VMs and Cloud Run services directly in its subnets. Each team keeps its own project for billing and permissions; the network is run in one place.

HOST project kade-net · owns the VPC, subnets, routes and firewall rulesshared-vpcsn-prod-app10.10.1.0/24resources fromproject prodsn-prod-run10.10.3.0/24resources fromproject prodsn-staging-app10.20.1.0/24resources fromproject stagingsn-staging-run10.20.3.0/24resources fromproject stagingSERVICE project kade-prodnetworkUser on sn-prod-* onlyits own billing and IAMSERVICE project kade-stagingnetworkUser on sn-staging-* onlyits own billing and IAMService projects put their VMs and Cloud Run services into the subnets they may use: one network, one plan, one rule set.

An example layout, not Kadé's: Kadé keeps two separate projects (section 07).

#PieceWhoWhat it allows
1Shared VPC Admin (roles/compute.xpnAdmin)Granted on the organization or folderMakes a project a host, and attaches service projects
2Network User (roles/compute.networkUser)Granted on the host project, or on single subnetsLets a service project's people create resources in those subnets, and nowhere else
3Network Admin, Security AdminThe platform team, in the host projectChange subnets, routes, firewall rules for everyone
A service project can attach to only one host project. Subnet-level Network User grants are how "staging may only use the staging subnet" is enforced.

Private Service Connect, the narrow option

When the only thing one network needs from another is a single service, there is no reason to join the whole networks. A Private Service Connect endpoint (chapter 9 section 04) gives the consumer one internal IP that forwards to that service. Nothing else is reachable, the ranges may overlap, and there is no transitivity problem. Chapter 9's tab "Cloud SQL from many VPCs" is exactly this pattern for a shared database.

What to learn next, if you need the full picture

This chapter is enough to read the rest of the course and to answer exam questions. To design, build and run a real multi-VPC network, these are the additional topics to learn, roughly in order:

#TopicWhy you would need it
1IP address planning across many networksEvery network you may ever connect must have its own non-overlapping block, decided up front (chapter 0's plan, at company scale)
2Shared VPC design and IAMHost and service projects, subnet-level permissions, who owns firewall rules, how Cloud Run and GKE attach to shared subnets
3VPC peering detailsCustom route export and import, peering limits and quotas, how firewall rules and DNS work across a peering
4Network Connectivity Center (NCC)Google's hub-and-spoke service. With VPC spokes it connects many VPCs transitively, which plain peering cannot
5Cross-project DNSDNS peering zones and cross-project zone binding, so names resolve everywhere they should (chapter 8)
6Private NATConnecting networks whose ranges already overlap and cannot be renumbered (chapter 7 section 08)
7Hybrid connectivity at scaleSharing one VPN or Interconnect across many VPCs (chapter 15 is the single-VPC version)
8Governance across projectsHierarchical firewall policies, organization policies that restrict which subnets and projects can be used, VPC Service Controls (chapters 5.2 and 17)
Google's "Best practices and reference architectures for VPC design" document is the natural next read: it walks through these patterns with diagrams.

Back to Kadé

Commands in this chapter

Commands 1 to 4 are read-only. Command 5 changes the project and is shown for reference; Kadé does not run it.

List a network's peerings

  • What it shows: Every peering on kade-vpc, its state, and its route export and import settings.
  • What to look for: servicenetworking-googleapis-com (chapter 9) is a peering too. INACTIVE means the other side has not created its half.
gcloud compute networks peerings list --network=kade-vpc

See the routes a peering shares

  • What it shows: Routes kade-vpc receives from, or sends to, one peering.
  • What to look for: The other side's subnet ranges in the incoming list. A missing range usually means an overlap or an export setting.
gcloud compute networks peerings list-routes servicenetworking-googleapis-com \  --network=kade-vpc --region=asia-southeast1 --direction=INCOMING

Is this project in a Shared VPC?

  • What it shows: The host project this project is attached to, if any.
  • What to look for: Empty output for Kadé: kade-prod is a standalone project.
gcloud compute shared-vpc get-host-project kade-prod

List service projects of a host

  • What it shows: In a Shared VPC, every project attached to a host project.
  • What to look for: Only the projects you expect. Replace the host project ID with your own.
gcloud compute shared-vpc associated-projects list HOST_PROJECT_ID

Create one half of a peering (changes the project; reference only)

  • What it shows: How a peering starts. The other project runs the same command pointing back.
  • What to look for: State INACTIVE until both halves exist, then ACTIVE. It fails immediately if any subnet ranges overlap.
gcloud compute networks peerings create prod-to-staging \  --network=kade-vpc --peer-project=kade-staging --peer-network=staging-vpc

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. D11