Files
CK-X/kind-cluster/scripts/env-setup
2025-04-02 10:39:59 +05:30

86 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
# ===============================================================================
# KIND 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}
NODE_IMAGE=${3:-kindest/node:v1.32.3}
#delete kind-config.yaml if present
if [ -f "kind-config.yaml" ]; then
echo "kind-config.yaml already exists, deleting it"
rm -f kind-config.yaml
fi
cat <<EOF > /tmp/kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 6443
hostPort: 6443
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
apiServer:
certSANs:
- "k8s-api-server"
- "127.0.0.1"
- "localhost"
EOF
for i in $(seq 1 "$NUM_WORKERS"); do
echo " - role: worker" >> /tmp/kind-config.yaml
done
echo "Cluster config with $NUM_WORKERS worker nodes generated: /tmp/kind-config.yaml"
echo "$(date '+%Y-%m-%d %H:%M:%S') | Creating KIND cluster with configuration..."
echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Name: $CLUSTER_NAME"
echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Image: $NODE_IMAGE"
echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Config: /tmp/kind-config.yaml"
echo "$(date '+%Y-%m-%d %H:%M:%S') | └── Number of nodes: $NUM_WORKERS"
# Create kind cluster
kind create cluster --name $CLUSTER_NAME --image $NODE_IMAGE --config /tmp/kind-config.yaml
# ===============================================================================
# Cluster Readiness Check
# ===============================================================================
echo "$(date '+%Y-%m-%d %H:%M:%S') | Verifying cluster status..."
CLUSTER_CHECK_COUNT=0
# Wait for kind cluster to be ready
while ! kind get clusters | grep "$CLUSTER_NAME"; do
CLUSTER_CHECK_COUNT=$((CLUSTER_CHECK_COUNT+1))
echo "$(date '+%Y-%m-%d %H:%M:%S') | [WAITING] KIND cluster not ready yet... (attempt $CLUSTER_CHECK_COUNT)"
sleep 10
done
echo "$(date '+%Y-%m-%d %H:%M:%S') | [SUCCESS] KIND cluster $CLUSTER_NAME is ready and operational"
# ===============================================================================
# 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://.*|server: https://k8s-api-server:6443|' /home/candidate/.kube/kubeconfig
echo "127.0.0.1 k8s-api-server" >> /etc/hosts
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