# Gives the cluster a route to the home LAN (192.168.8.0/24) through the # master's WireGuard tunnel (wg0) and SNATs it to the master's WG address, so # replies come back over the path the home router already knows. # # Hetzner's private network is routed (/32 per node) and drops packets whose # source is not a private address, so worker traffic cannot simply be routed # "via 10.1.0.3". Instead each worker gets a point-to-point GRE tunnel to the # master: the inner packet keeps its real source, the outer packet uses the # private node IPs. # # Prerequisite (host-side, see README.md): the master's wg0 peer must list # 192.168.8.0/24 in AllowedIPs. apiVersion: apps/v1 kind: DaemonSet metadata: name: home-lan-router namespace: kube-system labels: app: home-lan-router spec: selector: matchLabels: app: home-lan-router updateStrategy: type: RollingUpdate rollingUpdate: maxUnavailable: 10% template: metadata: labels: app: home-lan-router spec: hostNetwork: true dnsPolicy: ClusterFirstWithHostNet priorityClassName: system-node-critical tolerations: - operator: Exists containers: - name: router image: nicolaka/netshoot:v0.13 command: ["/bin/bash", "-c"] args: - | set -u HOME_LAN="192.168.8.0/24" MASTER_IP="10.1.0.3" WG_IF="wg0" TUN_NET="169.254.8.0/24" TUN_MTU=1396 MSS=1356 # Worker private IPs. Update this list when a node is added, # removed or renumbered. The master ignores its own address. WORKERS="10.1.0.2 10.1.0.4" is_master() { # The label is empty on workers; wg0 is a fallback in case the # label lookup comes back empty on the master. [ -n "${CONTROL_PLANE:-}" ] || ip link show "$WG_IF" >/dev/null 2>&1 } # /31 pair for a worker, derived from the last octet of its IP: # 10.1.0.2 -> 169.254.8.0/31, 10.1.0.4 -> 169.254.8.4/31. tun_net() { echo $(( ($1 - 2) * 2 )); } master_loop() { for w in $WORKERS; do local last dev net last="${w##*.}" net="$(tun_net "$last")" dev="home-gw-$net" if ! ip link show "$dev" >/dev/null 2>&1; then ip tunnel add "$dev" mode gre local "$MASTER_IP" remote "$w" ttl 255 fi ip link set "$dev" mtu "$TUN_MTU" up ip addr replace "169.254.8.$((net + 1))/31" dev "$dev" ip route replace "169.254.8.$net/31" dev "$dev" sysctl -qw "net.ipv4.conf.$dev.rp_filter=0" || true done sysctl -qw net.ipv4.conf.all.rp_filter=0 || true sysctl -qw net.ipv4.ip_forward=1 || true for src in "$TUN_NET" "10.0.0.0/8"; do iptables -t nat -C POSTROUTING -s "$src" -d "$HOME_LAN" -o "$WG_IF" -j MASQUERADE 2>/dev/null \ || iptables -t nat -A POSTROUTING -s "$src" -d "$HOME_LAN" -o "$WG_IF" -j MASQUERADE done iptables -C FORWARD -i 'home-gw+' -o "$WG_IF" -j ACCEPT 2>/dev/null \ || iptables -I FORWARD 1 -i 'home-gw+' -o "$WG_IF" -j ACCEPT iptables -C FORWARD -i "$WG_IF" -o 'home-gw+' -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 2>/dev/null \ || iptables -I FORWARD 1 -i "$WG_IF" -o 'home-gw+' -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT } worker_loop() { local local_ip last net dev local_ip="$(ip route get "$MASTER_IP" | sed -n 's/.* src \([^ ]*\).*/\1/p')" [ -n "$local_ip" ] || { echo "ERROR: cannot determine local IP"; return 1; } last="${local_ip##*.}" net="$(tun_net "$last")" dev="home-gw" if ! ip link show "$dev" >/dev/null 2>&1; then ip tunnel add "$dev" mode gre local "$local_ip" remote "$MASTER_IP" ttl 255 fi ip link set "$dev" mtu "$TUN_MTU" up ip addr replace "169.254.8.$net/31" dev "$dev" ip route replace "$HOME_LAN" via "169.254.8.$((net + 1))" dev "$dev" sysctl -qw "net.ipv4.conf.$dev.rp_filter=0" || true sysctl -qw net.ipv4.conf.all.rp_filter=0 || true # Let our SNAT rule own this destination, otherwise Cilium # masquerades to a tunnel device that has no address. iptables -t nat -C CILIUM_POST_nat -d "$HOME_LAN" -j RETURN 2>/dev/null \ || iptables -t nat -I CILIUM_POST_nat 1 -d "$HOME_LAN" -j RETURN iptables -t nat -C POSTROUTING -s "10.0.0.0/8" -d "$HOME_LAN" -j SNAT --to-source "169.254.8.$net" 2>/dev/null \ || iptables -t nat -A POSTROUTING -s "10.0.0.0/8" -d "$HOME_LAN" -j SNAT --to-source "169.254.8.$net" iptables -C FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 2>/dev/null \ || iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT iptables -t mangle -C FORWARD -o "$dev" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "$MSS" 2>/dev/null \ || iptables -t mangle -A FORWARD -o "$dev" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "$MSS" iptables -t mangle -C FORWARD -i "$dev" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "$MSS" 2>/dev/null \ || iptables -t mangle -A FORWARD -i "$dev" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss "$MSS" } echo "home-lan-router: node=${NODE_NAME:-unknown} role=$(is_master && echo master || echo worker)" while true; do if is_master; then master_loop else worker_loop fi sleep 60 done env: - name: NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName - name: CONTROL_PLANE valueFrom: fieldRef: fieldPath: "metadata.labels['node-role.kubernetes.io/control-plane']" securityContext: privileged: true resources: requests: cpu: 10m memory: 32Mi limits: memory: 64Mi