#!/bin/sh exec >> /proc/1/fd/1 2>&1 # =============================================================================== # K3D Cluster Creation # this script access the parametter i.e num of nodes i.e NUM_WORKERS # example command to run this script ./setup.sh 3 cluster1 # =============================================================================== NUM_WORKERS=${1:-0} # Default to 1 worker if not provided CLUSTER_NAME=${2:-cluster} # Check if k3d is installed, if not install it if ! command -v k3d &> /dev/null; then echo "$(date '+%Y-%m-%d %H:%M:%S') | k3d not found. Attempting installation..." # Retry k3d installation up to 5 times INSTALL_ATTEMPTS=0 while [ $INSTALL_ATTEMPTS -lt 5 ]; do INSTALL_ATTEMPTS=$((INSTALL_ATTEMPTS+1)) echo "$(date '+%Y-%m-%d %H:%M:%S') | Installation attempt $INSTALL_ATTEMPTS" # Use the same installation method as in entrypoint.sh TAG=v5.8.3 bash /usr/local/bin/k3d-install.sh # Check if installation was successful if command -v k3d &> /dev/null; then echo "$(date '+%Y-%m-%d %H:%M:%S') | k3d successfully installed" break else echo "$(date '+%Y-%m-%d %H:%M:%S') | k3d installation failed. Retrying in 5 seconds..." sleep 5 fi done # If k3d still not installed after 5 attempts, exit with error if ! command -v k3d &> /dev/null; then echo "$(date '+%Y-%m-%d %H:%M:%S') | ERROR: Failed to install k3d after 5 attempts" exit 1 fi fi # Delete cluster config if exists if [ -f "k3d-config.yaml" ]; then echo "k3d-config.yaml already exists, deleting it" rm -f k3d-config.yaml fi # Generate K3d config cat < /tmp/k3d-config.yaml apiVersion: k3d.io/v1alpha5 kind: Simple metadata: name: $CLUSTER_NAME servers: 1 agents: $NUM_WORKERS ports: - port: "$API_PORT:6443" nodeFilters: - loadbalancer kubeAPI: host: "127.0.0.1" hostPort: "$API_PORT" options: k3s: extraArgs: - arg: "--tls-san=k8s-api-server" nodeFilters: - server:* - arg: "--tls-san=127.0.0.1" nodeFilters: - server:* - arg: "--tls-san=localhost" nodeFilters: - server:* EOF echo "Cluster config with $NUM_WORKERS worker nodes generated: /tmp/k3d-config.yaml" echo "$(date '+%Y-%m-%d %H:%M:%S') | Creating K3d cluster with configuration..." echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Name: $CLUSTER_NAME" echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Config: /tmp/k3d-config.yaml" echo "$(date '+%Y-%m-%d %H:%M:%S') | └── Number of nodes: $((NUM_WORKERS + 1)) (1 server + $NUM_WORKERS agents)" # Create k3d cluster k3d cluster create --config /tmp/k3d-config.yaml # =============================================================================== # Cluster Readiness Check # =============================================================================== echo "$(date '+%Y-%m-%d %H:%M:%S') | Verifying cluster status..." CLUSTER_CHECK_COUNT=0 # Wait for k3d cluster to be ready while ! k3d cluster list | grep -q "$CLUSTER_NAME"; do CLUSTER_CHECK_COUNT=$((CLUSTER_CHECK_COUNT+1)) echo "$(date '+%Y-%m-%d %H:%M:%S') | [WAITING] K3d cluster '$CLUSTER_NAME' not ready yet... (attempt $CLUSTER_CHECK_COUNT)" sleep 5 done echo "$(date '+%Y-%m-%d %H:%M:%S') | ✅ K3d cluster '$CLUSTER_NAME' is up and running!" # =============================================================================== # Setup Complete # =============================================================================== echo "$(date '+%Y-%m-%d %H:%M:%S') | Docker and KIND environment is ready for use" # Save kubeconfig and set API server address cp /home/candidate/.kube/config /home/candidate/.kube/kubeconfig sed -i 's|server: https://127\.0\.0\.1:\([0-9]*\)|server: https://k8s-api-server:\1|' /home/candidate/.kube/kubeconfig export KUBECONFIG=/home/candidate/.kube/kubeconfig #info on config file setup done echo "$(date '+%Y-%m-%d %H:%M:%S') | Config file setup done" #info on cluster setup done echo "$(date '+%Y-%m-%d %H:%M:%S') | Cluster setup done" exit 0