#!/bin/bash set -euo pipefail # Kubernetes Cluster Bootstrap Script # Supports RKE2 and k3s K8S_DISTRO="${K8S_DISTRO:-rke2}" K8S_VERSION="${K8S_VERSION:-latest}" NODE_TYPE="${NODE_TYPE:-server}" MASTER_NODES="${MASTER_NODES:-}" TOKEN="${TOKEN:-}" log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >&2 } error() { log "ERROR: $*" exit 1 } install_rke2() { log "Installing RKE2 ${K8S_VERSION}..." # Install RKE2 curl -sfL https://get.rke2.io | INSTALL_RKE2_VERSION="${K8S_VERSION}" sh - # Configure RKE2 mkdir -p /etc/rancher/rke2 if [ "${NODE_TYPE}" = "server" ]; then cat > /etc/rancher/rke2/config.yaml < /etc/rancher/rke2/config.yaml < /etc/sysctl.d/99-kubernetes-cri.conf < /dev/null; then apt-get update apt-get install -y curl wget git jq elif command -v yum &> /dev/null; then yum install -y curl wget git jq fi } install_network_plugin() { log "Installing network plugin (Cilium)..." kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/1.14.0/install/kubernetes/quick-install.yaml log "Waiting for Cilium to be ready..." kubectl wait --for=condition=ready pod -l k8s-app=cilium -n kube-system --timeout=300s } install_storage_class() { log "Installing local-path storage class..." kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.24/deploy/local-path-storage.yaml # Set as default kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' } main() { log "Starting Kubernetes cluster bootstrap..." setup_system case "${K8S_DISTRO}" in rke2) install_rke2 ;; k3s) install_k3s ;; *) error "Unsupported Kubernetes distribution: ${K8S_DISTRO}" ;; esac if [ "${NODE_TYPE}" = "server" ]; then install_network_plugin install_storage_class log "Kubernetes cluster bootstrap completed!" log "Kubeconfig location: ~/.kube/config" kubectl get nodes else log "Agent node setup completed!" fi } main "$@"