infra/clusters/vntx/deploy.sh
2025-09-06 10:55:13 -05:00

177 lines
No EOL
4.4 KiB
Bash
Executable file

#!/bin/bash
set -e
echo "=== VNTX K3s Cluster Deployment Script ==="
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Check prerequisites
echo "Checking prerequisites..."
if ! command -v tofu &> /dev/null && ! command -v terraform &> /dev/null; then
echo -e "${RED}ERROR: OpenTofu or Terraform not found${NC}"
echo "Install OpenTofu: https://opentofu.org/docs/intro/install/"
exit 1
fi
if ! command -v ansible &> /dev/null; then
echo -e "${RED}ERROR: Ansible not found${NC}"
echo "Install Ansible: pip install ansible"
exit 1
fi
if ! command -v kubectl &> /dev/null; then
echo -e "${YELLOW}WARNING: kubectl not found${NC}"
echo "Install kubectl to manage the cluster after deployment"
fi
# Use tofu if available, otherwise terraform
TF_CMD="tofu"
if ! command -v tofu &> /dev/null; then
TF_CMD="terraform"
fi
echo -e "${GREEN}Prerequisites OK${NC}"
echo
# Step 1: Configure Terraform
echo "=== Step 1: Configure Terraform ==="
cd terraform
if [ ! -f terraform.tfvars ]; then
echo -e "${RED}ERROR: terraform.tfvars not found${NC}"
echo "Please edit terraform/terraform.tfvars with your settings"
exit 1
fi
# Check for SSH key
if grep -q "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC..." terraform.tfvars; then
echo -e "${RED}ERROR: SSH public key not configured${NC}"
echo "Please add your SSH public key to terraform.tfvars"
echo "You can find it in ~/.ssh/id_rsa.pub or ~/.ssh/id_ed25519.pub"
exit 1
fi
# Verify SSH key is present
if ! grep -E "ssh-(rsa|ed25519|ecdsa)" terraform.tfvars > /dev/null; then
echo -e "${RED}ERROR: No valid SSH key found in terraform.tfvars${NC}"
echo "Please add your SSH public key to terraform.tfvars"
exit 1
fi
# Get Proxmox password
if [ -z "$TF_VAR_proxmox_password" ]; then
echo -n "Enter Proxmox password: "
read -s TF_VAR_proxmox_password
export TF_VAR_proxmox_password
echo
fi
# Initialize Terraform
echo "Initializing Terraform..."
$TF_CMD init
# Plan deployment
echo
echo "Planning infrastructure..."
$TF_CMD plan -out=tfplan
echo
read -p "Deploy infrastructure? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
# Deploy VMs
echo "Deploying Flatcar VMs on Proxmox..."
$TF_CMD apply tfplan
echo -e "${GREEN}Infrastructure deployed!${NC}"
# Step 2: Wait for VMs to be ready
echo
echo "=== Step 2: Waiting for VMs to be ready ==="
cd ../ansible
echo "Waiting 60 seconds for VMs to boot..."
sleep 60
# Test connectivity
echo "Testing connectivity to nodes..."
ansible -i inventory.yml all -m ping --one-line
# Step 3: Prepare Debian nodes
echo
echo "=== Step 3: Preparing Debian nodes ==="
ansible-playbook -i inventory.yml prepare-debian.yml
# Step 4: Install K3s
echo
echo "=== Step 4: Installing K3s cluster ==="
ansible-playbook -i inventory.yml k3s-install.yml
# Step 5: Setup kubeconfig
echo
echo "=== Step 5: Setting up kubectl access ==="
if [ -f kubeconfig ]; then
mkdir -p ~/.kube
cp kubeconfig ~/.kube/config-vntx
export KUBECONFIG=~/.kube/config-vntx
echo "Cluster nodes:"
kubectl get nodes
echo
echo -e "${GREEN}K3s cluster deployed successfully!${NC}"
echo
echo "To use this cluster:"
echo " export KUBECONFIG=~/.kube/config-vntx"
echo " kubectl get nodes"
else
echo -e "${RED}ERROR: kubeconfig not found${NC}"
exit 1
fi
# Step 6: Deploy core services
echo
read -p "Deploy core services (MetalLB, Longhorn)? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cd ..
echo "Creating namespaces..."
kubectl apply -f system/base/namespaces.yaml
echo "Installing MetalLB..."
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.12/config/manifests/metallb-native.yaml
echo "Waiting for MetalLB to be ready..."
kubectl wait --namespace metallb-system \
--for=condition=ready pod \
--selector=app=metallb \
--timeout=90s
echo "Configuring MetalLB..."
kubectl apply -f system/metallb/config.yaml
echo "Installing Longhorn..."
kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.5.3/deploy/longhorn.yaml
echo -e "${GREEN}Core services deployed!${NC}"
fi
echo
echo "=== Deployment Complete ==="
echo
echo "Cluster endpoints:"
echo " Control plane: https://10.0.0.101:6443"
echo
echo "Next steps:"
echo "1. Deploy your services: kubectl apply -f infrastructure/"
echo "2. Check the deployment guide: cat DEPLOYMENT.md"