prole/k3s/install_k3s_first_server.sh
chrisfu 646745b4b0 chore(k3s): script and hostprobe updates, temp maintenance scripts
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 14:47:27 -07:00

47 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# Bootstrap the first K3s server (embedded etcd) on raspberry.knoe.org using k3sup.
# This script is idempotent: re-running will upgrade/ensure k3s is present.
# Requirements:
# - k3sup installed locally (https://github.com/alexellis/k3sup)
# - SSH access to the target host
# - The target host must have ports 6443/tcp, 10250/tcp open locally
set -euo pipefail
# Configurable via env vars; provide sensible defaults
: "${K3S_FIRST_SERVER_HOST:=raspberry.prole.org}"
: "${K3S_FIRST_SERVER_USER:=pi}"
: "${K3S_SSH_KEY:=$HOME/.ssh/id_rsa}"
: "${K3S_VERSION:=}"
# Extra args to pass directly to k3s server
K3S_EXTRA_ARGS_DEFAULT="--cluster-init"
: "${K3S_EXTRA_ARGS:=${K3S_EXTRA_ARGS_DEFAULT}}"
echo "[info] Installing first K3s server on ${K3S_FIRST_SERVER_HOST} (user=${K3S_FIRST_SERVER_USER})"
if ! command -v k3sup >/dev/null 2>&1; then
echo "[error] k3sup is not installed. See https://github.com/alexellis/k3sup" >&2
exit 1
fi
K3SUP_ARGS=(install \
--ip "${K3S_FIRST_SERVER_HOST}" \
--user "${K3S_FIRST_SERVER_USER}" \
--ssh-key "${K3S_SSH_KEY}" \
--k3s-extra-args "${K3S_EXTRA_ARGS}"
)
if [[ -n "${K3S_VERSION}" ]]; then
K3SUP_ARGS+=(--k3s-version "${K3S_VERSION}")
fi
# Create/merge kubeconfig in ~/.kube/config with context named after host
K3SUP_ARGS+=(--local-path "$HOME/.kube/config" --context "k3s-${K3S_FIRST_SERVER_HOST}")
set -x
k3sup "${K3SUP_ARGS[@]}"
set +x
echo "[ok] First K3s server installed with embedded etcd and context 'k3s-${K3S_FIRST_SERVER_HOST}'"