Use Cloudflare Tunnels with Kubernetes client-go plugin
This tutorial explains how to use Cloudflare Tunnels with Kubernetes client-go credential plugins for authentication. By following these steps, you can securely access your Kubernetes cluster through a Cloudflare Tunnel using the kubectl command-line tool.
- A Cloudflare account
- The Cloudflare Tunnel client (
cloudflared) installed on your machine - Access to a Kubernetes cluster
kubectlinstalled on your machine
-
Authenticate
cloudflaredwith your Cloudflare account:Terminal window cloudflared tunnel login -
Create a new tunnel:
Terminal window cloudflared tunnel create k8s-tunnel -
Configure your tunnel by creating a configuration file named
config.yml:tunnel: <TUNNEL_ID>credentials-file: /path/to/credentials.jsoningress:- hostname: k8s.example.comservice: tcp://kubernetes.default.svc.cluster.local:443- service: http_status:404Replace
<TUNNEL_ID>with your tunnel ID and adjust the hostname as needed. -
Start the tunnel:
Terminal window cloudflared tunnel run k8s-tunnel
Ensure your Kubernetes API server is configured to accept authentication from Cloudflare Tunnels. This may involve setting up an authentication webhook or configuring the API server to trust the Cloudflare Tunnel's client certificates.
-
Create a script named
cloudflare-k8s-auth.shwith the following content:#!/bin/bashecho '{"apiVersion": "client.authentication.k8s.io/v1beta1","kind": "ExecCredential","status": {"token": "'"$(cloudflared access token -app=https://k8s.example.com)"'"}}'Make the script executable:
Terminal window chmod +x cloudflare-k8s-auth.sh -
Update your
~/.kube/configfile to use the credential plugin:apiVersion: v1kind: Configclusters:- cluster:server: https://k8s.example.comname: cloudflare-k8susers:- name: cloudflare-useruser:exec:apiVersion: client.authentication.k8s.io/v1beta1command: /path/to/cloudflare-k8s-auth.shinteractiveMode: Nevercontexts:- context:cluster: cloudflare-k8suser: cloudflare-username: cloudflare-k8s-contextcurrent-context: cloudflare-k8s-context
Now you can use kubectl commands as usual. The client-go credential plugin will automatically handle authentication through the Cloudflare Tunnel:
kubectl get podsIf you encounter issues:
- Ensure
cloudflaredis running and the tunnel is active - Check that your
~/.kube/configfile is correctly configured - Verify that the Kubernetes API server is properly set up to accept authentication from Cloudflare Tunnels
- Review the Cloudflare Tunnel logs for any error messages
For more information, refer to the Cloudflare Tunnels documentation ↗ and the Kubernetes client-go credential plugins documentation ↗.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark