Chapters

Private access to Google services

Kadé's VMs have no external IPs, which is the strongest position a VM can be in. But Google's own services, from Cloud Storage to Cloud SQL, are not inside kade-vpc either. This chapter covers the three ways GCP gives private VMs a private path to them, and moves Kadé's database to Cloud SQL without ever giving it a public address.

Leaving and reaching privately · chapter 12 of 26 · 10 min read

The tension, and three private paths

Builds on2. Private and public addresses

Google's own services do not live inside your VPC. Cloud Storage, Secret Manager and Logging run on Google's servers, and Cloud SQL runs in a network Google manages. A VM normally reaches them through public addresses, just as it would reach any website:

  • With an external IP, a call to storage.googleapis.com works, because that name resolves to a public Google address and the VM can reach public addresses.
  • Without one (chapter 6.2), that path is gone. Removing external IPs makes the VM safer, but also cuts it off from the services it needs.

GCP gives back a private path in three different ways. People mix them up constantly, because their names are so alike. The drawing shows all three; the sections after it take one each:

Google's networkkade-vpc (yours) · no external IPskade-workersn-app · 10.10.1.20Cloud Run kade-apiegress via sn-runkade-psa-range10.10.32.0/20given to GooglePSC endpointan IP you chooseGoogle APIs · run by GoogleCloud Storage, Secret Manager,Logging…service network · run by GoogleCloud SQL kade-sqlprivate IP 10.10.32.3another company's serviceits own VPC, published with PSC1 · PGA2 · PSA3 · PSC

Three different shapes, all inside Google's network: a setting that lets traffic reach Google's APIs, a peering that joins Google's service network to yours, and an endpoint address that forwards to a service.

#MechanismReachesWhat it isKadé's use
1Private Google Access (PGA)Google APIs: Cloud Storage, Secret Manager, Logging…A setting on a subnetkade-worker's backups to Cloud Storage
2Private services access (PSA)Managed services that run in Google's own network: Cloud SQL, MemorystoreA VPC peering to Google's network, using an IP range you hand overCloud SQL kade-sql at 10.10.32.3
3Private Service Connect (PSC)Google APIs, Cloud SQL, or a service another company publishesAn endpoint: one internal IP in your VPC that forwards to the serviceNot needed yet; 10.10.200.0/24 is kept for it

Private Google Access

PGA is not something you create or run. It is a setting on a subnet (privateIpGoogleAccess, chapter 2). When it is on, a VM in that subnet with no external IP may send traffic to Google APIs. A VM with an external IP ignores the setting, because it already has a way out.

Where does that traffic go? The VM still sends it to Google's normal public addresses, through the route named default-internet-gateway. The name is misleading: for addresses that belong to Google's APIs, the "gateway" hands the packet straight to Google's front ends, and it never leaves Google's network. What PGA changes is permission: without it, Google drops traffic from a private address on the way to those public ones. With it, Google accepts it.

What decides the path is two things you already know: DNS (chapter 8), which picks the address, and routes (chapter 4), which pick the next hop. There are three ways to set those up. Each tab follows one call from kade-worker; the "what if" buttons break one piece at a time.

What if
kade-workersn-appno external IPPGA: onDNSstorage.googleapis.com → a publicGoogle addressroute0.0.0.0/0 →default-internet-gatewayinternet gatewayno external IP,PGA on:allowedCloud Storagekept on Google'snetwork1 resolve2 pick a route3 send4 deliver

Works: Cloud Storage reached with no external IP

DNS is untouched: storage.googleapis.com resolves to a normal Google address. The default route sends it towards the internet gateway, and because the subnet has PGA on, Google accepts it from a VM with no external IP and keeps it on its own network.

Show as config
sn-app: privateIpGoogleAccess = true
kade-vpc route: 0.0.0.0/0 → default-internet-gateway (priority 1000)
DNS: unchanged

kade-worker → storage.googleapis.com → Google address → default route → Google network → Cloud Storage
#Default domainsprivate.googleapis.comrestricted.googleapis.com
1AddressesGoogle's normal public addresses199.36.153.8/30199.36.153.4/30
2DNS change neededNoneYes: private zone or response policy (chapter 8)Yes, same
3Route neededThe default route to the internet gatewayThe default route, or a specific route for 199.36.153.8/30The default route, or a specific route for 199.36.153.4/30
4APIs reachableAllMost Google APIsOnly APIs that support VPC Service Controls
5Good forMost setups. Kadé's choiceNetworks with no default routeProjects inside a VPC Service Controls perimeter (chapter 17)

Private services access

Cloud SQL, Memorystore and similar services do not run in your project. Google runs them on VMs in its own projects, inside a VPC that Google manages: the service network. You never see it, log in to it or pay for it separately. Your VPC and that network are separate worlds with no route between them (chapter 2 section 01). A public IP bridges the gap by putting the database on the internet, which is exactly what Kadé wants to avoid.

Private services access joins the two networks with VPC peering, the same feature you would use to join two of your own VPCs. Because the networks are joined, the database needs an address that clashes with nothing on your side, so you set aside a range and Google takes the database's IP from it. Two steps, done once per VPC:

#StepWhat it createsKadé's value
1Allocate a rangeAn internal address range with purpose VPC_PEERING. Google hands managed services their IPs from itkade-psa-range = 10.10.32.0/20 (chapter 0 plan)
2Create the private connectionA VPC peering named servicenetworking-googleapis-com, made through the Service Networking APIBetween kade-vpc and Google's service network
After that, a Cloud SQL instance created with --network=kade-vpc still runs in Google's service network. It takes its IP from the range, and kade-vpc reaches it over the peering. No public IP, no authorized networks list.

Because it is real VPC peering, it follows peering's rules. The tabs show who can and cannot reach kade-sql:

outside GoogleKadé office172.16.0.0/16HA VPN (ch. 15)Google's networkkade-staging VPC10.20.0.0/16peered to kade-vpckade-vpc (yours)kade-worker10.10.1.20Cloud Run kade-apivia sn-runkade-psa-range10.10.32.0/20shares subnetroutes onlyservice networkrun by Googlekade-sql10.10.32.3:5432← peering

kade-worker and Cloud Run reach 10.10.32.3 over the peering

kade-vpc and Google's service network are joined by one VPC peering. Everything in kade-vpc that reaches 10.10.32.0/20 crosses it: kade-worker in sn-app and Cloud Run through sn-run. Nothing goes near the internet, and there is no authorized networks list to maintain.

Show as config
kade-psa-range: 10.10.32.0/20, purpose VPC_PEERING
peering: kade-vpc ⇄ servicenetworking-googleapis-com
kade-sql: private IP 10.10.32.3

kade-worker (10.10.1.20) → 10.10.32.3:5432   ✓
kade-api via sn-run     → 10.10.32.3:5432   ✓
#Peering ruleConsequence
1Not transitiveA VPC peered to kade-vpc (like kade-staging in chapter 14) cannot reach kade-sql through kade-vpc. No firewall rule fixes it; the routes simply do not pass along
2No overlapping rangesThe allocated range must not overlap any subnet or any other network you connect. This is why it is in the chapter 0 plan
3Size it onceGoogle asks for at least a /24; services in several regions take more. Adding a second range later is possible but needs the connection updated. Kadé's /20 leaves room
4Custom routes are not shared by defaultGoogle's network only learns your subnet routes. Routes to the office (VPN) must be exported on the peering explicitly (Tab C)
kade-vpckade-api-110.10.1.10runs the Cloud SQL connectorGoogle's networkCloud SQL Admin APIsqladmin.googleapis.comcreate, resize, certificatesservice networkkade-sql10.10.32.3 · your dataHTTPS + IAM tokenPrivate Google AccessPostgres on :5432private services access
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

Wide by design - scroll it sideways.

Private Service Connect

PSC turns the model around. Instead of flagging a subnet or peering two networks, you create an endpoint: one internal IP inside your own VPC that forwards to a service somewhere else. Under the hood it is a forwarding rule, the same kind of object as a load balancer's front (chapter 12.1), but with a private IP.

Two roles appear in every PSC setup:

  • The consumer is you. You create the endpoint, pick its IP, and your VMs send traffic to it like any other internal address.
  • The producer runs the service. It publishes the service through a service attachment, and Google carries traffic from your endpoint to it. For Google APIs and Cloud SQL, Google is the producer. For a vendor's service, the vendor is.

Nothing is peered, so there is no transitivity problem, and each endpoint is one IP with its own firewall rules and logs.

Google's networkkade-vpc (yours)kade-worker10.10.1.20DNS: storage-kadeapis.p.googleapis.com→ 10.10.200.2endpoint kadeapis10.10.200.2global, outside all subnetsbundle: all-apisGoogle APIs · run by GoogleCloud Storage, SecretManager…reached on your own address

Your own private address for Google APIs, with its own firewall rules and logs

The endpoint kadeapis is a global internal IP that you pick, outside every subnet (10.10.200.2, from the reserve in chapter 0). It forwards to Google APIs. GCP also creates DNS names like storage-kadeapis.p.googleapis.com, or you point *.googleapis.com at it with a private zone. Because it is your own address, you can write firewall rules and read logs for exactly this traffic.

Show as config
gcloud compute addresses create kadeapis-ip --global --purpose=PRIVATE_SERVICE_CONNECT \
  --addresses=10.10.200.2 --network=kade-vpc
gcloud compute forwarding-rules create kadeapis --global --network=kade-vpc \
  --address=kadeapis-ip --target-google-apis-bundle=all-apis
#TargetEndpoint addressWhat you get
1Google APIsA global internal IP outside your subnets, e.g. 10.10.200.2Your own address for Google APIs instead of PGA's shared ranges, with its own DNS names
2A published serviceAn internal IP from a subnet, e.g. 10.10.1.50GCP's version of AWS PrivateLink: a vendor publishes a service, you consume it on an internal IP, and neither side is exposed to the internet
3Cloud SQLAn internal IP from a subnet, in each VPC that needs itCloud SQL without the peering model, so several VPCs can each have their own endpoint

Choosing between them

#PGAPSAPSC
1What you createNothing: a subnet settingA range and one peering per VPCOne endpoint per service per VPC
2Address the VM usesGoogle's addressesAn IP from your allocated rangeAn IP you choose in your VPC
3Other peered VPCsUse their own PGAcannot reach iteach gets its own endpoint
4Office over VPNNeeds extra DNS and route setupNeeds route export and advertisingReaches the endpoint IP like any VPC address
5Separate firewall and logs per serviceNoPer rangeYes, per endpoint
6CostFreeNo charge for the connectionEndpoints and data processed are billed; check pricing
#You want to…Use
1Let a VM without an external IP call Google APIsPGA
2Give Cloud SQL or Memorystore a private IP in one VPCPSA
3Reach Google APIs on your own controllable addressPSC (Google APIs)
4Use another company's service privatelyPSC (published service)
5Reach one Cloud SQL instance from many VPCsPSC (Cloud SQL)
The short version: PGA is a flag for Google APIs, PSA is a peering for managed services, PSC is an endpoint for anything.

Cloud SQL with private IP, in practice

A private IP removes the database from the internet. A few more settings decide whether it is actually locked down:

#SettingKadé's valueWhy
1Public IPoffWith it on, the instance is reachable from the internet by anything on the authorized networks list
2Authorized networksemptyOnly applies to the public IP. Private IP connections ignore it
3SSL modeENCRYPTED_ONLYA private IP is not encryption. Without this, the database accepts unencrypted connections, and passwords cross the network in clear text
4How apps connectCloud SQL connector or Auth ProxyHandles TLS and can use IAM database login, so the app holds no long-lived password
5Firewallno new ingress rulekade-sql lives in Google's network, so kade-vpc's ingress rules never see it. Your egress rules still apply to traffic towards 10.10.32.0/20

The move from kade-db to kade-sql

Each step can be checked before the next one starts, and nothing shopper-facing changes until step 6:

Before the day

  1. 1.Range + connectionAllocate kade-psa-range, create the private connection (command 7)check before moving oncommands 2-3 show the range
  2. 2.Create kade-sqlPrivate IP only, ENCRYPTED_ONLY, regional HA (command 8)check before moving oncommand 4: no public IP
  3. 3.Copy the dataDatabase Migration Service, or dump and restore in a quiet hourcheck before moving onrow counts match on both
  4. 4.Lower the TTLdb.kade.internal TTL 300 → 30, a day before (chapter 8)check before moving ondig shows TTL 30

Cutover

  1. 5.Short write freezeStop writes to kade-db, final sync to kade-sqlcheck before moving onno new rows on kade-db
  2. 6.Flip the namedb.kade.internal → 10.10.32.3, recycle app connection poolscheck before moving onnew connections reach kade-sql

After

  1. 7.WatchErrors, latency, Cloud SQL connections; kade-db kept stopped, not deletedcheck before moving ona few days with no issues
  2. 8.Clean upDelete kade-db, kade-db-ip, kade-api-to-db, kade-run-to-db; TTL back to 300check before moving onfirewall list has no db rules

Steps 1 to 4 change nothing for shoppers. The only moment of risk is steps 5 and 6, and the way back is to point the name at 10.10.2.5 again: kade-db is stopped, not deleted, until step 8.

Back to Kadé

Commands in this chapter

Commands 1 to 5 are read-only. Commands 6 to 9 change the project.

Check Private Google Access on each subnet

  • What it shows: Whether PGA is on for every subnet in kade-vpc.
  • What to look for: True on every subnet with VMs that have no external IP.
gcloud compute networks subnets list --network=kade-vpc \  --format="table(name,region.basename(),ipCidrRange,privateIpGoogleAccess)"

List the private services access connection

  • What it shows: The peering to Google's service network and the ranges it uses.
  • What to look for: servicenetworking-googleapis-com with kade-psa-range in reservedPeeringRanges.
gcloud services vpc-peerings list --network=kade-vpc

List ranges allocated for peering

  • What it shows: Internal ranges with purpose VPC_PEERING, and their size.
  • What to look for: The same ranges as command 2. If they disagree, sort that out before creating anything.
gcloud compute addresses list --global --filter="purpose=VPC_PEERING" \  --format="table(name,address,prefixLength,network.basename())"

See how a Cloud SQL instance is connected

  • What it shows: Public IP on or off, the private network, authorized networks and SSL mode.
  • What to look for: ipv4Enabled: false, privateNetwork set to kade-vpc, no authorized networks, sslMode: ENCRYPTED_ONLY.
gcloud sql instances describe kade-sql --format="yaml(ipAddresses,settings.ipConfiguration)"

Check the peering's route export settings

  • What it shows: Whether kade-vpc shares its custom routes (such as VPN routes) with Google's service network.
  • What to look for: exportCustomRoutes: true once the office needs to reach kade-sql (chapter 15).
gcloud compute networks peerings list --network=kade-vpc

Turn on Private Google Access for a subnet (changes the project)

  • What it shows: The setting for a subnet that Cloud NAT does not cover.
  • What to look for: True in command 1.
gcloud compute networks subnets update sn-data \  --region=asia-southeast1 --enable-private-ip-google-access

Allocate the range and create the connection (changes the project)

  • What it shows: The two one-time steps of private services access.
  • What to look for: Commands 2 and 3 both show kade-psa-range. Needs the Service Networking API enabled.
gcloud compute addresses create kade-psa-range --global \  --purpose=VPC_PEERING --addresses=10.10.32.0 --prefix-length=20 --network=kade-vpcgcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \  --ranges=kade-psa-range --network=kade-vpc

Create Cloud SQL with a private IP only (changes the project)

  • What it shows: kade-sql on kade-vpc, from kade-psa-range, with no public IP and encrypted connections only.
  • What to look for: Command 4 shows the private address and no public one. Pick a machine size that suits your load.
gcloud sql instances create kade-sql --database-version=POSTGRES_16 \  --region=asia-southeast1 --availability-type=REGIONAL --tier=db-custom-2-7680 \  --network=kade-vpc --allocated-ip-range-name=kade-psa-range \  --no-assign-ip --ssl-mode=ENCRYPTED_ONLY

Share VPN routes with Google's service network (changes the project)

  • What it shows: Half of what the office needs to reach kade-sql (psaex tab C). The other half is the Cloud Router advertising 10.10.32.0/20 to the office (chapter 15).
  • What to look for: exportCustomRoutes: true in command 5.
gcloud compute networks peerings update servicenetworking-googleapis-com \  --network=kade-vpc --export-custom-routes

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

See it broken

Harbour is a platform built wrong on purpose. These defects are this chapter's ideas, left undone: what each one breaks, how you would find it, and the change that closes it.

  1. D4
    Private Google Access is off on the data subnet

    mediumcloses in phase 1, Groundwork

    Goes with Private Google Access

  2. D5
    Cloud SQL is on a public IP

    criticalcloses in phase 4, Private data path

    Goes with Private services access and Cloud SQL with private IP, in practice