Files
infrastructure/servers/legion/k8s/media/gluetun-qbittorrent.yaml
T

154 lines
5.4 KiB
YAML

# gluetun + qBittorrent — all torrent traffic routed through ProtonVPN WireGuard
# gluetun runs as the VPN container; qBittorrent shares its network namespace (same pod)
apiVersion: apps/v1
kind: Deployment
metadata:
name: qbittorrent
namespace: media
labels:
app: qbittorrent
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: qbittorrent
template:
metadata:
labels:
app: qbittorrent
spec:
initContainers:
# Ensure /dev/net/tun exists on the node
- name: tun-setup
image: busybox:latest
command: ["sh", "-c", "mkdir -p /dev/net && [ -c /dev/net/tun ] || mknod /dev/net/tun c 10 200 && chmod 666 /dev/net/tun && sysctl -w net.ipv6.conf.all.disable_ipv6=1 || true && ip route flush table 51820 2>/dev/null || true && ip rule del priority 101 2>/dev/null || true && ip rule del table 51820 2>/dev/null || true"]
securityContext:
privileged: true
containers:
# VPN container — must come first so network is up before qBittorrent starts
- name: gluetun
image: ghcr.io/qdm12/gluetun:latest
securityContext:
capabilities:
add: ["NET_ADMIN"]
env:
# Use custom provider pinned to US-TX#457 — this specific server+key combo is confirmed working
- name: VPN_SERVICE_PROVIDER
value: "custom"
- name: VPN_TYPE
value: "wireguard"
- name: WIREGUARD_PRIVATE_KEY
valueFrom:
secretKeyRef:
name: media-secrets
key: PROTONVPN_PRIVATE_KEY
# US-TX#457 server — generated from ProtonVPN portal with NAT-PMP=on
- name: WIREGUARD_PUBLIC_KEY
value: "ZLiSI0SkdK5O0/fhweOpZ2c78F30gWHtsfZcVV0vlj8="
- name: WIREGUARD_ADDRESSES
value: "10.2.0.2/32"
- name: WIREGUARD_ENDPOINT_IP
value: "95.173.217.219"
- name: WIREGUARD_ENDPOINT_PORT
value: "51820"
# Allow cluster-internal traffic to bypass VPN (for Radarr/Sonarr → qBittorrent API)
- name: FIREWALL_OUTBOUND_SUBNETS
value: "10.42.0.0/16,10.43.0.0/16"
ports:
- containerPort: 8888 # gluetun HTTP proxy (unused but required)
resources:
requests:
memory: 128Mi
cpu: 50m
limits:
memory: 512Mi
cpu: 200m
# NAT-PMP helper — requests a forwarded port from ProtonVPN gateway every 45s
# and updates qBittorrent's listen port so peers can connect inbound
- name: natpmpc-helper
image: alpine:latest
command:
- sh
- -c
- |
apk add -q libnatpmp curl 2>/dev/null
echo "Waiting for VPN and qBittorrent to start..."
sleep 30
while true; do
# Request port forwarding from ProtonVPN NAT-PMP gateway
OUT=$(natpmpc -a 0 0 udp 60 -g 10.2.0.1 2>&1)
PORT=$(echo "$OUT" | grep -oE 'Mapped public port [0-9]+' | grep -oE '[0-9]+$')
if [ -n "$PORT" ]; then
echo "$(date): Got forwarded port $PORT"
# Also request TCP mapping for the same port
natpmpc -a "$PORT" "$PORT" tcp 60 -g 10.2.0.1 >/dev/null 2>&1
# Update qBittorrent listen port (form-encoded: json=<json_string>)
SID=$(curl -s -c /tmp/qbt.txt -X POST http://localhost:8080/api/v2/auth/login \
-d "username=admin&password=adminadmin" 2>/dev/null)
curl -s -b /tmp/qbt.txt -X POST http://localhost:8080/api/v2/app/setPreferences \
-d "json={\"listen_port\":$PORT,\"random_port\":false}" >/dev/null 2>&1
echo "$(date): qBittorrent listen port set to $PORT"
else
echo "$(date): NAT-PMP failed: $OUT"
fi
sleep 45
done
resources:
requests:
memory: 32Mi
cpu: 10m
limits:
memory: 64Mi
cpu: 50m
# qBittorrent — shares gluetun's network namespace, all traffic through VPN
- name: qbittorrent
image: lscr.io/linuxserver/qbittorrent:latest
env:
- name: PUID
value: "1000"
- name: PGID
value: "1000"
- name: TZ
value: "America/Chicago"
- name: WEBUI_PORT
value: "8080"
ports:
- containerPort: 8080
volumeMounts:
- name: config
mountPath: /config
- name: media
mountPath: /media
resources:
requests:
memory: 256Mi
cpu: 100m
limits:
memory: 1Gi
cpu: 500m
volumes:
- name: config
persistentVolumeClaim:
claimName: qbittorrent-config
- name: media
persistentVolumeClaim:
claimName: media-data
---
apiVersion: v1
kind: Service
metadata:
name: qbittorrent
namespace: media
spec:
selector:
app: qbittorrent
ports:
- name: webui
port: 8080
targetPort: 8080
type: ClusterIP