cloudflare-tunnel

by rlex

Helm chartv0.8.0App 2023.10.0

Manage and use cloudflare tunnels (also known as argo tunnels) on k8s cluster

About this chart

cloudflare-tunnel

Manage and use cloudflare tunnels (also known as argo tunnels) on k8s cluster

Usage

This chart allows you to deploy cloudflared for cloudflare tunnels (also known as cloudflare argo) and manage internal tunnels ingresses. Due to the how cloudflare tunnels work, it's impossible to manage full cycle of cert creation and configuration inside container. You will need to install cloudflared utility to create and manage tunnels. After you installed it, login to cloudflare:

cloudflare login

It will ask you to choose zone you want to use for tunnels Then, create your tunnel:

cloudflared tunnel create k8s-tunnel-1

Notice unique ID of tunnel which will looks like this: 70ad5745-dbdd-427a-7c69-938a035a40c5. This is ID you will need to use for cloudflared.tunnel in helm chart variables. Name is just for easier identification of tunnel and is not used anywhere in config. Next, you will need to create kubernetes secret with credentials generated by cloudflared:

kubectl create secret generic tunnel-credentials --from-file=credentials.json=/Users/your_username/.cloudflared/<your_tunnel_id>.json -n your_k8s_namespace

In this case, secret will be stored with name "tunnel-credentials" which is also default in helm chart. Next, create DNS record pointing to your tunnel. You can either do that via Cloudflare portal, pointing hostname you need to cloudflare_tunnel_id.cfargotunnel.com or using cloudflared tool to point httpbin.example.com to k8s-tunnel-1:

cloudflared tunnel route dns k8s-tunnel-1 httpbin.example.org

Next step is to configure cloudflared inside k8s. Default config won't work because it requires your tunnel id in cloudflared.tunnel variable and some ingresses. Sample value block:

cloudflared:
  tunnel: 70ad5745-dbdd-427a-7c69-938a035a40c5
  ingress:
    - hostname: httpbin.example.org
      originRequest:
        noTLSVerify: true
      service: http://httpbin.default:443
    - hostname: hello.example.org
      service: hello_world
    - service: http_status:404
  serviceMonitor:
    enabled: true

This config will configure cloudflared to use tunnel with id 70ad5745-dbdd-427a-7c69-938a035a40c5 and start 3 ingresses:

  1. httpbin.example.org, pointing to service httpbin in namespace "default", also disabling TLS verify in case you have self-signed certificate
  2. hello.example.org, internal cloudflare service which acts as "hello world" to check that tunnel works and prints some debug info (host, headers, and etc). You will still need to create dns record pointing to that tunnel like we did with httpbin - there is no automatic DNS management in cloudflare
  3. 404 which will throw 404 pages in case you have DNS records pointing to that tunnel but no ingress configured with that hostname

Additionally, it will configure serviceMonitor for prometheus-operator. In case you do not use operator, and want to use annotations, metrics are on port 2000 of pod.

For more settings of ingresses please consult official cloudflare docs

Values

KeyTypeDefaultDescription
affinityobject{}affinity rules
cloudflared.argumentslist["tunnel","--config","/etc/cloudflared/config/config.yaml","run"]cloudflared command line arguments
cloudflared.ingresslist[{"hostname":"hello.example.com","service":"hello_world"},{"service":"http_status:404"}]Yaml wth ingress rules
cloudflared.serviceMonitor.enabledboolfalse
cloudflared.tunnelstringniltunnel UUID. Tunnel name will not work. Get it with 'cloudflared tunnel list'
cloudflared.tunnelSecretstring"tunnel-credentials"name of secret with stored tunnel credentials
extraEnvlist[]additional container environment variables
fullnameOverridestring""
image.pullPolicystring"IfNotPresent"
image.repositorystring"cloudflare/cloudflared"overrides default image
image.tagstring"2023.10.0"Overrides the image tag whose default is the chart appVersion.
imagePullSecretslist[]
nameOverridestring""
nodeSelectorobject{}node selector rules
podAnnotationsobject{}pod annotations
podDisruptionBudgetobject{"create":false,"maxUnavailable":"","minAvailable":1}Pod Disruption Budget configuration
podDisruptionBudget.createboolfalseSpecifies whether a PodDisruptionBudget should be created
podDisruptionBudget.maxUnavailablestring""Max number of pods that can be unavailable after the eviction
podDisruptionBudget.minAvailableint1Min number of pods that must still be available after the eviction
podSecurityContextobject{}pod security context
replicaCountint1Amount of replicas. Be aware that >1 replicas requires paid cloudflare loadbalancer subscription
resourcesobject{}pod limits/requests
securityContextobject{}security context
serviceAccount.annotationsobject{}Annotations to add to the service account
serviceAccount.createbooltrueSpecifies whether a service account should be created
serviceAccount.namestring""The name of the service account to use. If not set and create is true, a name is generated using the fullname template
tolerationslist[]tolerations rules

Upgrading

A major chart version change indicates that there is an incompatible breaking change needing manual actions.

Deploy cloudflare-tunnel with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cloudflare-tunnel on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcloudflare-tunnel is pinned at v0.8.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply