Skip to Content
Teitunnel

Guides

Load balancing

Serve one hostname from several machines through Cloudflare Tunnel with Cloudflare Load Balancing: health checks, failover and per-region status.

When the same app runs on several machines, Teitunnel can put Cloudflare's load balancer in front of it: visitors go to whichever machines are healthy, and traffic moves away from one that goes down. Use it for failover between two servers, or to keep a site up while you restart one machine.

For groups of routes on one machine, see several tunnels instead.

What you need

  • Cloudflare Load Balancing, a paid add-on. Turn it on in the Cloudflare dashboard under Traffic ▸ Load Balancing first.
  • The permissions Account · Load Balancing: Monitors and Pools · Edit and Zone · Load Balancers · Edit. If they're missing, Teitunnel shows what to add ("To load balance a route. Load Balancing is a paid Cloudflare add-on: turn it on in the dashboard first.").
  • The same route on each machine: run Teitunnel (the app, or teitunnel on a server) on every machine, and add the route with the same hostname on each.

Set it up

Add the route on the first machine

teitunnel route add app.yourhost.com 3000

Add the same route on the other machines

A hostname another machine serves is held by that machine, so the plan says so and asks for a confirmation before it points the DNS record here. Confirm it in the app, or pass --take-over:

teitunnel route add app.yourhost.com 3000 --take-over

Until the next step, visitors go to whichever machine the DNS record points at.

Turn on load balancing

Select the route in Routes and choose Load Balance… (the sheet is Load Balance Route; apply with Load Balance), or from the terminal:

teitunnel route balance app.yourhost.com

The plan ("Load balance app.yourhost.com across its tunnels") shows what's created before anything is (see below). With only one machine serving the route so far, it warns: "Only this tunnel serves app.yourhost.com so far. Add the same route on another machine so the load balancer has somewhere to fail over to."

What happens on Cloudflare

  • A health check that requests https://app.yourhost.com/ through each tunnel, with your hostname as the Host header, and expects any 2xx answer after following redirects.
  • A pool with one endpoint per machine: each tunnel's <tunnel id>.cfargotunnel.com, with your hostname as the Host header, as Cloudflare recommends for tunnels.
  • A load balancer named after the hostname. It takes precedence over the DNS record, so the record stays as a fallback.

Teitunnel marks what it creates (teitunnel:lb=<hostname> in the description) and only changes those, from any machine. If a load balancer made elsewhere already answers for the hostname, the plan stops ("app.yourhost.com already has a load balancer Teitunnel didn't create. Change it in the Cloudflare dashboard."). Every step can be undone in Activity, and a failure part-way puts back what was there.

See how each machine is doing

The route's details show Load balancing ("Across every tunnel that routes it") and Machines: every endpoint of the pool, with the result of Cloudflare's health checks from each region that checks it ("Healthy in all 3 regions", "Healthy in 2 of 3 regions", or failing with Cloudflare's reason, such as a timeout). The machine you're on is marked. Checks run about once a minute; the list refreshes while it's open. Balanced routes are marked Balanced in the routes list.

Machines joining and leaving

Once the hostname is balanced, a machine that adds the route joins the pool by itself, without taking the DNS record over. Removing the route on a machine takes it out of the pool; when the last one goes, the load balancer, pool and health check go too.

Turn it off

Stop Load Balancing… (or teitunnel route unbalance app.yourhost.com) deletes the load balancer, pool and health check. The hostname's DNS record serves it again, so visitors go to the machine the record points at.

Limits and costs

Load Balancing is a paid Cloudflare add-on; see Cloudflare's pricing for what your plan includes. Teitunnel creates one load balancer, one pool and one health check per balanced hostname, and adds nothing on top.

Troubleshooting

Edit on GitHub

Last updated on

On this page