Cristian Gutiérrez

How to use Cloudflare Tunnel with kgateway as the load balancer of a bare-metal Kubernetes cluster: no LoadBalancer, no MetalLB and no open ports.

Cloudflare Tunnel as a Kubernetes load balancer

14 min
2776 words

On dedicated servers or at home, a Service of type LoadBalancer sits in <pending> forever, because there’s no cloud provider to hand it an IP.

My clusters don’t have one either. Public traffic comes in through a Cloudflare Tunnel and goes from there to kgateway, a Gateway API implementation built on Envoy. No LoadBalancer, no MetalLB, no open ports on the nodes. The cloudflared replicas are the load balancer, and Cloudflare handles the failover.

Setting it up is the easy part. Most of what broke was streaming responses, which have a timeout at every hop. Below: how the load gets spread, the manifests I use, and the things I ran into.

Illustration: users reach the Cloudflare edge and the cloudflared replicas open the tunnel from inside the cluster, with no open ports, through to kgateway and the apps; a connector that goes down doesn't cut the traffic

Tunnel or LoadBalancer: what each one solvesh2

These aren’t the same kind of thing. MetalLB, k3s servicelb and a provider’s load balancer give a LoadBalancer Service an IP, so they work for any TCP or UDP traffic. A tunnel gives nothing an IP. It’s an HTTP proxy that Cloudflare runs in front of the cluster, so it only replaces them if everything you publish is HTTP or WebSocket. In my case it is.

With that in mind, these are the ways to get public HTTP into a bare-metal cluster:

OptionTrafficInbound ports on the nodesFailoverCatch
k3s servicelb (Klipper)Any TCP/UDPOn every node, or only on the ones labelled svccontroller.k3s.cattle.io/enablelbNone by itself. Something in front has to point at several nodesEvery node that serves it is a public entry point
MetalLBAny TCP/UDPOn the node that holds the IPThe IP moves to another nodeL2 mode needs the nodes on one network segment, BGP needs a router that peers with you. Dedicated servers in different data centres rarely have either
The provider’s load balancerTCPOnly to the load balancer, if it reaches the nodes over a private networkBuilt in, with health checksTied to one provider and its rules. Hetzner’s, for example, can target dedicated servers, but only in its eu-central zone
Ingress on several nodes + Cloudflare proxyHTTP(S)80/443, allowed only from Cloudflare’s IP rangesCloudflare Load Balancing (paid), or DNS records you update yourselfOpen ports and an IP allow-list to keep in sync
Cloudflare TunnelHTTP(S), WebSocketNoneBuilt in: the edge stops using a dead connector within secondsAll public traffic depends on one tunnel object

The tunnel really competes with the row above it. Both put TLS, WAF, rate limiting and DDoS protection at Cloudflare’s edge, and both carry Cloudflare’s limits: 125 s to the first byte, and 100 MB per request body on the Free and Pro plans. The difference is the ports. cloudflared only opens outbound connections, so the node firewall lets in SSH and the VPN and nothing else, and there’s no list of Cloudflare IP ranges to keep up to date.

Two smaller things I get from it: customer domains (Cloudflare for SaaS) go through the same tunnel with no extra infrastructure, and the gateway proxy stays a ClusterIP, with no hostPort or NodePort.

The catch in the last row is the one to think about. If the tunnel gets deleted, the token is rotated badly or Cloudflare has a Tunnels incident, everything public goes down at once. And if you also publish something that isn’t HTTP, like a mail server or anything over UDP, you still need one of the other rows for it.

How it spreads the load: three levelsh2

This is how a request travels. It’s interactive: click a cloudflared pod to kill it and watch the edge stop sending it traffic.

Kubernetes clusterCloudflare edgeClientsOutbound tunnel connectionsCloudflareMadridCloudflarePariscloudflared0 requestscloudflared0 requestscloudflared0 requestsServiceClusterIPkgateway0 requestskgateway0 requestsAppAppApp
Click a cloudflared pod to kill it.Each cloudflared reuses its keep-alive connections, so it sticks to one gateway pod.

Level 1: from Cloudflare’s edge to the connectorsh3

Each cloudflared replica opens four connections to the edge, spread across several Cloudflare data centres (colos). With three replicas that’s twelve connections to the same tunnel.

When a request reaches any colo, Cloudflare carries it over its network to one of those connections. There’s no affinity: any connector can receive any hostname. If a pod dies, its connections go away and the edge stops using them within seconds. That’s the failover, and there’s nothing to configure.

You don’t pick the colo. Cloudflare connects each node to whichever colos fit its network, so nodes in different data centres end up connected to different cities.

Level 2: from cloudflared to the gatewayh3

Every tunnel rule points at the same origin: the Service kgateway creates for the Gateway, for example http://public-gateway.kgateway-system.svc.cluster.local:80.

That Service is a ClusterIP, and kube-proxy balances per TCP connection, not per request. On top of that, cloudflared keeps a keep-alive pool to the origin (100 connections and 90 seconds idle by default). The result is that the split across the Envoy pods is sticky: one pod can see far more traffic than another for minutes. The counters in the diagram show it.

This matters if you have alerts that expect every gateway pod to get traffic: a freshly started pod can take a while to see its first request. Give those alerts a for of a few minutes.

Level 3: from the gateway to the apph3

Envoy routes each request according to the HTTPRoute objects (by Host, Path, headers…) and spreads it across the endpoints of each app’s Service. Unlike level 2, balancing here is per request, because Envoy talks to the pods directly.

Setting it uph2

1. Create the tunnelh3

The easiest option is a remotely-managed tunnel: the configuration lives in Cloudflare and the pod only needs a token.

In Zero Trust → Networks → Tunnels → Create a tunnel, pick cloudflared, name it and copy the token. Then, under Public Hostnames, add each hostname pointing at the gateway’s Service:

HostnameService
app.example.comhttp://public-gateway.kgateway-system.svc.cluster.local:80
*.apps.example.comhttp://public-gateway.kgateway-system.svc.cluster.local:80
(everything else)http_status:404

Rules are evaluated top to bottom, so a wildcard above a specific host swallows it. When you add a hostname, Cloudflare creates the DNS record for you: a proxied CNAME to <tunnel-id>.cfargotunnel.com.

Publishing a hostname takes two things: the tunnel rule and the HTTPRoute in the cluster. Without the first, Cloudflare doesn’t know where to send it. Without the second, Envoy answers 404.

2. The gateway, without a LoadBalancerh3

By default kgateway creates the proxy’s Service as a LoadBalancer, which on bare metal would sit in <pending>. A GatewayParameters object turns it into a ClusterIP, since only cloudflared will talk to it, from inside the cluster:

apiVersion: gateway.kgateway.dev/v1alpha1
kind: GatewayParameters
metadata:
name: public-gateway
namespace: kgateway-system
spec:
kube:
deployment:
replicas: 2
service:
type: ClusterIP
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: public-gateway
namespace: kgateway-system
spec:
gatewayClassName: kgateway
infrastructure:
parametersRef:
group: gateway.kgateway.dev
kind: GatewayParameters
name: public-gateway
listeners:
- name: http
port: 80
protocol: HTTP # TLS ends at Cloudflare
allowedRoutes:
namespaces:
from: All

The listener is HTTP because Cloudflare terminates TLS and the hop to the gateway stays inside the cluster. If you want that hop encrypted too, add an HTTPS listener with your certificate and point the tunnel at https:// with originServerName.

Each app is published with an HTTPRoute attached to that gateway:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: app
namespace: app
spec:
parentRefs:
- name: public-gateway
namespace: kgateway-system
hostnames: ['app.example.com']
rules:
- backendRefs:
- name: app
port: 8080
timeouts:
request: 0s # no limit; see the timeouts section

3. The token as a Secreth3

Terminal window
kubectl create namespace cloudflared
kubectl -n cloudflared create secret generic cloudflared-token \
--from-literal=token=<TUNNEL_TOKEN>

Even better if you manage it with External Secrets, Infisical, Vault or Sealed Secrets: if you lose the cluster, this Secret isn’t in git.

4. The cloudflared Deploymenth3

A Deployment, not a DaemonSet: the number of connectors doesn’t need to match the number of nodes.

apiVersion: apps/v1
kind: Deployment
metadata:
name: cloudflared
namespace: cloudflared
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: cloudflared
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0 # never lose capacity during a rollout
maxSurge: 1
template:
metadata:
labels:
app.kubernetes.io/name: cloudflared
spec:
terminationGracePeriodSeconds: 200 # grace-period + margin before SIGKILL
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: cloudflared
containers:
- name: cloudflared
image: cloudflare/cloudflared:<version> # pin it, never :latest
args:
- tunnel
- --no-autoupdate
- --metrics
- 0.0.0.0:2000
- --protocol
- http2
- --edge-ip-version
- "4"
- --grace-period
- 170s
- run
- --token
- $(TUNNEL_TOKEN)
env:
- name: TUNNEL_TOKEN
valueFrom:
secretKeyRef:
name: cloudflared-token
key: token
ports:
- name: metrics
containerPort: 2000
readinessProbe:
httpGet: { path: /ready, port: 2000 }
livenessProbe:
httpGet: { path: /ready, port: 2000 }
resources:
requests: { cpu: 100m, memory: 128Mi }
limits: { memory: 512Mi }
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities: { drop: [ALL] }
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: cloudflared
namespace: cloudflared
spec:
minAvailable: 2
selector:
matchLabels:
app.kubernetes.io/name: cloudflared

The arguments, one by one:

FlagWhy
--no-autoupdateThe image pins the version. The binary shouldn’t update itself inside a pod
--metrics 0.0.0.0:2000Prometheus metrics, /ready for the probes and /config to see the live configuration
--protocol http2The default (auto) uses QUIC and falls back to HTTP/2 if UDP fails. In my case QUIC kept cancelling long streaming responses; HTTP/2 over TCP is stable
--edge-ip-version 4If the nodes have no working IPv6 egress, with auto some connections get stuck retrying over IPv6
--grace-period 170sOn SIGTERM, stop accepting and wait for in-flight requests. The default is 30 s
--tokenFrom the Secret

/ready returns 200 only when the pod has at least one connection to the edge, so it works as-is for both probes.

Careful with the grace periodh3

With the default 30 s, every rollout and every scale-down cut off streaming responses halfway. Raising it fixes that, but there’s a ceiling: cloudflared won’t start with more than 3 minutes (grace-period must be equal or less than 3m0s). I found out when I set it to 270 s and almost every new pod went into CrashLoopBackOff at once. Thanks to maxUnavailable: 0 and the PDB, the old pods kept serving, so it stayed a config mistake and not an outage.

And terminationGracePeriodSeconds must be longer than the grace period, with some margin, or Kubernetes sends SIGKILL before draining finishes.

Spreading the connectorsh3

topologySpreadConstraints with ScheduleAnyway spreads pods but doesn’t force it: with more replicas than nodes, the scheduler can leave nodes without a connector. If your nodes sit in two data centres, add a zone constraint too (topology.kubernetes.io/zone) so both are represented in the tunnel.

Scaling: watch the metrich2

The temptation is to scale cloudflared with KEDA on cloudflared_tunnel_concurrent_requests_per_tunnel, the open requests per pod. That works while requests are short. With streaming responses that last minutes (SSE, for example with LLMs), concurrency shoots up while the pod barely uses CPU, and the autoscaler stays pinned at its maximum forever.

For that workload it’s better to scale on CPU, on rate(cloudflared_tunnel_total_requests[5m]), or to just fix the replica count. If you do use KEDA or an HPA, remember that every scale-down is a drain, and the grace period matters again.

Streaming and WebSocket: the timeout chainh2

Every hop has its own timeout. When “streams get cut”, this is the table to check:

HopTimeoutWhat happens when it’s exceeded
Cloudflare edge → origin125 s to the first byte (Enterprise can raise it up to 6,000 s)Error 524
cloudflared → gateway30 s connectTimeout502 from the edge
cloudflared on shutdown--grace-period (3 min max)The stream is cut with no body
Envoy, route with no timeout15 s for the complete response504 or a stream cut at 15 s

The last row is the one nobody expects. If an HTTPRoute sets no timeout, kgateway leaves the route timeout unset and Envoy applies its own default: 15 seconds, counted until the whole response is done. Any streaming response that runs longer gets cut. For those routes, set timeouts.request on the HTTPRoute (0s disables it), or use a TrafficPolicy with timeouts.request and timeouts.streamIdle.

Two more traps:

  • Envoy rejects WebSocket by default. It answers every handshake with 403 until you declare the upgrade in the listener policy:

    apiVersion: gateway.kgateway.dev/v1alpha1
    kind: HTTPListenerPolicy # ListenerPolicy in recent kgateway releases
    metadata:
    name: public-gateway
    namespace: kgateway-system
    spec:
    targetRefs:
    - group: gateway.networking.k8s.io
    kind: Gateway
    name: public-gateway
    upgradeConfig:
    enabledUpgrades: [websocket]
  • Anything that buffers the response (a filter or plugin that reads the whole body) means the first byte doesn’t arrive until the response is finished. If that takes more than 125 s, Cloudflare returns 524 even though the backend is answering fine.

With lots of HTTP/2 connections and long streams, the kernel’s default network buffers can fall short. A DaemonSet that raises these sysctl values on the nodes helped me:

net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.core.netdev_max_backlog = 65535
net.core.somaxconn = 32768

The client’s real IPh2

For Envoy, every connection comes from the IP of a cloudflared pod. The real IP travels in the headers Cloudflare sets: CF-Connecting-IP, X-Forwarded-For, True-Client-IP, plus CF-IPCountry with the country.

In the same listener policy you can tell Envoy which X-Forwarded-For hops to trust (xffTrustedCIDRs, with Cloudflare’s ranges). That way the access log and the backends see the client’s IP rather than the pod’s.

But anyone who reaches the gateway without going through Cloudflare can forge those headers, for example a pod inside the cluster. If you use them for rate limiting or auditing:

  • Use one Gateway only for tunnel traffic, and a separate one for internal traffic.
  • On the internal gateway, strip the CF-* headers with a RequestHeaderModifier filter on the routes (or a TrafficPolicy).
  • Use NetworkPolicies so that only the cloudflared pods can reach the public gateway.

NetworkPoliciesh2

Backends usually accept traffic only from the gateway’s Envoy pods. Two details:

  • If a tunnel hostname points straight at a Service, skipping the gateway, that app has to allow the cloudflared pods.
  • Some CNIs (kube-router, for example) apply rules asynchronously. A new gateway pod can become Ready before the backends’ NetworkPolicies know about it, and it returns 503 for those seconds. An initialDelaySeconds on its readiness probe that covers the delay avoids it.

Observabilityh2

cloudflared exposes Prometheus metrics on the --metrics port. The ones I watch:

MetricWhat for
cloudflared_tunnel_ha_connectionsShould be 4 per pod
cloudflared_tunnel_total_requestsThroughput, with rate()
cloudflared_tunnel_request_errorsErrors while proxying
cloudflared_tunnel_response_by_codeResponses by status code
cloudflared_tunnel_concurrent_requests_per_tunnelOpen requests per pod
cloudflared_tunnel_server_locationsWhich colo each connection goes to
cloudflared_orchestration_config_versionRemote config version. If it differs between pods, one of them missed a change
sum(cloudflared_tunnel_ha_connections) # expected: 4 × replicas
sum by (status_code)(rate(cloudflared_tunnel_response_by_code[5m]))
sum(rate(cloudflared_tunnel_request_errors[5m]))
sum by (edge_location)(cloudflared_tunnel_server_locations == 1)

My alerts: fewer than 4 HA connections on a pod for 5 minutes, a pod down, and an error rate above the normal floor. That floor isn’t zero: clients that drop streams produce a steady trickle of context canceled.

The cloudflared_proxy_connect_latency_* metrics look like tunnel latency, but they measure TCP and WARP proxy connections. On a tunnel that only publishes HTTP hostnames they get almost no data, so an alert on them never fires.

On the gateway side, Envoy’s JSON access log (also set in the listener policy) is the quickest way to match a complaint with a status. The response_flags field tells you whether the cut was a route timeout (UT), an upstream reset (UC) or the client leaving (DC).

To see the configuration a cloudflared pod has actually loaded (the image has no shell):

Terminal window
kubectl -n cloudflared port-forward deploy/cloudflared 2000:2000 &
curl -s localhost:2000/config | jq '.version, [.config.ingress[] | {hostname, service}]'

In the logs, ingressRule=N is the rule index, starting at 0.

Reading the errorsh2

What you seeWhereMost likely cause
context canceledcloudflaredThe client closed the request before it finished. Normal with streams and retries
unexpected EOF on several connections at oncecloudflaredThe origin closed: a gateway rollout or a backend restarting
504 or a stream cut at 15 sEnvoyThe HTTPRoute has no timeout and Envoy’s default applies
403 on a WebSocket handshakeEnvoyenabledUpgrades: [websocket] is missing on the listener
503 with a trace in EnvoyEnvoyIt can’t reach the backend: NetworkPolicy, empty endpoints
502 with no trace in EnvoyEdgecloudflared can’t connect to the gateway
524EdgeThe first byte didn’t arrive within 125 s. Almost always something buffering
530 / 1033EdgeThe tunnel has no healthy connectors, or the token is invalid

What I’d do differentlyh2

  • Keep the tunnel configuration in git. With a remotely-managed tunnel, the rules live in the dashboard, with no review or history. With Terraform (cloudflare_zero_trust_tunnel_cloudflared_config) they become code.
  • Use more than one tunnel if you serve things that shouldn’t fail together.
  • Get the token out of a one-off kubectl create, so the Secret can be recreated from a secrets manager.

I’d still pick the tunnel over MetalLB for public HTTP on bare metal. What I’d do earlier is the timeout table: decide the value for every hop before the first long stream gets cut, not after.

FAQh2

Do I need MetalLB if I use Cloudflare Tunnel?h3

Not for the traffic that comes in through the tunnel. The gateway’s Service can stay a ClusterIP, because cloudflared talks to it from inside the cluster.

Does Cloudflare Tunnel load balance across replicas?h3

Yes. Every cloudflared replica is a connector of the same tunnel, and Cloudflare’s edge spreads requests across all their connections and stops using the ones from a connector that’s down.

Does it work with Gateway API or only with Ingress?h3

Both. The tunnel only needs an origin URL, so it doesn’t matter whether there’s an ingress controller behind it or a gateway like kgateway, Envoy Gateway or Cilium.

How many cloudflared replicas do I need?h3

At least two for failover. Beyond that it depends on traffic, and they should be spread across different nodes and data centres.

QUIC or HTTP/2?h3

By default cloudflared tries QUIC. If you serve long streaming responses and see them getting cut, try --protocol http2.

Keep up by email?

One email when I publish a new post, no digests, no filler. Unsubscribe in one click.

I only use your email to tell you about new posts. Prefer RSS? Here it is

Did this post help you?Buy me a coffee

Comments