Files
CK-X/kind-cluster/scripts/env-setup

91 lines
3.0 KiB
Bash
Executable File

#!/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}
# 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 <<EOF > /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