20

使用kind快速创建本地集群

 3 years ago
source link: http://www.cnblogs.com/rancherlabs/p/13606352.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

简 介

kind是另一个Kubernetes SIG项目,但它与minikube有很大区别。它可以将集群迁移到Docker容器中,这与生成虚拟机相比,启动速度大大加快。简而言之,kind是一个使用Docker容器节点运行本地Kubernetes集群的工具(CLI)。

接下来,我们开始吧!

前期准备

想要顺利完成本教程,你需要在本地系统中准备好以下程序:

  • Go

  • 需要运行的Docker服务

安 装

使用以下命令下载和安装kind二进制文件:

GO111MODULE=”on” go get sigs.k8s.io/[email protected]

确保kind二进制文件是存在的

> kind version
kind v0.8.1 go1.14.2 darwin/amd64

现在,我们应该能够使用 kind CLI来启动一个Kubernetes集群:

Usage:
  kind [command]Available Commands:
  build       Build one of [node-image]
  completion  Output shell completion code for the specified shell
  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

在本文中,我们将聚焦于 creategetdelete 命令。

创建一个集群

执行以下命令即可创建一个集群:

kind create cluster

> kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.18.2)   
 ✓ Preparing nodes :package:
 ✓ Writing configuration :scroll:
 ✓ Starting control-plane   ️
 ✓ Installing CNI :electric_plug:
 ✓ Installing StorageClass :floppy_disk:
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kind Have a nice day! :wave:

将通过拉取最新的Kubernetes节点(v 1.18.2)来创建一个Kubernetes集群。刚刚我们已经创建了一个v 1.18.2的Kubernetes集群。

在创建集群的过程中如果我们没有 --name 参数,那么集群名称将会默认设置为 kind

创建特定版本的K8S集群

我们可以传 --image 参数来部署一个特定版本的Kubernetes集群。

使用的命令为:

kind create cluster --image kindest/node:v1.15.6

Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.15.6)   
 ✓ Preparing nodes :package:
 ✓ Writing configuration :scroll:
 ✓ Starting control-plane   ️
 ✓ Installing CNI :electric_plug:
 ✓ Installing StorageClass :floppy_disk:
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kind Have a nice day! :wave:

列出部署的集群

输入命令: kind get clusters

> kind get clusters
kind
kind-1.15.6

这应该列出我们此前创建的两个不同K8S版本的集群。

为kubectl设置上下文

创建集群之后,kubectl会指出最近创建的K8S集群。

让我们来检查一下所有可用的上下文。

> kubectl config get-contexts
CURRENT   NAME                               CLUSTER                      
          kind-kind                          kind-kind            
*         kind-kind-1.15.6                   kind-kind-1.15.6

从输出中,我们可以得到结论,kubectl上下文目前已经被设置为最新的集群,即kind-1.15.6。(上下文名称是以kind为前缀的)

要将kubectl上下文设置为版本是1.18.2的 kind 集群,我们需要进行如下操作:

> kubectl config set-context kind-kind 
Context "kind-kind" modified.

要验证kubectl是否指向正确的集群,我们需要检查节点:

> kubectl get nodes
NAME                        STATUS   ROLES    AGE     VERSION
kind-1.18.2-control-plane   Ready    master   8m20s   v1.18.2

删除某个集群

要删除一个特定的群集,可以在 --name 参数中把集群名称传递给删除命令。

命令为: kind delete cluster --name kind

> kind delete cluster --name kind
Deleting cluster "kind" ...

删除所有集群

如果你想一次性删除所有集群,请执行:

kind delete clusters –all

> kind delete clusters --all
Deleted clusters: ["kind-1.15.6"]

kind的优势是什么?

kind(Kubernetes in Docker)是一个基于Docker构建的Kubernetes集群的工具。它经过CNCF认证,并且支持多节点集群,包括高可用集群。并且支持Linux、macOS以及Windows操作系统,操作简单,学习成本低,非常适合用来在本地搭建基于Kubernetes的开发/测试环境。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK