Tunnel Tuning
CloudflareTunnelPolicy tunes the cloudflared connector behind a Tunnel Mode Gateway —
its edge transport, replica count, shutdown grace period, CPU autoscaling, and per-rule origin
behavior. This
guide covers every field it has, and the one thing worth checking before assuming any of them
took effect: this is a policy, and a policy’s spec only applies
once its ancestor status says so.
Prerequisites
Section titled “Prerequisites”A Tunnel Mode Gateway already running — see
Quickstart — Tunnel Mode if you don’t have one yet. The
examples below assume a Gateway named my-gateway in the default namespace.
Attach a policy, and wait for it to be accepted
Section titled “Attach a policy, and wait for it to be accepted”A CloudflareTunnelPolicy targets a Gateway by name, through spec.targetRefs — the same
group/kind/name shape every policy in this project uses:
apiVersion: cfgateway.dreamfuselabs.com/v1alpha1kind: CloudflareTunnelPolicymetadata: name: my-tunnel-policyspec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: my-gateway protocol: http2 replicas: 1Applying it doesn’t mean it took effect yet. Check the policy’s own ancestor status for
Accepted before assuming any of its fields reached the managed cloudflared Deployment —
a policy that lost a conflict, or that names a Gateway that doesn’t exist, applies nothing at
all:
kubectl get cloudflaretunnelpolicy/my-tunnel-policy \ -o jsonpath='{.status.ancestors[0].conditions[?(@.type=="Accepted")].status}'This should print True. If it prints False, read the same entry’s message field — it
names either the policy currently holding the target, or which targetRef couldn’t be
resolved. kubectl wait --for=condition=Accepted does not work here: Accepted lives on
status.ancestors[0].conditions, not on a top-level status.conditions, and kubectl wait
only matches top-level conditions. Poll the jsonpath above instead, or watch for the
Normal/Warning Event this project emits on every transition.
protocol: the edge transport, and why it matters for gRPC
Section titled “protocol: the edge transport, and why it matters for gRPC”protocol selects the transport cloudflared speaks to Cloudflare’s edge, passed straight
through as its --protocol flag. It accepts exactly two values: http2 and quic.
Left unset, the controller passes no --protocol flag at all, and cloudflared uses its own
default (QUIC). That default silently drops gRPC trailers, so a GRPCRoute behind an
unconfigured tunnel never carries gRPC traffic correctly — setting protocol: http2 on the
Gateway’s policy is a required opt-in before routing any gRPC traffic through it (see
Routing for the rest of what a GRPCRoute needs beyond this).
The real cost of http2, stated plainly: a tunnel on the http2 transport cannot also carry
WARP-to-Tunnel UDP traffic. The two capabilities don’t coexist on one connector. This is why
protocol is a per-tunnel policy field rather than a cluster-wide setting — it confines the
tradeoff to the Gateways that actually need gRPC, leaving every other tunnel on its ordinary
default. Don’t set protocol: http2 cluster-wide out of caution; set it only on the Gateways
whose routes actually need it.
replicas and terminationGracePeriodSeconds: the HA knobs
Section titled “replicas and terminationGracePeriodSeconds: the HA knobs”A CloudflareTunnelPolicy’s fields all live on one spec, so the examples below build on the
same my-tunnel-policy object from above rather than introducing a second policy targeting
the same Gateway — two policies both targeting my-gateway would contend for it under the
oldest-wins rule above, with only one actually taking effect. Edit the policy in place:
apiVersion: cfgateway.dreamfuselabs.com/v1alpha1kind: CloudflareTunnelPolicymetadata: name: my-tunnel-policyspec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: my-gateway protocol: http2 replicas: 3 terminationGracePeriodSeconds: 60replicasoverrides the managedcloudflaredDeployment’s replica count. More replicas means more independent edge connectors for that tunnel, which is this project’s one supported high-availability knob for the connector itself.terminationGracePeriodSecondsoverrides the managed Deployment’s pod termination grace period, giving in-flight connections through a terminatingcloudflaredpod longer to drain during a rollout before Kubernetes forcibly kills it.
These two fields aren’t the same kind of change, though. terminationGracePeriodSeconds lives
in the managed Deployment’s pod template, so changing it rolls the Deployment: a new pod
template means a real rollout, with the usual rollout mechanics (old pods drain and terminate
as new ones with the updated grace period come up). replicas is different: it’s a plain
Deployment.spec.replicas field outside the pod template, so changing it is an ordinary scale
operation, not a rollout — Kubernetes adds or removes pods from the existing template directly,
with no old/new template distinction and no draining of pods that aren’t being removed.
protocol (above) is the field most likely to surprise you here: changing it does roll the
Deployment, because it changes the container’s --protocol argument, which is part of the pod
template.
autoscaling: scale the connector on CPU
Section titled “autoscaling: scale the connector on CPU”A fixed replicas count is the default. autoscaling instead asks the controller to manage a
Kubernetes HorizontalPodAutoscaler (HPA) for the tunnel’s cloudflared Deployment, scaling on
average CPU utilization:
apiVersion: cfgateway.dreamfuselabs.com/v1alpha1kind: CloudflareTunnelPolicymetadata: name: my-tunnel-policyspec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: my-gateway autoscaling: enabled: true minReplicas: 2 maxReplicas: 6 targetCPUUtilizationPercentage: 70enabledis required.falseturns autoscaling off for this policy even whentunnelDefaultsturned it on, and the tunnel goes back toreplicas.minReplicas(at least 2) andmaxReplicas(at leastminReplicas) are required whenenabledistrue; the API rejects the object otherwise. Withenabled: falseany numbers are accepted but ignored, so you can switch it off and on without deleting them.targetCPUUtilizationPercentageis optional and defaults to 70. It is measured against the container’s CPU request. CPU utilization is the only metric.- While autoscaling is enabled,
replicasis ignored, whether it came from the policy or fromtunnelDefaults. The HPA owns the live replica count, and a reconcile never resets it. A new Deployment starts atminReplicas. Turning autoscaling off deletes the HPA first, then returns the Deployment toreplicas. - The controller creates one HPA per tunnel, with the same name and namespace as the Deployment, and owns its spec: a hand edit to it is overwritten on the next reconcile.
Set at the cluster level instead with the chart’s configuration.tunnelDefaults.autoscaling.*
values (see Helm Values). A policy’s autoscaling replaces the
tunnelDefaults one whole, with no per-field merge.
Before you enable it
Section titled “Before you enable it”- metrics-server must be installed. Without it the HPA has no metrics and never scales,
and nothing in this controller reports that: check
kubectl get hpafor<unknown>. - Don’t attach your own HPA to the tunnel’s Deployment under the tunnel’s name. The controller owns an HPA with that name while autoscaling is on. It will not take over, rewrite or delete one it did not create: enabling autoscaling then fails, with the error in the controller’s log and no condition on the tunnel. Rename or delete yours first. An HPA of your own under a different name is not touched, but two autoscalers on one Deployment conflict.
- Every managed
cloudflaredcontainer now has resource settings, autoscaling or not: a 100m CPU request, a 64Mi memory request and a 128Mi memory limit, with no CPU limit. A CPU utilization target cannot be computed without a CPU request. They are starting values measured on one machine, not tuned ones. Upgrading to the release that adds them rolls every existing tunnel’s Deployment once. A rollout at the default 2 replicas dropped no requests when tested at about 5 requests per second; a single replica was not tested and may briefly drop requests during the roll. - Scale-down was tested only at modest load. Scaling a 3-replica tunnel down to 2 and to 1 by hand under steady load of up to 50 requests per second, and through the autoscaler at about 5 requests per second, with the default 30-second grace period, produced no errors in testing. Higher rates and long-lived connections (WebSockets, streaming) were not tested. Kubernetes’ default scale-down stabilization window is five minutes.
- An average can hide a busy replica. In testing, one replica carried nearly all of a single client’s traffic while the others sat near idle. In one run using a 50m CPU request, the average utilization held at its target while that one pod ran at about 2.6 times that request. Load from many clients may spread differently; that was not measured.
Running a different cloudflared image
Section titled “Running a different cloudflared image”By default each tunnel runs the cloudflared version compiled into the controller release,
listed on the Compatibility page. It is the only version this
project tests. To run another, set the chart value cloudflared.image to a full image
reference (repository and tag, or an @sha256: digest). It applies to every tunnel the
controller manages, and changing it rolls them all once.
An image that differs from the compiled-in version is unsupported: a bug report against it must reproduce on the compiled-in version first.
If the image comes from a registry that needs credentials, list the Secret names under
configuration.tunnelDefaults.imagePullSecrets. Those names resolve in each tunnel’s own
namespace, so you create the Secret in every namespace that holds a tunnel using that
configuration. The controller never reads or copies it. Changing the list, like changing the
image, rolls every tunnel once. A missing or wrong Secret shows up as the new pod’s
ImagePullBackOff. On a tunnel that is already running, the rollout simply stalls while the
old pods keep serving, so the tunnel’s Ready condition does not change: check
kubectl rollout status on the tunnel’s Deployment instead. Because the tunnel reconciler does
not watch CloudflareGatewayConfiguration, an edit to the list lands at the next event or
resync, not immediately.
originRequest: per-rule origin behavior
Section titled “originRequest: per-rule origin behavior”Again, continuing the same my-tunnel-policy object rather than adding another policy on
my-gateway:
apiVersion: cfgateway.dreamfuselabs.com/v1alpha1kind: CloudflareTunnelPolicymetadata: name: my-tunnel-policyspec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: my-gateway protocol: http2 replicas: 3 terminationGracePeriodSeconds: 60 originRequest: http2Origin: true noTLSVerify: trueoriginRequest carries exactly two fields, both applied to every ingress rule cloudflared
generates for the targeted Gateway’s hostnames:
http2Originmakescloudflaredspeak HTTP/2 to the origin Service rather than HTTP/1.1.noTLSVerifydisables origin certificate verification for those rules — useful when the origin serves a self-signed certificate, as a test backend often does.
Both are pointer-typed on the CRD and only ever set, never defaulted to false: leaving a
field out of originRequest means “don’t touch this setting” (cloudflared’s own existing
behavior), not “force it off.” Omit originRequest entirely for ordinary HTTP/1.1 origins —
nothing about it needs to be set unless your origin specifically needs HTTP/2 or has a
certificate verification issue.
Precedence against tunnelDefaults
Section titled “Precedence against tunnelDefaults”Three of this policy’s five fields have a CloudflareGatewayConfiguration.spec.tunnelDefaults
counterpart: replicas, terminationGracePeriodSeconds, and autoscaling. For
those, a field set directly on CloudflareTunnelPolicy always wins over the same field set on
tunnelDefaults, which in turn wins over this project’s own hard defaults (replicas: 2,
terminationGracePeriodSeconds: 30, autoscaling off). Setting a field in both places is
allowed, not rejected — the policy field simply refines the cluster-wide default for its own
Gateway. Leaving one of them unset at every level falls through to the hard default.
autoscaling resolves as a whole struct: a policy’s autoscaling replaces the
tunnelDefaults one entirely, and a policy’s enabled: false is how a Gateway opts out of
autoscaling the configuration turned on.
protocol and originRequest have no tunnelDefaults equivalent. There’s no
config-wide default for either — only two levels exist: whatever the policy sets, or nothing
at all (no --protocol flag; originRequest untouched). Don’t look for
tunnelDefaults.protocol or tunnelDefaults.originRequest; neither field exists on
TunnelDefaults.
Removing a policy
Section titled “Removing a policy”Deleting a CloudflareTunnelPolicy reverts its targeted Gateway’s tunnel to whatever the next
level resolves to, per field: replicas/terminationGracePeriodSeconds/autoscaling fall
through to tunnelDefaults if set, otherwise the hard default; protocol/originRequest fall straight
through to nothing, since neither has a tunnelDefaults level to fall back to. In practice,
deleting a policy that set protocol: http2 means the Gateway’s tunnel goes back to no
--protocol flag at all — QUIC, cloudflared’s own default — which silently breaks gRPC
traffic on that Gateway if any GRPCRoute was depending on http2 being set:
kubectl delete cloudflaretunnelpolicy/my-tunnel-policyRouting covers the rest of what HTTPRoute and GRPCRoute need, including
the zone-level setting GRPCRoute traffic also depends on beyond this policy’s protocol: http2.