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.

✍️ 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.
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

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.confDiG 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.254Two 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 answersThe 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.

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 Adnsmasq caching resolver on port 5353
web.lab.internal. 60 IN A 10.142.0.23Break 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; donecached 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; donettl 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.42The 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 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-220ERROR: (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
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: 0FAQ
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 see | Why | Fix |
|---|---|---|
| A private name does not resolve | The zone is not attached to this VPC, or you are outside it | Attach the network to the zone; test from a VM in that VPC |
| Users still reach the old server | Resolvers cached the old answer | Wait for the TTL; next time lower it a day before |
A DNS name may have either one CNAME... | CNAME plus another record on one name | Use one CNAME only, or A records |
| The zone will not delete | It still has records | Delete all records except NS and SOA first |
| Your domain does not use Cloud DNS | The registrar still points to old name servers | Set the four ns-cloud-... names at your registrar |
What next
- Learn the commands behind this lab: Linux Networking Commands Every Cloud Engineer Needs.
- Put a private database behind a private name: Cloud SQL for PostgreSQL: Create, Connect and Back Up.
- More hands-on posts: ByteBlog. Practice in ByteLabs and see the courses.
Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.
⭐ Keep learning with CareerByteCode
Hand-picked to go deeper on this topicGet 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.
Related blogs
CareerByteCode TeamGoogle Cloud VPC for Beginners: Subnets, Routes and Firewalls
What you build: a custom VPC with two subnets and firewall rules, in your own Google Cloud account. Every step, command and screenshot explained for beginners.
#advanced
CareerByteCode TeamSite-to-Site VPN Between Two Google Cloud Regions
What you build: HA VPN with BGP between two Google Cloud networks in two regions, failover when a tunnel dies, and a wrong-secret fix. Every step tested.
#beginners
CareerByteCode TeamHow to Talk About Your Cloud Projects in Interviews
What you build: a STAR answer for cloud interviews from a real lab: a Cloud Run load test (0 failed, p95 64 ms), a broken release caught on its tag URL, and a script that uses only measured numbers.
#beginners
CareerByteCode TeamCloud Engineer Fresher Roadmap: 12 Labs in 12 Weeks
What you build: a 12-week cloud engineer plan for freshers with one tested lab every week, plus a real Week 0 readiness check that proves billing, a budget, a free VM and a bucket work.
💬 Comments (0)
Top · NewestSign in to comment