Chapters

DNS inside GCP

The fundamentals course covered how DNS works on the internet: resolvers, authoritative servers, records and TTLs. This chapter covers what GCP adds: names every VM gets for free, zones that only your VPC can see, and the order in which a VM's DNS questions get answered. That order explains most GCP DNS surprises.

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

The one idea: the answer depends on who asks

Builds on22. DNS23. Records and resolution

DNS turns names into addresses. Everything else in GCP DNS is detail about who answers, and whether the answer is public or private. The same name can give different answers depending on where the question comes from:

#Asked byNameAnswer
1A shopper's phoneapi.kade.lk34.120.88.10, the load balancer
2A shopper's phonedb.kade.internalDoes not exist
3kade-worker, inside kade-vpcdb.kade.internal10.10.2.5

Every question a VM asks goes to the same place: the metadata server at 169.254.169.254, which DHCP set as its DNS server (chapter 1.2 section 06). What the metadata server checks, and in what order, is section 04.

kade-workerDNS server set by DHCPmetadata server169.254.169.254knows all threeevery questionprivate zonesdb.kade.internalinternal DNSkade-db…c.kade-prod.internalpublic DNSapi.kade.lka VM you editedresolv.conf → 8.8.8.88.8.8.8a public resolverprivate zones, internal DNS: no answer
  • Run by Google
  • Your VPC

Wide by design - scroll it sideways.

Internal DNS: names you get for free

Every VM gets a DNS name automatically, with no setup. It is built from the VM's name, zone and project:

kade-dbVM nameasia-southeast1-bzone.calways c.kade-prodproject ID.internalalways internal.→ 10.10.2.5
  • Set by the VM and its project

Wide by design - scroll it sideways.

kade-db.asia-southeast1-b.c.kade-prod.internal     → 10.10.2.5
kade-api-1.asia-southeast1-a.c.kade-prod.internal  → 10.10.1.10

Other VMs in the same project and network resolve these names to internal IPs. The metadata server also adds a search domain, so from inside kade-vpc the short name kade-db usually works too. Internal load balancers get names the same way.

Why these names are not enough for Kadé:

#LimitConsequence
1Tied to the VM's name and zoneA new database VM, or one in another zone, has a different name. Everything that used the old name breaks
2Only for VMs and internal load balancersCloud SQL, or anything outside Compute Engine, has no internal DNS name here
3Only inside the project's networkOther VPCs or the office need peering or forwarding to use them (section 08)
For a stable, friendly name that you control, use a private zone.

Cloud DNS zones: public and private

Cloud DNS is GCP's managed authoritative DNS. "Authoritative" means it is the source of truth for a domain: it gives the real answer, not a cached copy (Fundamentals ch 22). A zone is the set of records for one domain. The most important thing about a zone is who may ask it questions:

Shopper's phoneanywhere on the internetpublic zone kade-lkkade.lk · anyone can askapi.kade.lk A 34.120.88.10api.kade.lk?kade-workerinside kade-vpcprivate zone kade-internalkade.internal · kade-vpc onlydb.kade.internal A 10.10.2.5db.kade.internal?✕db.kade.internal?From the internet, db.kade.internaldoes not exist at all: the privatezone only answers kade-vpc.Anyone may ask a public zone.It is what the world sees foryour domain.
  • Run by Google
  • Your VPC

Wide by design - scroll it sideways.

#Public zonePrivate zone
1Who can askThe whole internetOnly the VPC networks you authorize
2Used forYour public domain, when GCP is your DNS providerStable, friendly names for internal services
3Needs a registrar changeYes: the domain's name servers must point at Cloud DNSNo: nobody outside needs to know it exists
4Kadé'skade-lk for kade.lk (Fundamentals ch 22)kade-internal for kade.internal (new)

The order the metadata server follows

When a VM asks the metadata server a question, the metadata server checks five places in a fixed order, and the first one that has an answer gives it. Each tab follows one name down the list.

Questionkade-worker asks 169.254.169.254: db.kade.internalmetadata server169.254.169.254checks the five stepsfrom the top; the firstone with an answer wins1 · Outbound server policyalternative name serversnone set↓ next2 · Response policiesoverride chosen namesnone set↓ next3 · Private zonesprivate, forwarding, peering · longest suffixkade-internal matchesANSWERS4 · Internal DNS*.c.kade-prod.internalnot reached5 · Public DNSthe internetnot reached

Answer: 10.10.2.5 · from private zone kade-internal

Steps 1 and 2 have nothing set, so the question reaches the private zones. kade-internal matches the suffix kade.internal and answers. Internal DNS and public DNS are never asked.

Show as config
kade-worker → 169.254.169.254: db.kade.internal?
1 outbound server policy: none
2 response policies:       none
3 private zones:           kade-internal (kade.internal.) matches → A 10.10.2.5

Split-horizon: the same name, two answers

Run a public zone and a private zone for the same domain, and a name resolves differently depending on where the question comes from:

Name: api.kade.lk

From the internet      → public zone  kade-lk        → 34.120.88.10 (load balancer)
From inside kade-vpc   → private zone kade-lk-inside → a private IP, straight to the backend

Internal services use the same names the outside world uses, but their traffic goes directly inside the VPC instead of out to the internet and back in through the load balancer. It is a common pattern, and it has one trap:

Each tab is a different set of private zones. The left side shows every private zone kade-vpc can see and whether its suffix matches the name; the right side is public DNS. Use the "what if" buttons to change which name kade-worker asks for.

What if
Questionkade-worker asks: api.kade.lkSTEP 3 · PRIVATE ZONES kade-vpc CAN SEE (longest matching suffix wins)STEP 5 · PUBLIC DNSzone kade.internalsuffix does not matchdb.kade.internal → 10.10.2.5zone kade.lksuffix matches → this zone answersonly the one record needed insideapi.kade.lk → 10.10.1.10public zone kade.lknever asked: step 3 already answeredapi.kade.lk → 34.120.88.10status.kade.lk → 34.120.88.10www.kade.lk → 34.120.88.10

Answer: 10.10.1.10 · from private zone kade.lk

api.kade.lk matches the private zone kade.lk, which has the record, so kade-worker gets the private address. That part works as planned.

Show as config
Public zone  kade.lk:  api, www, status, shop … (20 records)
Private zone kade.lk:  api → 10.10.1.10          (only the one record needed inside)

kade-worker asks for api.kade.lk    → private zone → 10.10.1.10     ✔ as planned
kade-worker asks for status.kade.lk → private zone → NXDOMAIN       ✘
                                       (the private zone matched the suffix, so
                                        public DNS is never asked)

Try it: which answer do I get?

Choose who asks and which name. Switch the optional zones on to see how the answer changes. The office options only make sense once chapter 15's VPN exists.

Who asks
Name

Answer: 10.10.2.5, from the private zone kade-internal.

  1. kade-worker asks the metadata server, 169.254.169.254.
  2. Step 1: no outbound server policy. Step 2: no response policies.
  3. Step 3: the private zone kade-internal matches the suffix kade.internal, so it answers. Public DNS will not be asked.

Changing an answer safely: TTL

Builds on23. TTL and caching

Resolvers and apps cache answers for the record's TTL. Changing a record does not change what clients already cached; they only see the new address when their copy expires. The chart follows chapter 9's database cutover: db.kade.internal changes from 10.10.2.5 to 10.10.32.3 at time 0. Each row is something that looked the name up at a different moment.

What if
TTL 300 s · record changes at 0 s · each box = one cached answer, kept for one TTLCloud DNS record10.10.2.510.10.32.3-60 s0 s+60 s+120 s+180 s+240 s+300 s↑ record changedkade-api instance 1looked up at -250 sold 10.10.2.5new 10.10.32.3kade-api instance 2looked up at -20 sold 10.10.2.5kade-workerlooked up at +5 snew 10.10.32.3connection poolopened at -600 sold connectionrecycled at +60 s: reconnects to 10.10.32.3

Longest time a lookup still returns the old address: about 280 s

Each cached answer lives for 300 s from when it was looked up. Instance 2 looked up just before the change, so it keeps using the old address for 280 s. Up to five minutes of mixed traffic: some requests go to the old database, some to the new.

Show as config
db.kade.internal  A  10.10.2.5   TTL 300
(cutover at 0 s) → db.kade.internal  A  10.10.32.3

Worst case: an answer cached just before 0 s stays in use for 300 s.
#WhenStepWhy
1A day beforeLower db.kade.internal's TTL from 300 s to 30 sSo the old, long TTL has expired everywhere by cutover time
2CutoverChange the A record to 10.10.32.3, then recycle connection poolsNew lookups get Cloud SQL within 30 seconds; pools reconnect
3A day afterRaise the TTL back to 300 sFewer lookups, and the setting is back to normal

Connecting GCP DNS to other DNS systems

The remaining zone and policy types all exist for one reason: joining GCP's DNS to other DNS systems, such as the office, another VPC or another cloud. If all your DNS lives in one place, you never touch them.

#TypeWhat it doesKadé's useChapter
1Forwarding zone"For this domain, do not answer here; ask that DNS server"VMs resolve the office's own names over the VPN15
2Peering zoneLets one VPC use another VPC's private zoneskade-staging uses names from kade-vpc14
3Inbound server policyGives the VPC DNS entry points (one internal IP per subnet) that outside networks can queryOffice PCs resolve db.kade.internal15
4Outbound server policySends all of the VPC's questions to your own DNS servers insteadNot used: it switches off most of section 04-
5Response policyOverrides answers for chosen names: a different address, or no answer at allPoint *.googleapis.com at private API addresses9

Each tab follows one question from the asker to the answer, numbered in order. Request arrows are solid; replies come back underneath.

What if
kade-workerasksnas.office.kade.lanmetadata server169.254.169.254forwarding zoneoffice.kade.lan→ 172.16.1.53private routingoffice firewallmust allow 53from35.199.192.0/19office DNS172.16.1.53nas →172.16.1.401 question2 step 3: zone matches3 over the VPNsrc 35.199.192.0/194 to the serveranswer 172.16.1.40 returns the same way

Answer: 172.16.1.40, from the office DNS server

The forwarding zone sends the question to the office DNS server over the VPN. It arrives from 35.199.192.0/19, Cloud DNS's special path (chapter 4), and the answer returns the same way.

Show as config
Forwarding zone: office.kade.lan → target 172.16.1.53 (office DNS server), private routing

kade-worker asks nas.office.kade.lan
  → metadata server → forwarding zone matches
  → query sent over the VPN to 172.16.1.53, from 35.199.192.0/19
  → 172.16.1.40

DNSSEC

Normal DNS answers are not authenticated, so an attacker in the lookup path could return a forged answer that points your domain at their server. DNSSEC adds signatures to records, so a resolver can check that an answer came from the real authoritative source and was not changed.

public zone kade-lkapi.kade.lk A 34.120.88.10+ signatureattacker in the pathapi.kade.lk A 203.0.113.66no valid signaturevalidating resolver1 signature fits the key2 key fits the DS record✕Shopper's phonegets 34.120.88.10forged answer rejected:an error, never theattacker's server.lk registryDS record for kade.lkadded at your registrar;without it, nothing is checked
  • Run by Google
  • Allowed traffic and replies

Wide by design - scroll it sideways.

#PointDetail
1WherePublic zones only. Private zones are answered inside Google's network
2HowTurn it on for the zone, then add the DS record Cloud DNS gives you at your domain registrar. Until the DS record is there, nothing is checked
3RiskMoving the domain to another DNS provider without turning DNSSEC off correctly can make the whole domain fail to resolve (chapter 13)
4Worth it?A good hardening step for a public domain. Many domains still do not use it

When your public DNS is not in GCP

Chapter 13 moves kade.lk to Cloudflare. After that, Cloudflare is authoritative for the public domain, with its own records and DNSSEC, and the public zone kade-lk in Cloud DNS is deleted. The private zone kade-internal stays in Cloud DNS: public and private DNS do not have to live in the same place.

outside GCPShopper's phoneanywhere on the internetCloudflareauthoritative for kade.lkapi.kade.lk A 34.120.88.101 api.kade.lk?GCP · kade-prodload balancer34.120.88.102 trafficpublic zone kade-lkdeleted in chapter 13kade-workerinside kade-vpcprivate zone kade-internalstays in Cloud DNSdb.kade.internal
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

Wide by design - scroll it sideways.

External DNS is what makes three things concrete, all covered in chapter 13:

#TopicWhy DNS is the reason
1Static load balancer IPCloudflare's records hold the address. If it changed, the name would point at nothing. Kadé's is static (chapter 3), so this is already safe
2Grey cloud vs orange cloudA per-record Cloudflare setting: return the GCP address directly (grey, DNS only), or send traffic through Cloudflare first (orange, proxied). It decides whether Cloudflare's WAF and CDN are in the path at all
3Origin lockOnly possible for names whose traffic really comes through Cloudflare, so you must know which records are orange first
DNS decides what reaches your infrastructure, and by which path. When it lives outside GCP, the join between the two systems is where these decisions are made.

Logs and cost

#FeatureWhat it gives
1Query logging (a server policy setting on the VPC)Every question VMs in kade-vpc ask, and the answer. Useful to see which names an app really looks up, or to spot a VM resolving strange domains (chapter 16)
2Public zone loggingQuestions from the internet about your public zone

Back to Kadé

Commands in this chapter

Commands 1 to 4 are read-only. Commands 5 to 7 change the project.

Resolve a name the way the app does

  • What it shows: The address a VM gets for a name, through /etc/resolv.conf and the metadata server. Run inside the VM.
  • What to look for: The expected address. No output means the name does not resolve from this VM.
getent hosts db.kade.internal

Ask the metadata server directly, with details

  • What it shows: The full DNS answer: status, TTL and the answering server.
  • What to look for: status: NOERROR with an answer, or NXDOMAIN (no such name). The TTL counts down on repeated queries while the answer is cached.
dig @169.254.169.254 db.kade.internal

List zones and who can see them

  • What it shows: Every Cloud DNS zone in the project, its domain and whether it is public or private.
  • What to look for: A private zone whose domain is also a public domain (the trap in section 05).
gcloud dns managed-zones list \  --format="table(name,dnsName,visibility,privateVisibilityConfig.networks[].networkUrl.basename())"

List records in a zone

  • What it shows: All records in the private zone, with type, TTL and value.
  • What to look for: db.kade.internal. pointing where you expect.
gcloud dns record-sets list --zone=kade-internal

Create the private zone (changes the project)

  • What it shows: A private zone for kade.internal., visible only to kade-vpc. Note the trailing dot.
  • What to look for: The zone in command 3 with visibility private.
gcloud dns managed-zones create kade-internal \  --dns-name=kade.internal. --visibility=private \  --networks=kade-vpc --description="Kadé internal names"

Add the database record (changes the project)

  • What it shows: db.kade.internal pointing at kade-db, with a 5-minute TTL.
  • What to look for: Command 1 from any VM in kade-vpc returns 10.10.2.5.
gcloud dns record-sets create db.kade.internal. \  --zone=kade-internal --type=A --ttl=300 --rrdatas=10.10.2.5

Point the name somewhere else (changes the project)

  • What it shows: Chapter 9's cutover: the same name, now pointing at Cloud SQL, with the low TTL from section 07.
  • What to look for: Command 2 shows the new address within one TTL. Then recycle the app's database connections.
gcloud dns record-sets update db.kade.internal. \  --zone=kade-internal --type=A --ttl=30 --rrdatas=10.10.32.3

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