Files
netboot/files/k3s-join
Torbjørn Lindahl 492cc8abbc Add K3s agent setup with NVMe-backed persistent storage
Bind-mount K3s agent data, node identity, and kubelet dirs from
NVMe so container image cache and node registration survive reboots
on the diskless netboot nodes. Includes K3s binary download, agent
systemd service, DHCP hostname resolution, and open-iscsi for
Longhorn iSCSI support.
2026-03-01 19:11:12 +01:00

79 lines
2.0 KiB
Bash

#!/bin/bash
# K3s agent join script for netboot nodes
# Fetches token from server and starts k3s agent
#
# Runs at boot via k3s-join.service
set -euo pipefail
K3S_SERVER="192.168.100.1"
K3S_URL="https://${K3S_SERVER}:6443"
TOKEN_URL="http://${K3S_SERVER}:8800/k3s-token"
MAX_RETRIES=30
RETRY_DELAY=10
# Colors for console output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${GREEN}[k3s-join]${NC} $*"; logger -t k3s-join "$*"; }
warn() { echo -e "${YELLOW}[k3s-join]${NC} $*"; logger -t k3s-join -p warning "$*"; }
error() { echo -e "${RED}[k3s-join]${NC} $*"; logger -t k3s-join -p err "$*"; }
# Wait for network to be ready
wait_for_network() {
local count=0
while ! ping -c1 -W1 "$K3S_SERVER" &>/dev/null; do
count=$((count + 1))
if [ $count -ge $MAX_RETRIES ]; then
error "Network not available after $MAX_RETRIES attempts"
return 1
fi
warn "Waiting for network... ($count/$MAX_RETRIES)"
sleep $RETRY_DELAY
done
log "Network is up"
}
# Fetch join token from server
fetch_token() {
local count=0
local token=""
while [ -z "$token" ]; do
token=$(curl -sf "$TOKEN_URL" 2>/dev/null || true)
if [ -z "$token" ]; then
count=$((count + 1))
if [ $count -ge $MAX_RETRIES ]; then
error "Failed to fetch token after $MAX_RETRIES attempts"
return 1
fi
warn "Waiting for token... ($count/$MAX_RETRIES)"
sleep $RETRY_DELAY
fi
done
echo "$token"
}
# Main
log "Starting K3s agent join process"
wait_for_network
log "Fetching join token from $TOKEN_URL"
K3S_TOKEN=$(fetch_token)
if [ -z "$K3S_TOKEN" ]; then
error "Failed to get token, exiting"
exit 1
fi
log "Token acquired"
log "Starting K3s agent (server: $K3S_URL)"
exec /usr/local/bin/k3s agent \
--server="$K3S_URL" \
--token="$K3S_TOKEN" \
--node-name="$(hostname)"