CareerByteCode
ByteBlogby CareerByteCode

Cloud DNS: Host Your Domain on Google Cloud

What you build: Cloud DNS public and private zones, a caching resolver that serves an old answer until its TTL runs out, and the lower-TTL-first fix. Every step tested.

Cloud DNS: Host Your Domain on Google Cloud

✍️ Translate this blog into Hindi, Tamil or Telugu

DNS turns names into addresses, and almost every "the site is down" story has a DNS chapter. Cloud DNS is Google Cloud's managed DNS service: it can answer for your public domain on Google's name servers, and it can give your VPC private names that only your own machines see. In this lab you build both, change a record and watch a caching resolver keep the old answer until its TTL runs out, fix that with the "lower the TTL first" habit, and hit one of DNS's oldest rules.

Who it is for: cloud and DevOps beginners who want to understand DNS by watching it work. Time: about 45 minutes. Cost: two managed zones for about 10 minutes (a few cents), covered by the free trial credit.

Every command below was run for real by the CareerByteCode Team, and every output is the real result we got. We typed the commands in a Cloud Shell-like Linux VM inside the VPC (Debian with gcloud and dig); the private zone answers only inside the VPC, so use a small VM like ours for those steps.

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →

What we did not do: buy a domain. The public zone uses a made-up name, so we ask Google's name servers directly; the step at your domain registrar (pointing your domain to Cloud DNS) is described below but was not tested.

Part A: DNS in plain English

Cloud DNS zones
A private zone answers only inside the VPC; a public zone answers on Google's name servers; a caching resolver keeps answers for their TTL

Words you will use:

  • Zone: a set of records for a domain, managed together. A public zone answers anyone on the internet; a private zone answers only the VPC networks you attach.
  • Records: A (name to IPv4 address), CNAME (name to another name), TXT (free text, often used to prove you own a domain), NS (which servers answer for the zone), SOA (zone settings).
  • TTL (time to live): how many seconds others may keep (cache) an answer before asking again.
  • Resolver: the server your machine asks. On a Google Cloud VM it is the metadata server, 169.254.169.254. Your laptop asks your internet provider's resolver, which caches.
  • dig: the command-line tool for asking DNS questions.
  • Real-life example: DNS is the phone book. A caching resolver is a friend who memorised your number: if you change it, the friend keeps calling the old one until they forget it (the TTL).

Before you start

A Google Cloud project with billing linked (the free trial works) and a small VM in the default network with gcloud (we use the same lab VM as in our other labs).

Part B: the lab

Step 1: dig and the VM's resolver

# The tool for DNS questions is dig. Ask the VM's own resolver (Google's metadata server) for a public name first
sudo apt-get install -y -qq dnsutils > /dev/null 2>&1; dig -v 2>&1 | head -1
dig +noall +answer www.google.com A | head -2 | awk '{print $1, $3, $4, "(ttl " $2 " s)"}' | sed -E 's/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/<an address>/'
grep nameserver /etc/resolv.conf
DiG 9.18.49-1~deb12u3-Debian
www.google.com. IN A (ttl 300 s)
www.google.com. IN A (ttl 300 s)
nameserver 169.254.169.254
nameserver 169.254.169.254

Two lines for www.google.com (it has two addresses), a TTL of 300 seconds, and the VM asks the metadata server.

Step 2: a private zone with three records

Why: private names such as web.lab.internal for your own machines, visible only inside the VPC. We point web at the VM's own private IP, add a CNAME (www is another name for web) and a TXT record:

# A PRIVATE zone: names that only VMs in our VPC network can resolve (no domain to buy)
gcloud services enable dns.googleapis.com && echo "Cloud DNS API on"
gcloud dns managed-zones create lab-private --dns-name="lab.internal." --description="Batch lab private zone" --visibility=private --networks=default 2>&1 | tail -1
IP=$(gcloud compute instances describe $(hostname) --zone=us-east1-b --format="value(networkInterfaces[0].networkIP)")
gcloud dns record-sets create web.lab.internal. --zone=lab-private --type=A --ttl=60 --rrdatas=$IP > /dev/null
gcloud dns record-sets create www.lab.internal. --zone=lab-private --type=CNAME --ttl=300 --rrdatas=web.lab.internal. > /dev/null
gcloud dns record-sets create lab.internal. --zone=lab-private --type=TXT --ttl=300 --rrdatas='"owner=platform-team"' > /dev/null
gcloud dns record-sets list --zone=lab-private --format="table(name,type,ttl,rrdatas.list())"
Cloud DNS API on
Created [https://dns.googleapis.com/dns/v1/projects/your-project-id/managedZones/lab-private].
NAME               TYPE   TTL    RRDATAS
lab.internal.      NS     21600  ns-gcp-private.googledomains.com.
lab.internal.      SOA    21600  ns-gcp-private.googledomains.com. cloud-dns-hostmaster.google.com. 1 21600 3600 259200 300
lab.internal.      TXT    300    "owner=platform-team"
web.lab.internal.  A      60     10.142.0.23
www.lab.internal.  CNAME  300    web.lab.internal.

Step 3: resolve from inside the VPC

# Ask from inside the VPC: A, CNAME and TXT records answer from the private zone
dig +noall +answer web.lab.internal A
dig +noall +answer www.lab.internal A
dig +short lab.internal TXT
echo "from the public internet (Google Public DNS): $(dig +short @EXTERNAL_IP web.lab.internal A | wc -l) answers"
web.lab.internal.	60	IN	A	10.142.0.23
www.lab.internal.	300	IN	CNAME	web.lab.internal.
web.lab.internal.	60	IN	A	10.142.0.23
"owner=platform-team"
from the public internet (Google Public DNS): 0 answers

The CNAME is followed automatically to the A record. From Google Public DNS on the internet, the private zone gives 0 answers: private really means private.

Step 4: a public zone

Why: to host a real domain, you create a public zone; Google gives it four name servers. We used the made-up cbc-lab-demo.com with an A record pointing to 203.0.113.10, an address reserved for documentation.

# A PUBLIC zone for a domain name. We do not own this name, so nobody on the internet is sent here yet,
# but Google's name servers already answer for it when you ask them directly
gcloud dns managed-zones create lab-public --dns-name="cbc-lab-demo.com." --description="Batch lab public zone" --visibility=public 2>&1 | tail -1
gcloud dns record-sets create www.cbc-lab-demo.com. --zone=lab-public --type=A --ttl=300 --rrdatas=203.0.113.10 > /dev/null
gcloud dns managed-zones describe lab-public --format="value(nameServers)" | tr ';' '\n'
NS=$(gcloud dns managed-zones describe lab-public --format="value(nameServers[0])")
echo "ask Google's name server directly: $(dig +short @$NS www.cbc-lab-demo.com A)"
echo "ask the normal internet path:      $(dig +short www.cbc-lab-demo.com A | head -1 || true) (empty: the domain does not point to these name servers)"
Created [https://dns.googleapis.com/dns/v1/projects/your-project-id/managedZones/lab-public].
ns-cloud-c1.googledomains.com.
ns-cloud-c2.googledomains.com.
ns-cloud-c3.googledomains.com.
ns-cloud-c4.googledomains.com.
ask Google's name server directly: 203.0.113.10
ask the normal internet path:       (empty: the domain does not point to these name servers)

Google's name server already answers for the zone, but normal lookups find nothing, because the domain is not registered to these name servers.

For your own domain (not tested here): at your domain registrar, replace the domain's name servers with the four ns-cloud-... names from your zone. After that, the whole internet asks Cloud DNS.

Private and public zones
Private zone records, resolution inside the VPC, nothing from the internet, and a public zone answered by Google's name servers

Step 5: a caching resolver

Why: inside the VPC every question goes straight to Cloud DNS, so changes show at once. Out in the world, most people ask a caching resolver. We run one on the VM (dnsmasq on port 5353), set up to ask the VPC resolver and keep answers for their TTL. Save the settings as dnsmasq-lab.conf:

# A small caching DNS resolver on port 5353, like the resolver of an internet provider or an office network.
# It asks the VPC resolver (the metadata server) and keeps every answer for as long as its TTL allows.
port=5353
no-resolv
server=169.254.169.254
cache-size=1000
# Inside the VPC, every question goes straight to Cloud DNS, so a change shows at once. Out in the world, most people
# ask a CACHING resolver (their internet provider, the office network). We run one here: dnsmasq on port 5353
sudo apt-get install -y -qq dnsmasq > /dev/null 2>&1; sudo cp dnsmasq-lab.conf /etc/dnsmasq.d/lab.conf && sudo systemctl restart dnsmasq && echo "dnsmasq caching resolver on port 5353"
dig @127.0.0.1 -p 5353 +noall +answer web.lab.internal A
dnsmasq caching resolver on port 5353
web.lab.internal.	60	IN	A	10.142.0.23

Break it: change a record, keep getting the old answer

We change web.lab.internal to a new IP (TTL 60 seconds). Cloud DNS has the new value at once; the caching resolver does not:

# Break it: change the IP of web.lab.internal. The caching resolver keeps the OLD answer until its TTL (60 s) runs out
dig @127.0.0.1 -p 5353 +noall +answer web.lab.internal A | awk '{print "cached before the change: " $5 ", remaining ttl " $2 " s"}'
gcloud dns record-sets update web.lab.internal. --zone=lab-private --type=A --ttl=60 --rrdatas=10.0.0.99 > /dev/null && echo "record changed to 10.0.0.99 at $(date -u +%H:%M:%S)"
echo "Cloud DNS itself says: $(dig +short web.lab.internal A)"
for i in $(seq 1 10); do A=$(dig @127.0.0.1 -p 5353 +noall +answer web.lab.internal A | awk '{print $5 " (ttl " $2 " s)"}'); echo "$(date -u +%H:%M:%S) cached answer: $A"; case "$A" in 10.0.0.99*) break;; esac; sleep 10; done
cached before the change: 10.142.0.23, remaining ttl 48 s
record changed to 10.0.0.99 at 08:46:52
Cloud DNS itself says: 10.0.0.99
08:46:52 cached answer: 10.142.0.23 (ttl 46 s)
08:47:02 cached answer: 10.142.0.23 (ttl 36 s)
08:47:12 cached answer: 10.142.0.23 (ttl 26 s)
08:47:22 cached answer: 10.142.0.23 (ttl 16 s)
08:47:32 cached answer: 10.142.0.23 (ttl 6 s)
08:47:42 cached answer: 10.0.0.99 (ttl 60 s)

For 50 seconds the cache kept handing out the old address while the TTL counted down (46, 36, 26, 16, 6), then picked up the new one. Every user behind such a resolver would still reach the old server during that time. With a TTL of 3,600 seconds (common), that would be up to an hour.

Fix it: lower the TTL before a planned change

The habit: a day before a migration, lower the TTL (we use 5 seconds), wait for the old TTL to pass, then make the change:

📝⭐ Recommended by CareerByteCode · Certification examGoogle Analytics Certification (GA4) PracticeTake the examTake the exam →
# Fix the habit: lower the TTL BEFORE a planned change (here to 5 s), wait for the old TTL to pass, then change
gcloud dns record-sets update web.lab.internal. --zone=lab-private --type=A --ttl=5 --rrdatas=10.0.0.99 > /dev/null && echo "ttl lowered to 5 s"; sleep 65
dig @127.0.0.1 -p 5353 +noall +answer web.lab.internal A | awk '{print "cached now: " $5 ", ttl " $2 " s"}'
gcloud dns record-sets update web.lab.internal. --zone=lab-private --type=A --ttl=5 --rrdatas=10.0.0.42 > /dev/null && echo "changed to 10.0.0.42 at $(date -u +%H:%M:%S)"
for i in $(seq 1 6); do A=$(dig @127.0.0.1 -p 5353 +short web.lab.internal A); echo "$(date -u +%H:%M:%S) cached answer: $A"; if [ "$A" = 10.0.0.42 ]; then break; fi; sleep 3; done
ttl lowered to 5 s
cached now: 10.0.0.99, ttl 5 s
changed to 10.0.0.42 at 08:49:01
08:49:01 cached answer: 10.0.0.99
08:49:04 cached answer: 10.0.0.42

The cache picked up the new address within about 3 seconds. After the migration, raise the TTL again so resolvers do not ask all the time.

Break it and fix it
TTL 60: the cache serves the old IP for 50 seconds; TTL 5: the new IP in 3 seconds

Break it again: a CNAME next to another record

DNS has a rule: a name with a CNAME cannot have any other record. We try to add an A record to www.lab.internal:

# Break it again: a CNAME next to another record with the same name is not allowed by the DNS rules
gcloud dns record-sets create www.lab.internal. --zone=lab-private --type=A --ttl=60 --rrdatas=10.0.0.7 2>&1 | grep -iE "error|cname" | head -2 | cut -c1-220
ERROR: (gcloud.dns.record-sets.create) HTTPError 400: The resource record set 'entity.rrset' is invalid because the DNS name 'www.lab.internal.' has a resource record set of the type 'CNAME'. A DNS name may have either o
The CNAME rule
An A record next to a CNAME refused; clean up: 0 lab zones left

Cloud DNS refuses it with exactly that rule. This is also why you cannot put a CNAME on your bare domain (example.com), which already has NS and SOA records: use A records there, or your DNS provider's alias feature.

POC: prove it works

  • A private zone answered inside the VPC and gave nothing to the internet (Steps 2 and 3).
  • A public zone was answered by Google's name servers (Step 4).
  • A caching resolver kept the old answer until its TTL ran out (Break it); a low TTL made the change visible within seconds (Fix it).
  • Cloud DNS enforced the CNAME rule (Break it again).

Clean up (delete everything)

Why: a zone can only be deleted when it holds nothing but its own NS and SOA records, so the clean-up removes the records first. It also removes dnsmasq.

# Clean up: a zone can only be deleted when it holds nothing but its own NS and SOA records
for z in lab-private lab-public; do
  gcloud dns record-sets list --zone=$z --format="csv[no-heading](name,type)" 2>/dev/null | grep -vE ",(NS|SOA)$" | while IFS=, read -r n t; do gcloud dns record-sets delete $n --zone=$z --type=$t > /dev/null 2>&1; done
  gcloud dns managed-zones delete $z --quiet 2>&1 | tail -1
done
sudo apt-get purge -y -qq dnsmasq > /dev/null 2>&1; sudo rm -f /etc/dnsmasq.d/lab.conf; echo "dnsmasq removed"
echo "lab zones left: $(gcloud dns managed-zones list --format="value(name)" | grep -c '^lab-')"
Deleted [https://dns.googleapis.com/dns/v1/projects/your-project-id/managedZones/lab-private].
Deleted [https://dns.googleapis.com/dns/v1/projects/your-project-id/managedZones/lab-public].
dnsmasq removed
lab zones left: 0

FAQ

How long does a DNS change take to "propagate"? Nothing is pushed: each resolver asks again when its cached copy expires. So the answer is "up to the old TTL", which is why you lower it first.

Public or private zone? Public for names the internet uses (your website). Private for internal names (databases, internal APIs) that should never be visible outside.

What is split-horizon DNS? A public and a private zone with the same name, so api.example.com gives a private IP inside your VPC and a public one outside. We did not test it here.

Does Cloud DNS support DNSSEC? Yes, for public zones, to protect answers from tampering. We did not turn it on in this lab.

Why can I not CNAME my bare domain? The bare domain must have NS and SOA records, and a CNAME cannot sit next to other records. Use A records or an alias feature.

Troubleshooting

You seeWhyFix
A private name does not resolveThe zone is not attached to this VPC, or you are outside itAttach the network to the zone; test from a VM in that VPC
Users still reach the old serverResolvers cached the old answerWait for the TTL; next time lower it a day before
A DNS name may have either one CNAME...CNAME plus another record on one nameUse one CNAME only, or A records
The zone will not deleteIt still has recordsDelete all records except NS and SOA first
Your domain does not use Cloud DNSThe registrar still points to old name serversSet the four ns-cloud-... names at your registrar

What next

Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.

Read next →Google Cloud VPC for Beginners: Subnets, Routes and Firewalls⏱ 10 min

⭐ Keep learning with CareerByteCode

Hand-picked to go deeper on this topic
News, guides and updates from the CareerByteCode team
🏅 Expert writer

The CareerByteCode team writes about what is new on careerbytecode.in: courses, labs, exams, events and the community, which is free to join for influencers and content creators in tech.

Get CareerByteCode Team's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

Related blogs

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →