Teitunnel

Load balancing

Serve one hostname from several machines, with health checks and failover.

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.

What you need

  • Cloudflare Load Balancing, a paid add-on. Turn it on in the Cloudflare dashboard under Traffic ▸ Load Balancing.
  • The token permissions Account · Load Balancing: Monitors and Pools · Edit and Zone · Load Balancers · Edit. If they're missing, Teitunnel shows how to add them.
  • The same route on each machine: run Teitunnel (or teitunnel-cli on a server) on every machine, and add the route with the same hostname on each.

Turn it on

Select the route in Routes and choose Load Balance…, or from the terminal:

teitunnel-cli route balance app.example.com

The plan shows what's created before anything is:

  • a health check that requests https://app.example.com/ through each tunnel and expects a 2xx answer (after redirects);
  • a pool with one endpoint per machine: each tunnel's <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.

With only one machine serving the route so far, the plan warns you: add the route on another machine and balance again to include it.

See how each machine is doing

The route's details list 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.

Machines joining and leaving

A machine that adds the route later joins the pool by itself, instead of 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-cli route unbalance app.example.com) deletes the load balancer, pool and health check. The hostname's DNS record serves it again.

Teitunnel marks what it creates (teitunnel:lb=<hostname> in the description) and only changes those. If a load balancer made elsewhere already answers for the hostname, the plan stops and asks you to manage it in the dashboard. Every step can be undone, and a failure part-way puts back what was there.

Edit on GitHub

On this page