Use cloudflared to expose a Kubernetes app to the Internet
You can use Cloudflare Tunnel to connect applications and servers to Cloudflare’s network. Tunnel relies on a piece of software, cloudflared ↗, to create those connections.
The same Tunnel can be run from multiple instances of cloudflared
, giving you the ability to run many cloudflared
replicas to scale your system when incoming traffic changes.
In this tutorial, we will walk through running an application as a Kubernetes Service ↗, and then running cloudflared
in a separate Deployment ↗.
This architecture allows cloudflared
instances to proxy Internet traffic into whichever Kubernetes Service it was configured to.
This tutorial covers how to:
- Deploy
cloudflared
in a replica model - Proxy traffic into a Kubernetes service with Tunnel
Time to complete: 45 minutes
Start by downloading and installing the lightweight Cloudflare Tunnel daemon, cloudflared
. Reference our installation guide for instructions on how to install cloudflared
on your operating system.
Once installed, you can use the tunnel login
command in cloudflared
to obtain a certificate.
In the example below, simply change <example-tunnel>
to the name you wish to assign to your Tunnel.
Next, you will upload the generated Tunnel credential file as a secret to your Kubernetes cluster. You will also need to provide the filepath that the Tunnel credentials file was created under. You can find that path in the output of cloudflared tunnel create <example-tunnel>
above.
- Go to the Cloudflare dashboard.
- Go to the DNS tab.
- Now create a CNAME targeting
.cfargotunnel.com
. In this example, the tunnel ID is ef824aef-7557-4b41-a398-4684585177ad, so create a CNAME record specifically targetingef824aef-7557-4b41-a398-4684585177ad.cfargotunnel.com
.
You can also create multiple CNAME records targeting the same Tunnel, if desired.
Alternatively, you can perform this step from the command line by running cloudflared tunnel route dns <tunnel> <hostname>
. For example, cloudflared tunnel route dns example-tunnel tunnel.example.com
. You can use a similar method to route traffic to cloudflared
from a Cloudflare Load Balancer ↗, see docs for details.
Now, we’ll deploy cloudflared
by applying its manifest ↗. This will start a Deployment ↗ for running cloudflared
and a ConfigMap ↗ with cloudflared
’s config. When Cloudflare receives traffic for the DNS or Load Balancing hostname you configured in the previous step, it will send that traffic to the cloudflared
instances running in this deployment. Then, those cloudflared
instances will proxy the request to your application’s Service ↗.
At this point, you’ll see the httpbin welcome page.
In this tutorial, we’ve covered how the same Tunnel can be run in many cloudflared
processes. You can also use this knowledge to support elastic scaling, graceful cloudflared
restarts, and rolling upgrades in the future.
We love to hear your feedback! Join the discussion in our community ↗.