$ kind kind creates and manages local Kubernetes clusters using Docker container 'nodes'
Usage: kind [command]
Available Commands: build Build one of [base-image, node-image] create Creates one of [cluster] delete Deletes one of [cluster] export exports one of [logs] get Gets one of [clusters, nodes, kubeconfig-path] help Help about any command load Loads images into nodes version prints the kind CLI version
Flags: -h, --help help for kind --loglevel string logrus log level [panic, fatal, error, warning, info, debug] (default "warning") --version version for kind
Use "kind [command] --help" for more information about a command.
简单说下几个比较常用选项的含义:
build:用来从 Kubernetes 源代码构建一个新的镜像。
create:创建一个 Kubernetes 集群。
delete:删除一个 Kubernetes 集群。
get: 可用来查看当前集群、节点信息以及 Kubectl 配置文件的地址。
load:从宿主机向 Kubernetes 节点内导入镜像。
使用 Kind 创建 Kubernetes 集群
搭建一个单节点集群
搭建单节点集群是 Kind 最基础的功能,当然使用起来也很简单,仅需一条指令即可完成。
1 2 3 4 5 6 7 8 9 10
$ kind create cluster --name my-cluster Creating cluster "my-cluster" ... ✓ Ensuring node image (kindest/node:v1.15.3) 🖼 ✓ Preparing nodes 📦 ✓ Creating kubeadm config 📜 ✓ Starting control-plane 🕹️ Cluster creation complete. You can now use the cluster with:
export KUBECONFIG="$(kind get kubeconfig-path --name="my-cluster")" kubectl cluster-info
# 获取指定集群的配置文件所在的路径 $ export KUBECONFIG="$(kind get kubeconfig-path --name="my-cluster")" $ kubectl cluster-info Kubernetes master is running at https://localhost:34458 KubeDNS is running at https://localhost:34458/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
$ kubectl get nodes NAME STATUS ROLES AGE VERSION my-cluster-control-plane Ready master 2m v1.15.3
$ kubectl get nodes NAME STATUS ROLES AGE VERSION my-cluster-multi-node-control-plane Ready master 3m20s v1.15.3 my-cluster-multi-node-worker Ready <none> 3m8s v1.15.3
$ kind create cluster --name my-cluster-ha --config my-cluster-ha.yaml Creating cluster "my-cluster-ha" ... ✓ Ensuring node image (kindest/node:v1.15.3) 🖼 ✓ Preparing nodes 📦📦📦📦📦📦📦 ✓ Starting the external load balancer ⚖️ ✓ Creating kubeadm config 📜 ✓ Starting control-plane 🕹️ ✓ Joining more control-plane nodes 🎮 ✓ Joining worker nodes 🚜 Cluster creation complete. You can now use the cluster with:
export KUBECONFIG="$(kind get kubeconfig-path --name="my-cluster-ha")" kubectl cluster-info master $ export KUBECONFIG="$(kind get kubeconfig-path --name="my-cluster-ha")" master $ kubectl cluster-info Kubernetes master is running at https://localhost:44019 KubeDNS is running at https://localhost:44019/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
同样,我们根据上面命令执行完后,输出的提示信息进行操作来验证一下集群是否部署成功。
1 2 3 4 5 6 7 8
$ kubectl get nodes NAME STATUS ROLES AGE VERSION my-cluster-ha-control-plane Ready master 3m42s v1.15.3 my-cluster-ha-control-plane2 Ready master 3m24s v1.15.3 my-cluster-ha-control-plane3 Ready master 2m13s v1.15.3 my-cluster-ha-worker Ready <none> 96s v1.15.3 my-cluster-ha-worker2 Ready <none> 98s v1.15.3 my-cluster-ha-worker3 Ready <none> 95s v1.15.3
Node 镜像的构建比较复杂,目前是通过运行 Base 镜像并在 Base 镜像内执行操作,再保存此容器内容为镜像的方式来构建的,包含的操作有:
构建 Kubernetes 相关资源,比如:二进制文件和镜像。
运行一个用于构建的容器
把构建的 Kubernetes 相关资源复制到容器里
调整部分组件配置参数,以支持在容器内运行
预先拉去运行环境需要的镜像
通过 docker commit 方式保存当前的构建容器为 Node 镜像
如何快速删除一个集群
如果你不需要本地的集群环境,通过以下命令进行删除:
1 2 3
$ kind delete cluster --name my-cluster Deleting cluster "my-cluster" ... $KUBECONFIG is still set to use /root/.kube/kind-config-my-cluster even though that file has been deleted, remember to unset it