Skip to content

Commit 62aa3bc

Browse files
crwaters16harshil1712
authored andcommitted
adding tutorial (#17285)
1 parent 2233120 commit 62aa3bc

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
updated: 2024-10-02
3+
category: 🔐 Zero Trust
4+
pcx_content_type: tutorial
5+
title: Use Cloudflare Tunnels with Kubernetes client-go plugin
6+
---
7+
8+
# Use Cloudflare Tunnels with Kubernetes client-go credential plugins
9+
10+
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.
11+
12+
## Prerequisites
13+
14+
- A Cloudflare account
15+
- The Cloudflare Tunnel client (`cloudflared`) installed on your machine
16+
- Access to a Kubernetes cluster
17+
- `kubectl` installed on your machine
18+
19+
## 1. Set up a Cloudflare Tunnel
20+
21+
1. Authenticate `cloudflared` with your Cloudflare account:
22+
23+
```sh
24+
cloudflared tunnel login
25+
```
26+
27+
2. Create a new tunnel:
28+
29+
```sh
30+
cloudflared tunnel create k8s-tunnel
31+
```
32+
33+
3. Configure your tunnel by creating a configuration file named `config.yml`:
34+
35+
```yaml
36+
tunnel: <TUNNEL_ID>
37+
credentials-file: /path/to/credentials.json
38+
ingress:
39+
- hostname: k8s.example.com
40+
service: tcp://kubernetes.default.svc.cluster.local:443
41+
- service: http_status:404
42+
```
43+
44+
Replace `<TUNNEL_ID>` with your tunnel ID and adjust the hostname as needed.
45+
46+
4. Start the tunnel:
47+
48+
```sh
49+
cloudflared tunnel run k8s-tunnel
50+
```
51+
52+
## 2. Configure the Kubernetes API server
53+
54+
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.
55+
56+
## 3. Set up client-go credential plugin
57+
58+
1. Create a script named `cloudflare-k8s-auth.sh` with the following content:
59+
60+
```bash
61+
#!/bin/bash
62+
63+
echo '{
64+
"apiVersion": "client.authentication.k8s.io/v1beta1",
65+
"kind": "ExecCredential",
66+
"status": {
67+
"token": "'"$(cloudflared access token -app=https://k8s.example.com)"'"
68+
}
69+
}'
70+
```
71+
72+
Make the script executable:
73+
74+
```sh
75+
chmod +x cloudflare-k8s-auth.sh
76+
```
77+
78+
2. Update your `~/.kube/config` file to use the credential plugin:
79+
80+
```yaml
81+
apiVersion: v1
82+
kind: Config
83+
clusters:
84+
- cluster:
85+
server: https://k8s.example.com
86+
name: cloudflare-k8s
87+
users:
88+
- name: cloudflare-user
89+
user:
90+
exec:
91+
apiVersion: client.authentication.k8s.io/v1beta1
92+
command: /path/to/cloudflare-k8s-auth.sh
93+
interactiveMode: Never
94+
contexts:
95+
- context:
96+
cluster: cloudflare-k8s
97+
user: cloudflare-user
98+
name: cloudflare-k8s-context
99+
current-context: cloudflare-k8s-context
100+
```
101+
102+
## 4. Use kubectl with Cloudflare Tunnel
103+
104+
Now you can use `kubectl` commands as usual. The client-go credential plugin will automatically handle authentication through the Cloudflare Tunnel:
105+
106+
```sh
107+
kubectl get pods
108+
```
109+
110+
## Troubleshooting
111+
112+
If you encounter issues:
113+
114+
- Ensure `cloudflared` is running and the tunnel is active
115+
- Check that your `~/.kube/config` file is correctly configured
116+
- Verify that the Kubernetes API server is properly set up to accept authentication from Cloudflare Tunnels
117+
- Review the Cloudflare Tunnel logs for any error messages
118+
119+
For more information, refer to the [Cloudflare Tunnels documentation](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/) and the [Kubernetes client-go credential plugins documentation](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins).

0 commit comments

Comments
 (0)