Routing DNS and Running the Tunnel
Outcome: a named tunnel answering at its real hostname, all day, started by the machine itself rather than by a person at a terminal. This chapter's steps act on your Cloudflare account and your DNS, so — like chapter 5's creation steps — they are documented rather than run by the lab, and the text says so where it matters. What the lab verified (the config file itself, and every error that guards these steps) is in chapters 5 and 6.
Give the tunnel its public name
A tunnel object answers to nothing until DNS points at it. One command does it:
cloudflared tunnel route dns photos demo.example.org
route dns uses cert.pem (chapter 5)
to write one record into the
example.org zone: a CNAME from
demo.example.org to
<tunnel-uuid>.cfargotunnel.com. Chapter
2 named that target as the trick in one
line, and it bears repeating slowly: the
record does not contain your address,
your machine's name, or anything about
your network. It says this hostname is
served by that tunnel, wherever its
connectors currently are. Move the
tunnel to another machine, another
house, another continent — the record
does not change; the connectors register,
and the name follows. If a record for
the hostname already exists, route dns
refuses to overwrite it (the --overwrite-dns
flag exists for when you mean it), which
is the correct reluctance for a command
that can redirect a name.
Run it
cloudflared tunnel run photos
cloudflared reads the config named for
the tunnel (chapter 6), loads the
credentials file, and opens its four
edge connections (chapter 8). Its log
lines report each connection registered,
with the data centre it landed in; from
that moment https://demo.example.org/
answers with the origin behind rule #0.
Ctrl-C, a dead process, or a reboot ends
all of it — the tunnel object remains in
the account (chapter 5), the DNS record
remains in the zone, and visitors meet a
Cloudflare error page naming the tunnel
unreachable (error 1033). A stopped
tunnel is not a subtle failure; it is a
sign on the door.
Run it without you
A tunnel you start by hand is a tunnel that stops when the terminal closes. cloudflared therefore knows how to install itself as a system service:
sudo cloudflared service install
On Linux this writes a systemd unit
that runs the tunnel from
/etc/cloudflared/ — config, credentials
and all — at boot, restarting it when it
falls (macOS uses a launchd job, Windows
a service; the shape is the same and all
of it is documented-only in this guide).
Two habits belong with the service
route. Keep the /etc/cloudflared/
copies and your working copies in step —
the service reads its config, not the
one in your home folder, and "I edited
the rules and nothing changed" is this
chapter's most common ghost story. And
validate before you restart (chapter 6):
a service that fails to start on a broken
config has turned an edit into an outage.
Knowing it is alive
A running cloudflared keeps a small local
status endpoint — the metrics
endpoint, on a loopback port (chosen at
random unless --metrics fixes it) —
which answers /ready with how many edge
connections are currently up. The
companion command asks the question in
script-friendly form:
cloudflared tunnel ready
It exits 0 when at least one connection
is ready and non-zero otherwise (the
subcommand appears in the lab's
cloudflared tunnel --help, transcript
t00) — the natural health check for
whatever supervises the tunnel, and a
sharper question than "is the process
running", asked of the part that actually
matters: the connections.