kind单机多node集群操作

638 阅读3分钟

单机环境:Ubuntu 20.04 安装 kind

Kind 是 Kubernetes In Docker 的缩写,顾名思义是使用 Docker 容器作为 Node 并将 Kubernetes 部署至其中的一个工具。官方文档中也把 Kind 作为一种本地集群搭建的工具进行推荐。

  1. 安装kubectl,这个不介绍了,之前的文件写的很详细了,不知道的可以查看之前的文章或者自行的百度 2.安装kind
# 安装不上的可以自行百度,方法很多,这里使用go环境安装
export GO111MODULE=”on” 
go get sigs.k8s.io/kind@v0.8.1
# kind 命令
kind create cluster
# 将通过拉取最新的 Kubernetes 节点v1.19.1来创建一个 Kubernetes 集群。集群名称默认为 kind,也可以指定版本
kind create cluster --image kindest/node:v1.19.1
# 还可以根据自己的版本yaml文件生成集群和指定名字
kind create cluster --config mykind.yaml --name mykind
# 生成后查看集群
kind get clusters
# 列出可用的集群
kubectl config get-contexts

#CURRENT   NAME          CLUSTER       AUTHINFO      NAMESPACE
#*         kind-mykind   kind-mykind   kind-mykind

#设置切换集群,默认前缀是kind
kubectl config set-context kind-clusterName
# 查看集群配置切换是否成功
kubectl get nodes
# 
kind get kubeconfig --name="mykind"


删除集群
kind delete cluster --name mykind
# 删除所有的集群
kind delete clusters --all
# 查看节点,我配置的是一主俩从
kubectl get nodes

NAME                   STATUS   ROLES    AGE     VERSION
mykind-control-plane   Ready    master   3m11s   v1.19.1
mykind-worker          Ready    <none>   2m39s   v1.19.1
mykind-worker2         Ready    <none>   2m39s   v1.19.1

# 
  • build:用来从 Kubernetes 源代码构建一个新的镜像。
  • create:创建一个 Kubernetes 集群。
  • delete:删除一个 Kubernetes 集群。
  • get:可用来查看当前集群、节点信息以及 Kubectl 配置文件的地址。
  • load:从宿主机Kubernetes 节点内导入镜像。
  1. 多节点配置
# 一共3个节点,1个主节点,2个从节点。默认TCP,可以配置为udp
# 主机默认开放的是0-65535,集群可以使用的是30000-32700+,所以就是把端口映射到不同的区段当做一个节点,然后使用:kind create cluster --config mykind.yaml --name clusterName
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30100
    hostPort: 30100
    listenAddress: "0.0.0.0"
  - containerPort: 30101
    hostPort: 30101
    listenAddress: "0.0.0.0"
  - containerPort: 30102
    hostPort: 30102
    listenAddress: "0.0.0.0"
  - containerPort: 30103
    hostPort: 30103
    listenAddress: "0.0.0.0"
  - containerPort: 30104
    hostPort: 30104
    listenAddress: "0.0.0.0"
  - containerPort: 30105
    hostPort: 30105
    listenAddress: "0.0.0.0"
  - containerPort: 30106
    hostPort: 30106
    listenAddress: "0.0.0.0"
  - containerPort: 31107
    hostPort: 31107
    listenAddress: "0.0.0.0"
  - containerPort: 30108
    hostPort: 30108
    listenAddress: "0.0.0.0"
  - containerPort: 30109
    hostPort: 30109
    listenAddress: "0.0.0.0"    
- role: worker
  extraPortMappings:
  - containerPort: 30100
    hostPort: 30200
    listenAddress: "0.0.0.0"
  - containerPort: 30101
    hostPort: 30201
    listenAddress: "0.0.0.0"
  - containerPort: 30102
    hostPort: 30202
    listenAddress: "0.0.0.0"
  - containerPort: 30103
    hostPort: 30203
    listenAddress: "0.0.0.0"
  - containerPort: 30104
    hostPort: 30204
    listenAddress: "0.0.0.0"
  - containerPort: 30105
    hostPort: 30205
    listenAddress: "0.0.0.0"
  - containerPort: 30106
    hostPort: 30206
    listenAddress: "0.0.0.0"
  - containerPort: 30107
    hostPort: 30207
    listenAddress: "0.0.0.0"
  - containerPort: 30108
    hostPort: 30208
    listenAddress: "0.0.0.0"
  - containerPort: 30109
    hostPort: 30209
    listenAddress: "0.0.0.0"
- role: worker
  extraPortMappings:
  - containerPort: 30100
    hostPort: 30300
    listenAddress: "0.0.0.0"
  - containerPort: 30101
    hostPort: 30301
    listenAddress: "0.0.0.0"
  - containerPort: 30102
    hostPort: 30302
    listenAddress: "0.0.0.0"
  - containerPort: 30103
    hostPort: 30303
    listenAddress: "0.0.0.0"
  - containerPort: 30104
    hostPort: 30304
    listenAddress: "0.0.0.0"
  - containerPort: 30105
    hostPort: 30305
    listenAddress: "0.0.0.0"
  - containerPort: 30106
    hostPort: 30306
    listenAddress: "0.0.0.0"
  - containerPort: 30107
    hostPort: 30307
    listenAddress: "0.0.0.0"
  - containerPort: 30108
    hostPort: 30308
    listenAddress: "0.0.0.0"
  - containerPort: 30109
    hostPort: 30309
    listenAddress: "0.0.0.0"

  1. 查看集群 node
kubectl get nodes
NAME                   STATUS   ROLES    AGE     VERSION
mykind-control-plane   Ready    master   3m11s   v1.19.1
mykind-worker          Ready    <none>   2m39s   v1.19.1
mykind-worker2         Ready    <none>   2m39s   v1.19.1
  1. 集群部署完成