kind(Kubernetes in Docker) 使ってkubernetesクラスタ構築


kindとは

Kubernetes in Dockerの略でDockerコンテナを複数個起動し、そのコンテナをKubernetesNodeとして利用することで、複数台構成のKubernetesクラスタを構築する。

作成する環境

以下の環境(Master1台、Worker1台のKubernetesClusterを1台)を作成する。
※VPS契約しなくてもMacやWindowsのローカルでできると思われます。

使用した環境のバージョン纏め

[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ログイン禁止等)
・証明書配置
etc...

1−2. Docker インストール

(※参考URL)[https://qiita.com/ymasaoka/items/b6c3ffea060bcd237478]

1−3. kubectl のインストール [※参考URL](https://tech-lab.sios.jp/archives/19226)

chmod +x ./kubectl
mv ./kubectl /usr/local/bin/
ls -l /usr/local/bin/kubectl
kubectl version
※以下エラー出るが無視
 The connection to the server localhost:8080 was refused - did you specify the right host or port?

1−4. kind のインストール [※参考URL](https://tech-lab.sios.jp/archives/19226)

curl -Lo ./kind "https://github.com/kubernetes-sigs/kind/releases/download/v0.7.0/kind-$(uname)-amd64"
chmod +x ./kind
mv ./kind /usr/local/bin/
ls -l /usr/local/bin/kind
kind version

2. 単一ノード (Master 1台構成)のkubernetesクラスタ作成

2−1. 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! 😊

2−2. 確認

[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.コンテキスト設定

※コンテキストを設定し指定することで、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

2−5. 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?

3. 複数ノード (Master 1台、Worker1台構成)のkubernetesクラスタ作成

3−1. yamlファイル作成

※apiVersion,imageのバージョンが合っていないと失敗する

kind.yaml
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

3−2. kubernetesクラスタ作成コマンド実行

[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! 👋

3−3. 確認

[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

以上