archive2021

Publishing a home Grafana through a Cloudflare Tunnel, without port forwarding

Cloudflare made its Tunnel (formerly Argo Tunnel) free for everyone. How to publish a home Grafana with a named tunnel and an ingress config, and what to protect before you do it.

4 min read#ops#cloudflare#grafana

On 15 April 2021 Cloudflare announced that the tunnel part of Argo Tunnel is free for all accounts, and renamed it Cloudflare Tunnel. Before this it was billed by bandwidth as part of Argo Smart Routing, which is still a paid add-on. (Cloudflare blog)

Since 2018 one Grafana has been reachable from outside through ports 80 and 443 on the home router, forwarded to nginx with Let’s Encrypt (that setup). Everything else I reach through WireGuard. Since last year the sensor dashboards live in a second Grafana on the Pi 4, and for that one I always had to start the VPN first, which I do not want on the phone. Opening more ports is not the answer either: the home IP is public, and some ISPs do not give you a public IPv4 at all. A tunnel avoids both: cloudflared runs on your machine and makes outbound connections to Cloudflare. Nothing listens on the router.

I moved one Grafana instance this week. These are the steps. You need a domain whose DNS is on Cloudflare.

When this is a good idea

It fits a small service at home that you want to reach from outside: a dashboard, a webhook receiver. It does not fit if you do not want Cloudflare to see your traffic. Cloudflare terminates TLS, so it can read everything that goes through the tunnel. For a sensor dashboard I accept that. For some things I would use a VPN instead.

Install and log in

cloudflared has builds for amd64, arm and arm64 on its GitHub releases page. On a Pi with the 32-bit OS, take the arm build. Then:

cloudflared tunnel login

This prints a URL. Open it in a browser, pick the domain, and a certificate is saved to ~/.cloudflared/cert.pem.

Create a named tunnel

cloudflared tunnel create home

This creates the tunnel and writes a credentials file ~/.cloudflared/<UUID>.json. Treat that file like a password. Anyone with it can run your tunnel.

Point a hostname at the tunnel:

cloudflared tunnel route dns home grafana.example.com

It creates a CNAME record to <UUID>.cfargotunnel.com.

Config with ingress rules

# ~/.cloudflared/config.yml
tunnel: home
credentials-file: /home/pi/.cloudflared/<UUID>.json

ingress:
  - hostname: grafana.example.com
    service: http://localhost:3000
  - service: http_status:404

The last rule has no hostname and catches everything else. cloudflared refuses to start without it. You can test the rules before running:

cloudflared tunnel ingress validate
cloudflared tunnel ingress rule https://grafana.example.com/
cloudflared tunnel run home

Run it as a service

sudo mkdir -p /etc/cloudflared
sudo cp ~/.cloudflared/config.yml ~/.cloudflared/<UUID>.json /etc/cloudflared/
# edit credentials-file in /etc/cloudflared/config.yml to the new path
sudo cloudflared service install
sudo systemctl status cloudflared

My first try failed here. The service reads /etc/cloudflared/config.yml, and my copied config still pointed to the credentials file in my home directory. The error message was clear once I looked at journalctl -u cloudflared.

Before you open it

Grafana is now reachable by anyone on the internet. Until now this Grafana on the Pi was only reachable on the LAN and through WireGuard, so I had become lazy with it. The settings I made for the public one in 2018 were not there. Things I changed:

  • Turned off anonymous access and sign-up in grafana.ini ([auth.anonymous] enabled = false, [users] allow_sign_up = false).
  • Changed the admin password, which was still the one from the first setup.
  • Set root_url in [server] to https://grafana.example.com/ so links and redirects are right.
  • Made Grafana listen on 127.0.0.1 only, since the tunnel connects locally.

Cloudflare Access can put a login page in front of the hostname, so a request never reaches Grafana without passing it. I have not set this up yet. It is the next thing to try, because it would also protect other services that have weaker logins than Grafana.

After two days without problems I moved the old public Grafana behind the tunnel too, and closed ports 80 and 443 on the router. The only open port left is the one for WireGuard.