Skip to content
SumGuy's Ramblings
Go back

How to securely deploy Cloudflare Tunnels

In the current digital landscape, securely exposing local applications to the internet is one of the paramount challenges for developers and system administrators. This is where Cloudflare Tunnels, formerly known as Argo Tunnel, steps in. This service bypasses traditional methods that often involve complex VPN setups or risky exposure of local ports to the world. Whether you’re a seasoned developer or an IT enthusiast dabbling in web technologies, understanding how to leverage Cloudflare Tunnels is crucial.

What is Cloudflare Tunnel?

Cloudflare Tunnel allows you to expose your web services and applications to the internet securely, without opening inbound ports. Instead, it establishes an outbound connection (a “tunnel”) from your local environment to Cloudflare’s edge network. This setup not only ensures your applications are secure by default (since your local network is never exposed directly) but also integrates seamlessly with Cloudflare’s performance and security features.

Key Benefits of Cloudflare Tunnel

Installing and Using Cloudflare Tunnel

Prerequisites

Installation Steps

Step 1: Install the cloudflared Tool

First, you need to install the cloudflared daemon, which is responsible for creating and managing your tunnels. Installation instructions differ slightly depending on your operating system.

curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb && sudo dpkg -i cloudflared.deb
curl -L --output cloudflared.rpm https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-x86_64.rpm && sudo yum localinstall -y cloudflared.rpm

Docker compose

services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    container_name: cloudflared
    restart: unless-stopped
    command: tunnel run TUNNELNAME
    environment:
      - TUNNEL_TOKEN=aaaaabbbbccccddddd
    networks:
      - cloudflare
      
networks:
  cloudflare:
    external: true

Make sure to change TUNNEL_TOKEN above along with TUNNELNAME in the command. replace TUNNELNAME with the name you picked in #4 below

run the following command:

docker network create cloudflared
docker compose up -d && docker compose logs -f

using docker you can now simply add other containers to your Cloudflare network for them to be visible to Cloudflare tunnel. This enhances security since if a service or container isn’t in the Cloudflare network in docker it won’t be visible to Cloudflare. Add the below parts to your existing docker-compose.yml to add a service to Cloudflare network, this example below shows a caddy docker container being added to the Cloudflare network. lines 9-14 were added to this example.

services:
  caddy:
    image: caddy:latest
    container_name: caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    networks:
      - cloudflare
      
networks:
  cloudflare:
    external: true

Setup & Management

Launch

After installation, and setup is complete on the cloduflare panel, start cloudflared via following commands:

Linux & Mac:

sudo cloudflared service install <tunnel token>

Windows:

cloudflared.exe service install <tunnel token>

Practical Use Cases

Conclusion

Cloudflare Tunnel offers a potent combination of security, performance, and ease of use for exposing internet-facing services. By following the above guidelines, users can securely and efficiently expose any local web service, leveraging Cloudflare’s robust network capabilities. Whether for development, personal, or production environments, Cloudflare Tunnel is a versatile tool in the modern web developer’s toolkit.

Further Learning and Support

For more detailed configurations and troubleshooting, visit the Cloudflare documentationCloudflare documentation and community forums, which offer a wealth of information and community-driven support.


Share this post on:

Previous Post
Optimizing Ansible for Faster Playbook Execution
Next Post
Uptime Monitoring with Uptime Kuma