#!/bin/bash # Color codes for echo statements RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # No Color # Function to display colored messages echo_colored() { local color=$1 shift echo -e "${color}$@$NC" } # Step 1: Ask for the number of nodes read -p "Enter the number of nodes in your K8S cluster: " num_nodes # Step 2: Gather node details and store them node_details=() for ((i=1; i<=num_nodes; i++)); do read -p "Enter IP address for node $i: " ip read -p "Enter SSH username for node $i: " user read -s -p "Enter SSH password for node $i: " password echo "" node_details+=("$ip:$user:$password") done # Step 3: Display stored IP addresses and ask for confirmation echo_colored "$GREEN" "Stored Node Details:" for detail in "${node_details[@]}"; do IFS=':' read -r -a split <<< "$detail" echo_colored "$YELLOW" "IP: ${split[0]}, User: ${split[1]}" done read -p "Are the above details correct? (yes/no): " confirm if [ "$confirm" != "yes" ]; then echo_colored "$RED" "Please edit your node details and run this script again." exit 1 fi # Step 4: Ask for the master node read -p "Enter the IP address of the master node (control plane): " master_ip # Function to execute commands on a remote node using SSH ssh_cmd() { local ip=$1 local user=$2 local password=$3 shift 3 sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$user@$ip" "$@" } # Step 5: Update and upgrade all nodes, then reboot them echo_colored "$GREEN" "Updating and upgrading all nodes..." for detail in "${node_details[@]}"; do IFS=':' read -r -a split <<< "$detail" ssh_cmd "${split[0]}" "${split[1]}" "${split[2]}" "sudo apt update && sudo apt upgrade -y" ssh_cmd "${split[0]}" "${split[1]}" "${split[2]}" "sudo reboot" done # Step 6: Install K8S on the master node echo_colored "$GREEN" "Installing Kubernetes on the master node..." master_ip_split=(${master_ip//:/ }) sshpass -p "${master_ip_split[2]}" ssh -o StrictHostKeyChecking=no "${master_ip_split[1]}@${master_ip_split[0]}" << 'EOF' sudo apt update curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - cat <