使用 Kind 搭建你的本地 Kubernetes 集群

Kind 是我很喜欢也一直在参与的项目,我计划将 Kind 相关的文章写成一个系列。(flag++) 这是第一篇。node

Kind 介绍

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

安装

二进制安装

Kind 使用 Golang 进行开发,在仓库的 Release 页面,已经上传了构建好的二进制,支持多种操做系统,可直接按需下载进行使用。git

e.g.github

# 下载最新的 0.2.0 版本
wget -O /usr/local/bin/kind https://github.com/kubernetes-sigs/kind/releases/download/0.2.0/kind-linux-amd64 && chmod +x /usr/local/bin/kind
复制代码

经过源码安装

若是你本地已经配置好了 Golang 的开发环境,那你能够直接经过源码进行安装。golang

e.g.docker

go get -u sigs.k8s.io/kind
复制代码

运行完上述命令后,会将 kind 的可执行文件放到 $(go env GOPATH)/bin 文件夹内,你可能须要将此目录加入到 $PATH 中。api

或者也能够先 clone 源代码再经过 go build 进行构建。网络

依赖

  • Kind 的主要功能目前须要有 Docker 环境的支持,可参考 Docker 官方文档进行安装。工具

  • 若是须要操做集群,则须要安装 kubectl 命令行。安装方法可参考官方文档ui

搭建单节点集群

如下的演示均使用最新的代码(即经过源码安装)。

基础用法

搭建单节点集群是 Kind 最基础的功能。

e.g.

master $ kind create cluster --name moelove
Creating cluster "moelove" ...
 ✓ Ensuring node image (kindest/node:v1.13.4) 🖼
 ✓ 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="moelove")"
kubectl cluster-info
复制代码

以上命令中, --name 是可选参数,如不指定,默认建立出来的集群名字为 kind

咱们根据命令执行完的输出进行操做:

master $ export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")"
master $ 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'.
master $ kubectl get nodes
NAME                    STATUS    ROLES     AGE       VERSION
moelove-control-plane   Ready     master    2m        v1.13.4
复制代码

以上命令中,kind get kubeconfig-path --name="moelove" 会返回该指定集群配置文件所在的路径。

能够看到单节点的 Kubernetes 已经搭建成功。

注意

  • 默认状况下,Kind 会先下载 kindest/node:v1.13.4 镜像,该镜像目前托管于 Docker Hub 上,下载时间取决于网络情况。
  • Kind 实际使用 kubeadm 进行集群的建立,对 kubeadm 有所了解的人都知道它默认使用的镜像在国内下载不到,因此须要本身解决网络问题。或者参考下面的方式:

Kind 在建立集群的时候,支持经过 --config 的参数传递配置文件给 Kind,在国内,咱们能够经过使用国内镜像源的方式来加速集群的建立。(这个方法也适用于直接经过 kubeadm 搭建 Kubernetes 集群)

咱们先经过如下命令删除刚才搭建的集群:

master $ kind delete  cluster --name moelove
Deleting cluster "moelove" ...
$KUBECONFIG is still set to use /root/.kube/kind-config-moelove even though that file has been deleted, remember to unset it
复制代码

接下来,将下面的配置内容保存至一个 YAML 文件中,好比名为 kind-config.yaml

kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
kubeadmConfigPatches:
- |  apiVersion: kubeadm.k8s.io/v1beta1
 kind: ClusterConfiguration
 metadata:
 name: config
 networking:
 serviceSubnet: 10.0.0.0/16
 imageRepository: registry.aliyuncs.com/google_containers
 nodeRegistration:
 kubeletExtraArgs:
 pod-infra-container-image: registry.aliyuncs.com/google_containers/pause:3.1
- |  apiVersion: kubeadm.k8s.io/v1beta1
 kind: InitConfiguration
 metadata:
 name: config
 networking:
 serviceSubnet: 10.0.0.0/16
 imageRepository: registry.aliyuncs.com/google_containers
nodes:
- role: control-plane
复制代码

咱们使用该配置文件搭建集群。

master $ kind create cluster --name moelove --config kind.yaml
Creating cluster "moelove" ...
 ✓ Ensuring node image (kindest/node:v1.13.4) 🖼
 ✓ 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="moelove")"
kubectl cluster-info
复制代码

上面经过 --config 将咱们的配置文件传递给 Kind 用于搭建集群,推荐国内用户使用这种方式

搭建高可用集群

Kind 也支持搭建高可用的 K8S 集群,不过只能经过配置文件来实现。能够直接将下面的内容保存至文件中,再将配置文件传递给 Kind 便可。

e.g.

kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
kubeadmConfigPatches:
- |  apiVersion: kubeadm.k8s.io/v1beta1
 kind: ClusterConfiguration
 metadata:
 name: config
 networking:
 serviceSubnet: 10.0.0.0/16
 imageRepository: registry.aliyuncs.com/google_containers
 nodeRegistration:
 kubeletExtraArgs:
 pod-infra-container-image: registry.aliyuncs.com/google_containers/pause:3.1
- |  apiVersion: kubeadm.k8s.io/v1beta1
 kind: InitConfiguration
 metadata:
 name: config
 networking:
 serviceSubnet: 10.0.0.0/16
 imageRepository: registry.aliyuncs.com/google_containers
nodes:
- role: control-plane
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
复制代码

咱们使用如下的命令来搭建高可用的 Kubernetes 集群:

master $ kind create cluster --name moelove-ha --config kind-ha-config.yaml
Creating cluster "moelove-ha" ...
 ✓ Ensuring node image (kindest/node:v1.13.4) 🖼
 ✓ 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="moelove-ha")"
kubectl cluster-info
master $ export KUBECONFIG="$(kind get kubeconfig-path --name="moelove-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'.
复制代码

能够作下简单的验证:

master $ kubectl get nodes
NAME                        STATUS   ROLES    AGE     VERSION
moelove-ha-control-plane    Ready    master   3m42s   v1.13.4
moelove-ha-control-plane2   Ready    master   3m24s   v1.13.4
moelove-ha-control-plane3   Ready    master   2m13s   v1.13.4
moelove-ha-worker           Ready    <none>   96s     v1.13.4
moelove-ha-worker2          Ready    <none>   98s     v1.13.4
moelove-ha-worker3          Ready    <none>   95s     v1.13.4
复制代码

能够看到已经成功建立了多 master 的 Kubernetes 集群。

总结

这是使用 Kind 搭建本地 Kubernetes 集群的第一篇,同时本篇的内容也是《Kubernetes 从上手到实践》第 4 节内容的补充,搭配食用效果更佳 :)


能够经过下面二维码订阅个人文章公众号【MoeLove】

TheMoeLove
相关文章
相关标签/搜索