通过使用kind(Kubernetes in Docker),我们可以搭建Kubernetes集群

kind是什么意思

使用Kubernetes在Docker中的简称,通过启动多个Docker容器并将这些容器用作Kubernetes节点,可以构建一个多节点的Kubernetes集群。

创建环境

image.png

环境版本的总结

[root@xxxxxxxx ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

[root@xxxxxxxx ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.1
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        831ebea
 Built:             Tue Dec 15 04:37:17 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.1
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       f001486
  Built:            Tue Dec 15 04:35:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

[root@xxxxxxxx ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.2", GitCommit:"52c56ce7a8272c798dbc29846288d7cd9fbae032", GitTreeState:"clean", BuildDate:"2020-06-03T04:00:21Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

[root@xxxxxxxx ~]# kind version
kind v0.7.0 go1.13.6 linux/amd64

1. 初始設定、安裝

1-1. VPS初始设定(如有必要基于安全等理由)

・创建用户
・配置sshd相关设置(禁止root登录等)
・配置证书
等等…

1-2. 安装Docker

请根据以下参考链接,用中文进行重新表述(仅提供一种选择):https://qiita.com/ymasaoka/items/b6c3ffea060bcd237478

1-3. 安装kubectl [参考URL](https://tech-lab.sios.jp/archives/19226)

chmod +x ./kubectl
将./kubectl移动到/usr/local/bin/目录下
列出/usr/local/bin/目录下kubectl的详细信息
查看kubectl的版本
※忽略以下错误
连接到服务器localhost:8080被拒绝 – 是否指定了正确的主机或端口?

1-4. 安裝Kind [※參考網址](https://tech-lab.sios.jp/archives/19226)

使用curl命令从以下网址下载kind二进制文件:https://github.com/kubernetes-sigs/kind/releases/download/v0.7.0/kind-$(uname)-amd64。
将下载的kind文件赋予执行权限,命令为chmod +x ./kind。
将kind文件移动到/usr/local/bin/目录下,命令为mv ./kind /usr/local/bin/。
运行ls -l /usr/local/bin/kind来检查kind版本。

1. 创建一个只含有一个节点(主节点)的Kubernetes集群。

执行kubernetes集群创建命令。

[root@xxxxxxxx ~]# kind create cluster

Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.17.0) ? 
 ✓ 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! ?

确认

[root@xxxxxxxx ~]# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
9cc673f1e968   kindest/node:v1.17.0   "/usr/local/bin/entr…"   2 minutes ago   Up 2 minutes   127.0.0.1:49157->6443/tcp   kind-control-plane

2-3. 上下文设置

Note: This translation may vary depending on the specific context or meaning in the original text.

通过设置和指定上下文,可以指定kubectl命令的目标Kubernetes集群。

[root@xxxxxxxx ~]# kubectl cluster-info --context kind-kind
Kubernetes master is running at https://127.0.0.1:49157
KubeDNS is running at https://127.0.0.1:49157/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

2-4. 确认Kubernetes集群的启动

[root@xxxxxxxx ~]# kubectl get node
NAME                 STATUS   ROLES    AGE     VERSION
kind-control-plane   Ready    master   5m58s   v1.17.0

删除 Kubernetes 集群。

[root@xxxxxxxx ~]# kind delete cluster
Deleting cluster "kind" ...
[root@xxxxxxxx ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@xxxxxxxx ~]# kubectl get node
The connection to the server localhost:8080 was refused - did you specify the right host or port?

创建一个拥有多个节点(1个主节点,1个工作节点)的Kubernetes集群。

创建一个名为”yaml”的文件。

如果api版本和图像版本不匹配,将会失败。

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  image: kindest/node:v1.18.2
- role: worker
  image: kindest/node:v1.18.2

执行 kubernetes 集群创建命令时,3 减去 2。

[root@xxxxxxxx ~]# kind create cluster --config kind.yaml --name kindclluster
Creating cluster "kindclluster" ...
 ✓ Ensuring node image (kindest/node:v1.18.2) ?
 ✓ Preparing nodes ? ?  
 ✓ Writing configuration ? 
 ✓ Starting control-plane ?️ 
 ✓ Installing CNI ? 
 ✓ Installing StorageClass ? 
 ✓ Joining worker nodes ? 
Set kubectl context to "kind-kindclluster"
You can now use your cluster with:

kubectl cluster-info --context kind-kindclluster

Have a nice day! ?

确认

[root@xxxxxxxx ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED              STATUS              PORTS                       NAMES
bc55874ac1ff   kindest/node:v1.18.2   "/usr/local/bin/entr…"   About a minute ago   Up About a minute                               kindclluster-worker
57fd25bbbeba   kindest/node:v1.18.2   "/usr/local/bin/entr…"   About a minute ago   Up About a minute   127.0.0.1:49158->6443/tcp   kindclluster-control-plane

[root@xxxxxxxx ~]# kubectl get nodes
NAME                         STATUS   ROLES    AGE   VERSION
kindclluster-control-plane   Ready    master   72s   v1.18.2
kindclluster-worker          Ready    <none>   37s   v1.18.2

3-4. 删除kubernetes集群

[root@xxxxxxxx ~]# kind delete cluster
Deleting cluster "kind" ...
[root@xxxxxxxx ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@xxxxxxxx ~]# kubectl get node
The connection to the server localhost:8080 was refused - did you specify the right host or port?

4. 部署nginxpod

kubectl create deployment nginx --image=nginx
kubectl create service nodeport nginx --tcp=8080:80 service/nginx created
kubectl port-forward --address localhost svc/nginx 8080:8080 
curl 127.0.0.1:8080

以上 (yǐ :以上

广告
将在 10 秒后关闭
bannerAds