#!/bin/bash # Script to properly join node3 to existing K3s etcd cluster # Run this script ON node3 set -e echo "=== Adding node3 to existing K3s etcd cluster ===" # Step 1: Stop K3s and clean up echo "Stopping K3s and cleaning up..." sudo systemctl stop k3s || true sudo systemctl stop k3s-agent || true # Remove server data to prevent conflicts echo "Cleaning up previous server data..." sudo rm -rf /var/lib/rancher/k3s/server # Step 2: Get the token from an existing server echo "" echo "You need to get the cluster token from node1 or node2." echo "On node1 or node2, run:" echo " sudo cat /var/lib/rancher/k3s/server/token" echo "" read -p "Enter the K3s cluster token: " K3S_TOKEN # Step 3: Create proper K3s configuration for joining existing cluster echo "Creating K3s configuration..." sudo mkdir -p /etc/rancher/k3s # Important: We need to specify this is joining an existing cluster cat << EOF | sudo tee /etc/rancher/k3s/config.yaml # Join existing cluster - do not initialize new one server: "https://10.0.101.21:6443" token: "${K3S_TOKEN}" # Additional configuration tls-san: - "10.0.101.23" - "node3" node-ip: "10.0.101.23" EOF # Step 4: Uninstall K3s agent if it exists if [ -f /usr/local/bin/k3s-agent-uninstall.sh ]; then echo "Uninstalling K3s agent..." sudo /usr/local/bin/k3s-agent-uninstall.sh fi # Step 5: Install K3s as server joining the existing cluster echo "Installing K3s as server (joining existing cluster)..." curl -sfL https://get.k3s.io | sh -s - server \ --server https://10.0.101.21:6443 \ --token "${K3S_TOKEN}" # Step 6: Wait for services to start echo "Waiting for K3s to start..." sleep 20 # Step 7: Check status echo "Checking K3s status..." sudo systemctl status k3s --no-pager || true sudo journalctl -u k3s -n 50 --no-pager echo "" echo "=== Setup complete ===" echo "If you see errors about etcd already being initialized, that's expected." echo "Check from your local machine with:" echo " kubectl get nodes" echo "" echo "The node should now show roles: control-plane,etcd,master" echo "" echo "To check etcd cluster members from node1:" echo " sudo k3s etcdctl member list"