kind 安装 k8s

1. 安装docker 环境

# 使用命令快速安装
sudo sh -c "$(curl -fsSL https://get.docker.com)"

2. 安装 kubectl 工具

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

sudo install -o root -g root -g root -m 0755 kubectl /usr/local/bin/kubectl

# 验证版本
 kubectl version --client
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:36:36Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7

3. 安装 kind

url: https://github.com/kubernetes-sigs/kind/releases

wget https://github.com/kubernetes-sigs/kind/releases/download/v0.17.0/kind-linux-amd64
# 也可以使用下面加速地址
wget https://git.xfj0.cn/https://github.com/kubernetes-sigs/kind/releases/download/v0.17.0/kind-linux-amd64

chmod +x kind-linux-amd64
mv kind-linux-amd64 /usr/local/bin/kind

# 验证版本
kind version
kind v0.17.0 go1.19.2 linux/amd64

4. 操作

$ kind -h
kind creates and manages local Kubernetes clusters using Docker container 'nodes'

Usage:
  kind [command]

Available Commands:
  build       Build one of [node-image]
  completion  Output shell completion code for the specified shell (bash, zsh or fish)
  create      Creates one of [cluster]
  delete      Deletes one of [cluster]
  export      Exports one of [kubeconfig, logs]
  get         Gets one of [clusters, nodes, kubeconfig]
  help        Help about any command
  load        Loads images into nodes
  version     Prints the kind CLI version

Flags:
  -h, --help              help for kind
      --loglevel string   DEPRECATED: see -v instead
  -q, --quiet             silence all stderr output
  -v, --verbosity int32   info log verbosity, higher value produces more output
      --version           version for kind

$ kind create -h
Creates one of local Kubernetes cluster (cluster)

Usage:
  kind create [flags]
  kind create [command]

Available Commands:
  cluster     Creates a local Kubernetes cluster

Flags:
  -h, --help   help for create

Global Flags:
      --loglevel string   DEPRECATED: see -v instead
  -q, --quiet             silence all stderr output
  -v, --verbosity int32   info log verbosity, higher value produces more output

$ kind create cluster -h
Creates a local Kubernetes cluster using Docker container 'nodes'

Usage:
  kind create cluster [flags]

Flags:
      --config string       path to a kind config file
  -h, --help                help for cluster
      --image string        node docker image to use for booting the cluster
      --kubeconfig string   sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config
  -n, --name string         cluster name, overrides KIND_CLUSTER_NAME, config (default kind)
      --retain              retain nodes for debugging when cluster creation fails
      --wait duration       wait for control plane node to be ready (default 0s)

Global Flags:
      --loglevel string   DEPRECATED: see -v instead
  -q, --quiet             silence all stderr output
  -v, --verbosity int32   info log verbosity, higher value produces more output

4.1 创建集群

kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.25.3) 
 ✓ Preparing nodes
 ✓ Writing configuration 
 ✓ Starting control-plane 
 ✓ Installing CNI 
 ✓ Installing StorageClass 
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Thanks for using kind! 

上面是使用 命令形式创建集群

4.2 使用 yaml 文件创建集群

# kind-config.yaml

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: zzg
nodes:
- role: control-plane
- role: worker
- role: worker

# 该文件一共创建3个节点,一个控制节点,两个工作节点,需要通过 --config 参数指定文件即可
kind create cluster --config kind-config.yaml 
Creating cluster "zzg" ...
 ✓ Ensuring node image (kindest/node:v1.25.3) 
 ✓ Preparing nodes 
 ✓ Writing configuration 
 ✓ Starting control-plane 
 ✓ Installing CNI 
 ✓ Installing StorageClass 
 ✓ Joining worker nodes 
Set kubectl context to "kind-zzg"
You can now use your cluster with:

kubectl cluster-info --context kind-zzg

Not sure what to do next?  Check out https://kind.sigs.k8s.io/docs/user/quick-start/

# 查看集群
kubectl get nodes
NAME                STATUS   ROLES           AGE     VERSION
zzg-control-plane   Ready    control-plane   2m15s   v1.25.3
zzg-worker          Ready    <none>          98s     v1.25.3
zzg-worker2         Ready    <none>          98s     v1.25.3

# kind 查看集群
kind get clusters
kind
zzg

# 切换集群
$ kubectl config use-context kind-kind
Switched to context "kind-kind".
$ kubectl config use-context kind-zzg
Switched to context "kind-zzg".