#!/bin/bash # AiWorker K3s HA Cluster Installation Script # Location: Houston, Texas (us-hou-1) # K3s Version: v1.35.0+k3s1 set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}🚀 AiWorker K3s HA Cluster Installation${NC}" echo "========================================" # Configuration K3S_VERSION="v1.35.0+k3s1" CONTROL_PLANE_IPS=("108.165.47.233" "108.165.47.235" "108.165.47.215") CONTROL_PLANE_PRIVATE=("10.100.0.2" "10.100.0.3" "10.100.0.4") WORKER_IPS=("108.165.47.225" "108.165.47.224" "108.165.47.222") WORKER_PRIVATE=("10.100.0.5" "10.100.0.6" "10.100.0.7") # Step 1: Install first control plane with cluster-init echo -e "\n${YELLOW}Step 1/5: Installing first control plane (HA mode)${NC}" ssh -o StrictHostKeyChecking=no root@${CONTROL_PLANE_IPS[0]} "curl -sfL https://get.k3s.io | \ INSTALL_K3S_VERSION=${K3S_VERSION} \ INSTALL_K3S_EXEC='server \ --cluster-init \ --disable traefik \ --disable servicelb \ --node-name k8s-cp-01 \ --node-ip ${CONTROL_PLANE_PRIVATE[0]} \ --flannel-iface eth1 \ --tls-san ${CONTROL_PLANE_IPS[0]} \ --tls-san ${CONTROL_PLANE_IPS[1]} \ --tls-san ${CONTROL_PLANE_IPS[2]} \ --tls-san ${CONTROL_PLANE_PRIVATE[0]} \ --tls-san ${CONTROL_PLANE_PRIVATE[1]} \ --tls-san ${CONTROL_PLANE_PRIVATE[2]}' \ sh -" echo -e "${GREEN}✓ First control plane installed${NC}" # Get K3s token echo -e "\n${YELLOW}Retrieving K3s token...${NC}" K3S_TOKEN=$(ssh root@${CONTROL_PLANE_IPS[0]} "cat /var/lib/rancher/k3s/server/node-token") echo -e "${GREEN}✓ Token retrieved${NC}" # Download kubeconfig echo -e "\n${YELLOW}Downloading kubeconfig...${NC}" ssh root@${CONTROL_PLANE_IPS[0]} "cat /etc/rancher/k3s/k3s.yaml" | \ sed "s/127.0.0.1/${CONTROL_PLANE_IPS[0]}/g" > ~/.kube/aiworker-config chmod 600 ~/.kube/aiworker-config echo -e "${GREEN}✓ Kubeconfig saved to ~/.kube/aiworker-config${NC}" # Wait for first node to be ready echo -e "\n${YELLOW}Waiting for first node to be ready...${NC}" sleep 10 # Step 2: Join additional control planes echo -e "\n${YELLOW}Step 2/5: Joining additional control planes${NC}" for i in 1 2; do echo " Installing k8s-cp-0$((i+1))..." ssh -o StrictHostKeyChecking=no root@${CONTROL_PLANE_IPS[$i]} "curl -sfL https://get.k3s.io | \ INSTALL_K3S_VERSION=${K3S_VERSION} \ K3S_TOKEN='${K3S_TOKEN}' \ INSTALL_K3S_EXEC='server \ --server https://${CONTROL_PLANE_PRIVATE[0]}:6443 \ --disable traefik \ --disable servicelb \ --node-name k8s-cp-0$((i+1)) \ --node-ip ${CONTROL_PLANE_PRIVATE[$i]} \ --flannel-iface eth1 \ --tls-san ${CONTROL_PLANE_IPS[0]} \ --tls-san ${CONTROL_PLANE_IPS[1]} \ --tls-san ${CONTROL_PLANE_IPS[2]}' \ sh -" & done wait echo -e "${GREEN}✓ All control planes installed${NC}" sleep 15 # Step 3: Join worker nodes echo -e "\n${YELLOW}Step 3/5: Joining worker nodes${NC}" for i in 0 1 2; do echo " Installing k8s-worker-0$((i+1))..." ssh -o StrictHostKeyChecking=no root@${WORKER_IPS[$i]} "curl -sfL https://get.k3s.io | \ INSTALL_K3S_VERSION=${K3S_VERSION} \ K3S_TOKEN='${K3S_TOKEN}' \ K3S_URL='https://${CONTROL_PLANE_PRIVATE[0]}:6443' \ INSTALL_K3S_EXEC='--node-name k8s-worker-0$((i+1)) \ --node-ip ${WORKER_PRIVATE[$i]} \ --flannel-iface eth1' \ sh -" & done wait echo -e "${GREEN}✓ All workers joined${NC}" sleep 15 # Step 4: Verify cluster echo -e "\n${YELLOW}Step 4/5: Verifying cluster${NC}" export KUBECONFIG=~/.kube/aiworker-config kubectl get nodes -o wide # Step 5: Install core components echo -e "\n${YELLOW}Step 5/5: Installing core components${NC}" # Nginx Ingress echo " Installing Nginx Ingress Controller..." kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/baremetal/deploy.yaml kubectl wait --for=condition=ready pod -l app.kubernetes.io/component=controller -n ingress-nginx --timeout=300s echo -e "${GREEN}✓ Nginx Ingress installed${NC}" # Cert-Manager echo " Installing Cert-Manager..." kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.2/cert-manager.yaml kubectl wait --for=condition=ready pod -l app.kubernetes.io/component=controller -n cert-manager --timeout=300s echo -e "${GREEN}✓ Cert-Manager installed${NC}" # Create Let's Encrypt issuers cat <