# 进入防止 kind-tool.sh 的目录
$ cd kt-dir/
# 用 alias 给个别名,更便于使用
$ alias kt="./kind-tool.sh"
# 创建单节点集群,此种默认采用最新版本镜像
$ kt create single-node-cluster
# 指定 k8s 镜像版本
$ kt create single-node-cluster --image kindest/node:v1.24.5
# 采用 默认版本(目前配置为 kindest/node:v1.24.3 )—— k8s 1.24.3 版本
$ kt create single-node-cluster --default# 查看目前配置的多节点集群模板
$ kt cds list
🔍 Listing all available templates:
- 3node-demo
- ingress-cluster-demo# 创建 3 节点的集群
$ kt create 3-node-cluster --image kindest/node:v1.24.3 --config 3node-demo# 查看当前所有集群
$ kt list# 删除集群
$ kt delete 3-node-cluster# 加载指定镜像到指定集群
$ kt load 3-node-cluster busybox:latest# 查看帮助信息
$ kt -h
Usage: ./kind-tool.sh [command] [options]
Commands:create <cluster-name> [--image <image>] [--config <template>] [--default] Create a new Kind clustersub-args:--image <image> Create a cluster using the image you specified--config <template> Template is used to configure the architecture of the cluster (multi node or port exposure)--default Create a cluster using the default image version (k8s 1.24.3)delete <cluster-name> Delete the Kind clusterexport-kubeconfig <cluster-name> Export Kubeconfig for cluster, Short command(ek)load-image <cluster-name> <image> Load Docker image into Kind cluster, Short command(load)status <cluster-name> Get cluster statuslist List all existing Kind clustersuse <cluster-name> Switch to the specified Kind clustercluster-demo-setting <subcommand> [options] Short command(cds for cluster-demo-setting)subcommands:list List all supported templatesshow <template-name> Show the content of a specific templatehelp Display this help message
脚本
#!/bin/bash# 配置
KUBECONFIG_PATH="./"
WITH_METRICS=false
WITH_INGRESS=false
LOAD_IMAGE=false
DEFAULT_IMAGE="kindest/node:v1.24.3" # 默认镜像# 显示勾选或叉
checkmark="✅"
crossmark="❌"# 模板配置
TEMPLATES_DIR="cluster-demo-setting"
DEFAULT_TEMPLATE="ingress-cluster-demo"# 打印帮助信息
usage() {echo "Usage: $0 [command] [options]"echo "Commands:"echo " create <cluster-name> [--image <image>] [--config <template>] [--default] Create a new Kind cluster"echo " sub-args:"echo " --image <image> Create a cluster using the image you specified "echo " --config <template> Template is used to configure the architecture of the cluster (multi node or port exposure)"echo " --default Create a cluster using the default image version (k8s 1.24.3)"echo " delete <cluster-name> Delete the Kind cluster"echo " export-kubeconfig <cluster-name> Export Kubeconfig for cluster, Short command(ek)"echo " load-image <cluster-name> <image> Load Docker image into Kind cluster, Short command(load)"echo " status <cluster-name> Get cluster status"echo " list List all existing Kind clusters"echo " use <cluster-name> Switch to the specified Kind cluster"echo " cluster-demo-setting <subcommand> [options] Short command(cds for cluster-demo-setting)"echo " subcommands:"echo " list List all supported templates"echo " show <template-name> Show the content of a specific template"echo " help Display this help message"exit 1
}# 检查是否安装 kind 和 kubectl
command -v kind &>/dev/null || { echo "Kind is not installed!"; exit 1; }
command -v kubectl &>/dev/null || { echo "kubectl is not installed!"; exit 1; }# 打印命令并执行
run_command() {echo "-------- Command Info -------"echo "Executing: $1"echo "<-------- Command Result ------->"eval "$1"echo " "
}# 加载指定模板配置
load_template() {TEMPLATE_NAME=$1TEMPLATE_FILE="$TEMPLATES_DIR/$TEMPLATE_NAME.yaml"if [ ! -f "$TEMPLATE_FILE" ]; thenecho "Error: Template '$TEMPLATE_NAME' not found!"exit 1fiecho "#Using template '$TEMPLATE_NAME' from $TEMPLATE_FILE..."cat "$TEMPLATE_FILE"echo
}# 创建集群
create_cluster() {CLUSTER_NAME=$1IMAGE=TEMPLATE=shift # 移除第一个参数(集群名称)# 解析命令行参数while [[ "$#" -gt 0 ]]; docase $1 in--image) IMAGE="$2"; shift 2 ;;--config) TEMPLATE="$2"; shift 2 ;;--default) IMAGE=$DEFAULT_IMAGE; shift ;;*) echo "Unknown parameter: $1"; usage ;;esacdoneecho "🚀 Creating Kind cluster: $CLUSTER_NAME with image: $IMAGE and template: $TEMPLATE..."# 加载模板if [ -n "$TEMPLATE" ]; thenload_template "$TEMPLATE" > "$CLUSTER_NAME-kind-config.yaml"CONFIG="--config $(pwd)/$CLUSTER_NAME-kind-config.yaml"fi# 打印并执行创建集群命令if [ -n "$CONFIG" ] && [ -n "$IMAGE" ]; thenrun_command "kind create cluster --name \"$CLUSTER_NAME\" --image \"$IMAGE\" $CONFIG"elif [ -n "$CONFIG" ]; thenrun_command "kind create cluster --name \"$CLUSTER_NAME\" $CONFIG"elif [ -n "$IMAGE" ]; thenrun_command "kind create cluster --name \"$CLUSTER_NAME\" --image \"$IMAGE\""elserun_command "kind create cluster --name \"$CLUSTER_NAME\""fi# 如果需要安装 metrics-serverif $WITH_METRICS; thenecho "📊 Installing metrics-server..."run_command "kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml"fi# 如果需要安装 ingress-nginxif $WITH_INGRESS; thenecho "🌐 Installing ingress-nginx..."run_command "kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml"fiecho "✅ Cluster created successfully!"
}# 切换集群
use_cluster() {CLUSTER_NAME=$1echo "🔄 Switching to Kind cluster: $CLUSTER_NAME..."run_command "kubectl config use-context kind-$CLUSTER_NAME"echo "✅ Switched to $CLUSTER_NAME."
}# 导出 kubeconfig 文件
export_kubeconfig() {CLUSTER_NAME=$1echo "📤 Exporting kubeconfig to $KUBECONFIG_PATH..."# 打印并执行导出 kubeconfig 命令KUBECONFIG_FILE="./$CLUSTER_NAME-kubeconfig"run_command "kind get kubeconfig --name \"$CLUSTER_NAME\" > \"$KUBECONFIG_FILE\""echo "✅ Kubeconfig exported!"
}# 加载 Docker 镜像到 Kind 集群
load_image() {CLUSTER_NAME=$1IMAGE_TAG=$2echo "💾 Loading Docker image '$IMAGE_TAG' into Kind cluster '$CLUSTER_NAME'..."# 打印并执行加载镜像命令run_command "kind load docker-image \"$IMAGE_TAG\" --name \"$CLUSTER_NAME\""echo "✅ Image loaded successfully!"
}# 健康检查函数
health_check() {echo "🧠 Running health check for the cluster..."echo -n "🧩 Nodes reachable: "run_command "kubectl get nodes &>/dev/null && echo \"$checkmark\" || echo \"$crossmark\""echo -n "📈 Metrics-server running: "run_command "kubectl get deployment metrics-server -n kube-system &>/dev/null && echo \"$checkmark\" || echo \"$crossmark\""echo -n "🌐 Ingress controller running: "run_command "kubectl get pods -n ingress-nginx &>/dev/null && echo \"$checkmark\" || echo \"$crossmark\""echo -n "🧪 Demo app deployed: "run_command "kubectl get deploy demo &>/dev/null && echo \"$checkmark\" || echo \"$crossmark\""echo -n "🛰️ Ingress route configured: "run_command "kubectl get ingress demo-ingress &>/dev/null && echo \"$checkmark\" || echo \"$crossmark\""
}# 处理集群模板设置命令
cluster_demo_setting() {if [[ "$1" == "list" ]]; thenecho "🔍 Listing all available templates:"for template in "$TEMPLATES_DIR"/*.yaml; doecho "- $(basename "$template" .yaml)"doneelif [[ "$1" == "show" ]]; thenif [[ -z "$2" ]]; thenecho "Error: Template name is required!"exit 1fiload_template "$2"elseecho "Unknown subcommand: $1"usagefi
}# 集群命令执行
case "$1" in"create")if [ -z "$2" ]; thenecho "Cluster name is required!"usagefi# ${@:2} 表示从忽略第一个参数,传入其余所有参数create_cluster "${@:2}";;"list")echo "🔍 Listing all existing Kind clusters..."run_command "kind get clusters"run_command "kubectl config get-contexts"run_command "kubectl config current-context";;"delete")if [ -z "$2" ]; thenecho "Cluster name is required!"usagefiecho "🗑️ Deleting Kind cluster: $2..."run_command "kind delete cluster --name \"$2\"";;"status")if [ -z "$2" ]; thenecho "Cluster name is required!"usagefiecho "🔍 Checking cluster status..."run_command "kind get clusters | grep -q \"$2\" && echo \"$checkmark Cluster exists: $2\" || echo \"$crossmark Cluster does not exist.\"";;"use")if [ -z "$2" ]; thenecho "Cluster name is required to switch!"usagefiuse_cluster "$2";;"load"|"load-image")if [ -z "$2" ] || [ -z "$3" ]; thenecho "Both cluster name and image tag are required!"usagefiload_image "$2" "$3";;"ek"|"export-kubeconfig")if [ -z "$2" ]; thenecho "Cluster name is required!"usagefiexport_kubeconfig "$2";;"health-check")health_check;;"cds"|"cluster-demo-setting")cluster_demo_setting "${@:2}";;"-h"|"--help"|"help")usage;;*)usage;;
esac