Repository navigation
Expand file tree
/
Copy path05-deploy-cloudflared.sh
More file actions
executable file
·93 lines (81 loc) · 2.6 KB
/
Copy path05-deploy-cloudflared.sh
File metadata and controls
executable file
·93 lines (81 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
# ==============================================================================
# Step 05: Deploy Cloudflare Tunnel Agent (Zero-Trust Ingress)
# ==============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=scripts/common.sh
source "${SCRIPT_DIR}/common.sh"
check_root
CF_TOKEN="${CLOUDFLARE_TUNNEL_TOKEN:-${CF_TOKEN:-}}"
if [[ -z "${CF_TOKEN}" ]]; then
warn "No Cloudflare Tunnel Token provided (CLOUDFLARE_TUNNEL_TOKEN or CF_TOKEN)."
warn "Skipping automatic Cloudflare Tunnel deployment."
warn "You can deploy it later by providing the token to this script."
exit 0
fi
if ! command -v kubectl &> /dev/null; then
fatal "kubectl command not found. Ensure K3s control-plane is installed before running this script."
fi
info "Starting Step 05: Deploying Cloudflare Tunnel into Kubernetes..."
# 1. Create namespace
kubectl create namespace cloudflare --dry-run=client -o yaml | kubectl apply -f -
# 2. Create Secret with Tunnel Token
kubectl create secret generic cloudflared-token \
--namespace cloudflare \
--from-literal=token="${CF_TOKEN}" \
--dry-run=client -o yaml | kubectl apply -f -
# 3. Apply Cloudflared Deployment
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: cloudflared
namespace: cloudflare
labels:
app: cloudflared
spec:
replicas: 2
selector:
matchLabels:
app: cloudflared
template:
metadata:
labels:
app: cloudflared
spec:
containers:
- name: cloudflared
image: cloudflare/cloudflared:latest
args:
- tunnel
- --no-autoupdate
- run
- --token
- \$(TUNNEL_TOKEN)
env:
- name: TUNNEL_TOKEN
valueFrom:
secretKeyRef:
name: cloudflared-token
key: token
resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 50m
memory: 64Mi
securityContext:
runAsNonRoot: true
runAsUser: 65532
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
EOF
info "Waiting for cloudflared deployment rollout..."
kubectl rollout status deployment/cloudflared -n cloudflare --timeout=60s || true
success "Step 05 completed: Cloudflare Tunnel deployed in 'cloudflare' namespace!"
info "Cloudflare Tunnel is now securely forwarding traffic to internal cluster services without open ports."