Migrate from kind to k3d for cluster fromation

This commit is contained in:
Nishan
2025-04-10 23:55:30 +05:30
parent c7bf53c630
commit 91b6263cf3
8 changed files with 101 additions and 95 deletions

View File

@@ -6,7 +6,7 @@
"name": "CKAD Comprehensive Lab - 1", "name": "CKAD Comprehensive Lab - 1",
"category": "CKAD", "category": "CKAD",
"description": "Hands-on exercises covering core Kubernetes concepts, pod configuration, deployments, and services with practical tasks for application deployment in a cluster.", "description": "Hands-on exercises covering core Kubernetes concepts, pod configuration, deployments, and services with practical tasks for application deployment in a cluster.",
"warmUpTimeInSeconds": 180, "warmUpTimeInSeconds": 260,
"difficulty": "Medium", "difficulty": "Medium",
"examDurationInMinutes": 120 "examDurationInMinutes": 120
}, },
@@ -26,7 +26,7 @@
"name": "CKS Practice Lab - Kubernetes Security Essentials", "name": "CKS Practice Lab - Kubernetes Security Essentials",
"category": "CKS", "category": "CKS",
"description": "Practice essential Kubernetes security concepts including network policies, RBAC, supply chain security, and runtime protection", "description": "Practice essential Kubernetes security concepts including network policies, RBAC, supply chain security, and runtime protection",
"warmUpTimeInSeconds": 180, "warmUpTimeInSeconds": 260,
"difficulty": "Hard", "difficulty": "Hard",
"examDurationInMinutes": 120 "examDurationInMinutes": 120
}, },
@@ -36,7 +36,7 @@
"name": "CKA Practice Lab - Core Concepts", "name": "CKA Practice Lab - Core Concepts",
"category": "CKA", "category": "CKA",
"description": "Practice essential Kubernetes administrator tasks covering cluster management, networking, storage, and security", "description": "Practice essential Kubernetes administrator tasks covering cluster management, networking, storage, and security",
"warmUpTimeInSeconds": 160, "warmUpTimeInSeconds": 260,
"difficulty": "Easy", "difficulty": "Easy",
"examDurationInMinutes": 60 "examDurationInMinutes": 60
}, },
@@ -56,7 +56,7 @@
"name": "Helm Fundamentals Lab", "name": "Helm Fundamentals Lab",
"category": "Other", "category": "Other",
"description": "Comprehensive lab covering Helm basics including chart installation, repositories, customization, packaging, debugging, and advanced Helm operations for Kubernetes applications.", "description": "Comprehensive lab covering Helm basics including chart installation, repositories, customization, packaging, debugging, and advanced Helm operations for Kubernetes applications.",
"warmUpTimeInSeconds": 120, "warmUpTimeInSeconds": 260,
"difficulty": "Medium", "difficulty": "Medium",
"examDurationInMinutes": 90 "examDurationInMinutes": 90
} }

View File

@@ -20,11 +20,19 @@ RUN apt-get update && apt-get install -y \
docker.io\ docker.io\
jq jq
# Accept build-time architecture argument
ARG TARGETARCH
#kubectl #install kubectl for x86_64 and ARM64 architectures
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \ RUN if [ "$TARGETARCH" = "amd64" ]; then \
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& chmod +x kubectl \ && chmod +x kubectl \
&& mv kubectl /usr/local/bin/kubectl && mv kubectl /usr/local/bin/kubectl; \
elif [ "$TARGETARCH" = "arm64" ]; then \
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl" \
&& chmod +x kubectl \
&& mv kubectl /usr/local/bin/kubectl; \
fi
# Install Helm # Install Helm
RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \ RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 \

View File

@@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
exec >> /proc/1/fd/1 2>&1
# cleanup-exam-env.sh # cleanup-exam-env.sh
# #
@@ -20,14 +21,9 @@ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null candidate@k8s-ap
#cleanup docker env #cleanup docker env
log "Cleaning up docker environment" log "Cleaning up docker environment"
docker system prune -a --volumes -f docker system prune -a --volumes -fa
docker rm -f $(docker ps -a -q) docker network prune -fa
docker rmi -f $(docker images -q) docker image prune -fa
docker volume rm $(docker volume ls -q)
docker network rm $(docker network ls -q)
docker container rm $(docker container ls -q)
docker image rm $(docker image ls -q)
docker volume rm $(docker volume ls -q)
# Remove the exam environment directory # Remove the exam environment directory
log "Removing exam environment directory" log "Removing exam environment directory"

View File

@@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
exec >> /proc/1/fd/1 2>&1
# Log function with timestamp # Log function with timestamp

View File

@@ -3,24 +3,8 @@ FROM docker:dind
# Accept build-time architecture argument # Accept build-time architecture argument
ARG TARGETARCH ARG TARGETARCH
# install kind for x86_64 architecture #install curl
RUN if [ "$TARGETARCH" = "amd64" ]; then \ RUN apk add --no-cache curl
wget https://kind.sigs.k8s.io/dl/v0.27.0/kind-linux-amd64 && \
chmod +x kind-linux-amd64 && \
mv kind-linux-amd64 /usr/local/bin/kind && \
echo "Building for x86_64 platform"; \
touch /amd64-ready; \
fi
# install kind for ARM64 architecture
RUN if [ "$TARGETARCH" = "arm64" ]; then \
wget https://kind.sigs.k8s.io/dl/v0.27.0/kind-linux-arm64 && \
chmod +x kind-linux-arm64 && \
mv kind-linux-arm64 /usr/local/bin/kind && \
echo "Building for ARM64 platform"; \
touch /arm64-ready; \
fi
# use our own to start kind cluster # use our own to start kind cluster
COPY entrypoint.sh /usr/local/bin/entrypoint.sh COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh

View File

@@ -8,8 +8,12 @@
echo "$(date '+%Y-%m-%d %H:%M:%S') | ===== INITIALIZATION STARTED =====" echo "$(date '+%Y-%m-%d %H:%M:%S') | ===== INITIALIZATION STARTED ====="
echo "$(date '+%Y-%m-%d %H:%M:%S') | Executing container startup script..." echo "$(date '+%Y-%m-%d %H:%M:%S') | Executing container startup script..."
# Execute current entrypoint # Execute current entrypoint script
sh /usr/local/bin/startup.sh & if [ -f /usr/local/bin/startup.sh ]; then
sh /usr/local/bin/startup.sh &
else
echo "$(date '+%Y-%m-%d %H:%M:%S') | [INFO] Default startup script not found at /usr/local/bin/startup.sh"
fi
# =============================================================================== # ===============================================================================
# Docker Readiness Check # Docker Readiness Check
@@ -36,6 +40,9 @@ adduser -S -D -H -s /sbin/nologin -G sshd sshd
#start ssh service #start ssh service
/usr/sbin/sshd -D & /usr/sbin/sshd -D &
#install k3d
wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | TAG=v5.8.3 bash
sleep 10 sleep 10
touch /ready touch /ready

View File

@@ -1,43 +1,48 @@
# Cleanup script to remove the Kind cluster #!/bin/bash
exec >> /proc/1/fd/1 2>&1
# Cleanup script to remove the K3d cluster
CLUSTER_NAME=${1:-cluster} CLUSTER_NAME=${1:-cluster}
#check if CLUSTER_NAME is set # Check if CLUSTER_NAME is set
if [ -z "$CLUSTER_NAME" ]; then if [ -z "$CLUSTER_NAME" ]; then
echo "CLUSTER_NAME is not set" echo "CLUSTER_NAME is not set"
exit 1 exit 1
fi fi
# Check if the Kind cluster is running # Check if the K3d cluster is running
if kind get clusters | grep -q "$CLUSTER_NAME"; then if k3d cluster list | grep -q "$CLUSTER_NAME"; then
# Delete the Kind cluster # Delete the K3d cluster
kind delete cluster --name "$CLUSTER_NAME" k3d cluster delete "$CLUSTER_NAME"
# Wait until the cluster is fully deleted # Wait until the cluster is fully deleted
while kind get clusters -q | grep -q "$CLUSTER_NAME"; do while k3d cluster list | grep -q "$CLUSTER_NAME"; do
sleep 1 sleep 1
done done
echo "Kind cluster $CLUSTER_NAME has been deleted." echo "K3d cluster '$CLUSTER_NAME' has been deleted."
else else
echo "Kind cluster $CLUSTER_NAME is not running." echo "K3d cluster '$CLUSTER_NAME' is not running."
exit 0 exit 0
fi fi
# Perform a full Docker system prune (removes all unused data) but do not remove kindest/node images # Perform a full Docker system prune (removes all unused containers, networks, images, and build cache)
docker system prune -a -f --filter "label=org.opencontainers.image.name=kindest/node" # This does NOT remove K3d images, unless you manually want to
docker volume prune -f docker volume prune -f
docker network prune -f
docker image prune -f --filter "label!=ghcr.io/k3d-io"
echo "Docker system cleaned up." echo "Docker system cleaned up."
#delete kind-config.yaml if present # Delete K3d config file if present
if [ -f "/tmp/kind-config.yaml" ]; then if [ -f "/tmp/k3d-config.yaml" ]; then
rm -f /tmp/kind-config.yaml rm -f /tmp/k3d-config.yaml
echo "Kind-config.yaml file deleted." echo "K3d config file deleted."
fi fi
#delete kubeconfig file if present in the /root/.kube/kubeconfig # Delete kubeconfig file if present in /home/candidate/.kube/kubeconfig (adjust path if needed)
if [ -f "/root/.kube/kubeconfig" ]; then if [ -f "/home/candidate/.kube/kubeconfig" ]; then
rm -f /root/.kube/kubeconfig rm -f /home/candidate/.kube/kubeconfig
echo "Kubeconfig file deleted." echo "Kubeconfig file deleted."
fi fi

View File

@@ -1,54 +1,58 @@
#!/bin/sh #!/bin/sh
exec >> /proc/1/fd/1 2>&1
# =============================================================================== # ===============================================================================
# KIND Cluster Creation # K3D Cluster Creation
# this script access the parametter i.e num of nodes i.e NUM_WORKERS # this script access the parametter i.e num of nodes i.e NUM_WORKERS
# example command to run this script ./setup.sh 3 cluster1 # example command to run this script ./setup.sh 3 cluster1
# =============================================================================== # ===============================================================================
NUM_WORKERS=${1:-0} # Default to 1 worker if not provided NUM_WORKERS=${1:-0} # Default to 1 worker if not provided
CLUSTER_NAME=${2:-cluster} CLUSTER_NAME=${2:-cluster}
NODE_IMAGE=${3:-kindest/node:v1.32.3}
#delete kind-config.yaml if present # Delete cluster config if exists
if [ -f "kind-config.yaml" ]; then if [ -f "k3d-config.yaml" ]; then
echo "kind-config.yaml already exists, deleting it" echo "k3d-config.yaml already exists, deleting it"
rm -f kind-config.yaml rm -f k3d-config.yaml
fi fi
cat <<EOF > /tmp/kind-config.yaml # Generate K3d config
kind: Cluster cat <<EOF > /tmp/k3d-config.yaml
apiVersion: kind.x-k8s.io/v1alpha4 apiVersion: k3d.io/v1alpha5
nodes: kind: Simple
- role: control-plane metadata:
extraPortMappings: name: $CLUSTER_NAME
- containerPort: 6443 servers: 1
hostPort: 6443 agents: $NUM_WORKERS
kubeadmConfigPatches: ports:
- | - port: "$API_PORT:6443"
apiVersion: kubeadm.k8s.io/v1beta3 nodeFilters:
kind: ClusterConfiguration - loadbalancer
apiServer: kubeAPI:
certSANs: host: "127.0.0.1"
- "k8s-api-server" hostPort: "$API_PORT"
- "127.0.0.1" options:
- "localhost" 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 EOF
for i in $(seq 1 "$NUM_WORKERS"); do echo "Cluster config with $NUM_WORKERS worker nodes generated: /tmp/k3d-config.yaml"
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 K3d cluster with configuration..."
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') | ├── Name: $CLUSTER_NAME"
echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Image: $NODE_IMAGE" echo "$(date '+%Y-%m-%d %H:%M:%S') | ├── Config: /tmp/k3d-config.yaml"
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 + 1)) (1 server + $NUM_WORKERS agents)"
echo "$(date '+%Y-%m-%d %H:%M:%S') | └── Number of nodes: $NUM_WORKERS"
# Create kind cluster # Create k3d cluster
kind create cluster --name $CLUSTER_NAME --image $NODE_IMAGE --config /tmp/kind-config.yaml k3d cluster create --config /tmp/k3d-config.yaml
# =============================================================================== # ===============================================================================
# Cluster Readiness Check # Cluster Readiness Check
@@ -57,14 +61,14 @@ kind create cluster --name $CLUSTER_NAME --image $NODE_IMAGE --config /tmp/kind-
echo "$(date '+%Y-%m-%d %H:%M:%S') | Verifying cluster status..." echo "$(date '+%Y-%m-%d %H:%M:%S') | Verifying cluster status..."
CLUSTER_CHECK_COUNT=0 CLUSTER_CHECK_COUNT=0
# Wait for kind cluster to be ready # Wait for k3d cluster to be ready
while ! kind get clusters | grep "$CLUSTER_NAME"; do while ! k3d cluster list | grep -q "$CLUSTER_NAME"; do
CLUSTER_CHECK_COUNT=$((CLUSTER_CHECK_COUNT+1)) 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)" echo "$(date '+%Y-%m-%d %H:%M:%S') | [WAITING] K3d cluster '$CLUSTER_NAME' not ready yet... (attempt $CLUSTER_CHECK_COUNT)"
sleep 10 sleep 5
done done
echo "$(date '+%Y-%m-%d %H:%M:%S') | [SUCCESS] KIND cluster $CLUSTER_NAME is ready and operational" echo "$(date '+%Y-%m-%d %H:%M:%S') | ✅ K3d cluster '$CLUSTER_NAME' is up and running!"
# =============================================================================== # ===============================================================================
# Setup Complete # Setup Complete
@@ -74,9 +78,10 @@ echo "$(date '+%Y-%m-%d %H:%M:%S') | Docker and KIND environment is ready for us
# Save kubeconfig and set API server address # Save kubeconfig and set API server address
cp /home/candidate/.kube/config /home/candidate/.kube/kubeconfig cp /home/candidate/.kube/config /home/candidate/.kube/kubeconfig
sed -i 's|server: https://.*|server: https://k8s-api-server:6443|' /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
echo "127.0.0.1 k8s-api-server" >> /etc/hosts
export KUBECONFIG=/home/candidate/.kube/kubeconfig export KUBECONFIG=/home/candidate/.kube/kubeconfig
#info on config file setup done #info on config file setup done
echo "$(date '+%Y-%m-%d %H:%M:%S') | Config file setup done" echo "$(date '+%Y-%m-%d %H:%M:%S') | Config file setup done"