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
kubectl
installed on your machine
-
Authenticate
cloudflared
with your Cloudflare account: -
Create a new tunnel:
-
Configure your tunnel by creating a configuration file named
config.yml
:Replace
<TUNNEL_ID>
with your tunnel ID and adjust the hostname as needed. -
Start the 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.sh
with the following content:Make the script executable:
-
Update your
~/.kube/config
file to use the credential plugin:
Now you can use kubectl
commands as usual. The client-go credential plugin will automatically handle authentication through the Cloudflare Tunnel:
If you encounter issues:
- Ensure
cloudflared
is running and the tunnel is active - Check that your
~/.kube/config
file 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 ↗.