Skip to content

Headscale server on k3s

Self-hosted Headscale server on k3s — v0.28.0

← Back to Index


Prerequisites

Requirement Details
k3s cluster Running with at least 1 worker node
cert-manager Already installed with letsencrypt-prod ClusterIssuer
APISIX Running in ingress-apisix namespace
DNS headscale.castis.io A record pointing to APISIX external IP
Storage local-path or longhorn-db storage class available

Verify prerequisites

# Check k3s nodes
kubectl get nodes -o wide

# Check cert-manager
kubectl get pods -n cert-manager
kubectl get clusterissuer

# Check APISIX external IP
kubectl get svc -n ingress-apisix | grep apisix-gateway

# Check DNS resolves
nslookup headscale.castis.io

Architecture Overview

Internet
    ↓
headscale.castis.io (DNS A record)
    ↓
APISIX Gateway (LoadBalancer — external IP)
    ↓
ApisixRoute + ApisixTls (TLS via cert-manager)
    ↓
headscale Service (namespace: headscale)
    ↓
headscale-0 Pod (StatefulSet on worker node)
    ↓
PVC — local-path or longhorn-db (1Gi)

Node Preparation

Label the worker node

# Verify worker label exists
kubectl get node <worker-node-name> \
  -o jsonpath='{.metadata.labels.node-role\.kubernetes\.io/worker}'
echo ""
# Should output: worker

# If label is missing, add it
kubectl label node <worker-node-name> \
  node-role.kubernetes.io/worker=worker --overwrite

Create namespace

kubectl create namespace headscale
kubectl get namespace headscale

Storage Setup

Recommended: local-path for simplicity. Use longhorn-db only if Longhorn disk pressure is resolved.

Check available storage classes

kubectl get storageclass

If using longhorn-db — verify it's schedulable

kubectl get nodes.longhorn.io -n longhorn-system -o yaml | \
  grep -A 5 "type: Schedulable"
# All nodes must show status: "True"

If Longhorn has DiskPressure — reduce storage reservation

# Edit each node and reduce storageReserved to 5GB
kubectl edit nodes.longhorn.io <node-name> -n longhorn-system
# Change: storageReserved: 5000000000

Deploy Headscale

Create working directory

mkdir -p ~/headscale && cd ~/headscale

File 1 — configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: headscale-config
  namespace: headscale
data:
  config.yaml: |
    server_url: https://headscale.castis.io
    listen_addr: 0.0.0.0:8080
    metrics_listen_addr: 0.0.0.0:9090
    grpc_listen_addr: 0.0.0.0:50443
    private_key_path: /var/lib/headscale/private.key
    noise:
      private_key_path: /var/lib/headscale/noise_private.key
    prefixes:
      v4: <TAILNET_IPV4_PREFIX>
      v6: <TAILNET_IPV6_PREFIX>
    derp:
      urls:
        - https://controlplane.tailscale.com/derpmap/default
      auto_update_enabled: true
      update_frequency: 24h
    database:
      type: sqlite
      sqlite:
        path: /var/lib/headscale/db.sqlite
    dns:
      magic_dns: true
      base_domain: ts.castis.io
      override_local_dns: false
    log:
      level: info

Important: base_domain must be different from server_url domain. server_url uses headscale.castis.io so base_domain is ts.castis.io

File 2 — statefulset.yaml

apiVersion: v1
kind: Service
metadata:
  name: headscale
  namespace: headscale
spec:
  selector:
    app: headscale
  ports:
    - name: http
      port: 80
      targetPort: 8080
    - name: metrics
      port: 9090
      targetPort: 9090
    - name: grpc
      port: 50443
      targetPort: 50443
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: headscale
  namespace: headscale
spec:
  selector:
    matchLabels:
      app: headscale
  serviceName: headscale
  replicas: 1
  template:
    metadata:
      labels:
        app: headscale
    spec:
      nodeSelector:
        node-role.kubernetes.io/worker: worker
      containers:
        - name: headscale
          image: headscale/headscale:v0.28.0
          args: ["serve"]
          ports:
            - containerPort: 8080
              name: http
            - containerPort: 9090
              name: metrics
            - containerPort: 50443
              name: grpc
          volumeMounts:
            - name: config
              mountPath: /etc/headscale
            - name: data
              mountPath: /var/lib/headscale
          livenessProbe:
            httpGet:
              path: /
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: config
          configMap:
            name: headscale-config
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: local-path    # or longhorn-db if Longhorn is healthy
        resources:
          requests:
            storage: 1Gi

File 3 — certificate.yaml

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: headscale-cert
  namespace: headscale
spec:
  secretName: headscale-cert
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
    - headscale.castis.io

File 4 — apisixtls.yaml

apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: headscale-tls
  namespace: headscale
spec:
  hosts:
    - headscale.castis.io
  secret:
    name: headscale-cert
    namespace: headscale

File 5 — apisixroute.yaml

apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  name: headscale-routes
  namespace: headscale
spec:
  http:
    - name: headscale-route
      match:
        hosts:
          - headscale.castis.io
        paths:
          - /*
      backends:
        - serviceName: headscale
          servicePort: 80
          resolveGranularity: service
      websocket: true    # required for Tailscale TS2021 protocol

Important: websocket: true is required. Without it Tailscale clients cannot connect.

Apply in order

# 1. ConfigMap
kubectl apply -f configmap.yaml
kubectl get configmap -n headscale

# 2. StatefulSet + Service
kubectl apply -f statefulset.yaml
kubectl get pods -n headscale -w
# Wait until: headscale-0  1/1  Running

TLS Certificate

# Apply certificate
kubectl apply -f certificate.yaml

# Watch until READY = True (1-2 minutes)
kubectl get certificate -n headscale -w

APISIX Routing

# Apply TLS
kubectl apply -f apisixtls.yaml
kubectl get apisixtls -n headscale

# Apply Route
kubectl apply -f apisixroute.yaml
kubectl get apisixroute -n headscale

Verify everything

echo "=== Pods ===" && kubectl get pods -n headscale -o wide
echo "=== PVC ===" && kubectl get pvc -n headscale
echo "=== Certificate ===" && kubectl get certificate -n headscale
echo "=== ApisixTls ===" && kubectl get apisixtls -n headscale
echo "=== ApisixRoute ===" && kubectl get apisixroute -n headscale
echo "=== Health ===" && curl https://headscale.castis.io/health

Expected health check response:

{"status":"pass"}


Post-Install Setup

Create a user

# Create user
kubectl exec -n headscale headscale-0 -- headscale users create <username>

# List users to get user ID
kubectl exec -n headscale headscale-0 -- headscale users list

Generate preauth key

# Use user ID number from users list (not username)
kubectl exec -n headscale headscale-0 -- \
  headscale preauthkeys create --user <user-id> --reusable --expiration 24h

Note: In v0.28.0 --user flag requires the numeric ID, not the username.

List connected nodes

kubectl exec -n headscale headscale-0 -- headscale nodes list

Check which device belongs to which user

kubectl exec -n headscale headscale-0 -- headscale nodes list
# USER column shows which user each device belongs to

Move a node to a different user

kubectl exec -n headscale headscale-0 -- \
  headscale nodes move --identifier <node-id> --user <user-id>

Subnet Routing

Allows devices in a private subnet to be reachable through Tailscale without installing Tailscale on each device.

Architecture

Your Laptop (Tailscale + accept-routes)
         ↓
    Headscale Server
         ↓
Subnet Router Node (Tailscale + advertise-routes)
         ↓
Private Subnet <OFFICE_LAN_CIDR>
├── nuc-a  (<NUC_A_LAN_IP>) — no Tailscale needed
└── other devices (<OFFICE_LAN_HOST_IP>) — no Tailscale needed

Who needs what

Device Install Tailscale advertise-routes accept-routes IP Forwarding
Subnet Router Node (nuc-b) Yes Yes Yes Yes
Your Laptop / Other nodes Yes No Yes No
Devices in private subnet No No No No

Step 1 — Enable IP forwarding on subnet router node (nuc-b)

echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# Verify
sysctl net.ipv4.ip_forward
# Should show: net.ipv4.ip_forward = 1

Step 2 — Connect subnet router with route advertising

sudo tailscale up \
  --login-server https://headscale.castis.io \
  --authkey <preauth-key> \
  --advertise-routes=<OFFICE_LAN_CIDR> \
  --accept-routes

Step 3 — Approve the route in Headscale

# Check node ID and routes
kubectl exec -n headscale headscale-0 -- headscale nodes list
kubectl exec -n headscale headscale-0 -- headscale nodes list-routes

# Approve route (use -i for node identifier)
kubectl exec -n headscale headscale-0 -- \
  headscale nodes approve-routes -i <node-id> -r <OFFICE_LAN_CIDR>

# Verify approved
kubectl exec -n headscale headscale-0 -- headscale nodes list-routes
# Approved column should show <OFFICE_LAN_CIDR>

Step 4 — Remove a route

# Pass empty string to remove all approved routes
kubectl exec -n headscale headscale-0 -- \
  headscale nodes approve-routes -i <node-id> -r ""

Troubleshooting

Pod stuck in ContainerCreating

kubectl describe pod headscale-0 -n headscale | tail -20
kubectl get events -n headscale --sort-by='.lastTimestamp'

Longhorn DiskPressure

# Check disk status
kubectl get nodes.longhorn.io -n longhorn-system -o yaml | \
  grep -A 10 "storageAvailable\|Schedulable"

# Fix by reducing storageReserved
kubectl edit nodes.longhorn.io <node-name> -n longhorn-system
# Set storageReserved: 5000000000

Tailscale client gets 500 error

kubectl logs -n headscale headscale-0 --tail=20
# Check for WebSocket warning — fix by adding websocket: true to ApisixRoute

WebSocket not passing through APISIX

# Symptom in logs:
# No Upgrade header in TS2021 request

# Fix: ensure websocket: true is in apisixroute.yaml
kubectl edit apisixroute headscale-routes -n headscale

Check all resources status

echo "=== Node label ===" && kubectl get nodes --show-labels | grep worker
echo "=== Pods ===" && kubectl get pods -n headscale -o wide
echo "=== PVC ===" && kubectl get pvc -n headscale
echo "=== Certificate ===" && kubectl get certificate -n headscale
echo "=== ApisixTls ===" && kubectl get apisixtls -n headscale
echo "=== ApisixRoute ===" && kubectl get apisixroute -n headscale
echo "=== Health ===" && curl https://headscale.castis.io/health
echo "=== Nodes ===" && kubectl exec -n headscale headscale-0 -- headscale nodes list

Useful Headscale CLI commands (v0.28.0)

# Users
kubectl exec -n headscale headscale-0 -- headscale users list
kubectl exec -n headscale headscale-0 -- headscale users create <name>

# Nodes
kubectl exec -n headscale headscale-0 -- headscale nodes list
kubectl exec -n headscale headscale-0 -- headscale nodes list-routes

# Preauth keys (use numeric user ID not username)
kubectl exec -n headscale headscale-0 -- \
  headscale preauthkeys create --user <id> --reusable --expiration 24h
kubectl exec -n headscale headscale-0 -- \
  headscale preauthkeys list --user <id>

# Routes
kubectl exec -n headscale headscale-0 -- \
  headscale nodes approve-routes -i <node-id> -r <subnet>

# Health
kubectl exec -n headscale headscale-0 -- headscale health

Key Notes for v0.28.0

Change Old New
Database config db_type + db_path database.type + database.sqlite.path
IP prefixes ip_prefixes prefixes.v4 + prefixes.v6
DNS config dns_config dns
User flag --user username --user <numeric-id>
Routes command headscale routes list headscale nodes list-routes
Route approve headscale routes enable -r <id> headscale nodes approve-routes -i <node-id> -r <subnet>
DERP Required — must configure at least one Use Tailscale public DERP map