Closed9
Kind の導入

Kindとは、Dockerコンテナをノードとして使用して、ローカルのKubernetesクラスターを実行するためのツールのことです。

kindのインストール
- WSL2(Ubuntu 22.04)上にインストールする
- Docker CEをインストール済み
以下のコマンドを実行する。
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
# For ARM64
[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-arm64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
kubectlのインストール
sudo snap install kubectl --classic
補完機能を使えるようにする
echo 'source <(kubectl completion bash)' >> ~/.bashrc

クラスタの作成
以下のコマンドを実行し、クラスタを作成する
kind create cluster
名前(--name)を指定しないでクラスタを作成すると、デフォルトではkindとクラスタ名が付けられる。
作成されたクラスタの情報は以下のように参照できる。
kindで作成されたクラスタはcontextの名前にはkind-
のプレフィックスがつく。
$ kubectl cluster-info --context kind-kind
Kubernetes control plane is running at https://127.0.0.1:41695
CoreDNS is running at https://127.0.0.1:41695/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

クラスタの削除
kinde delete cluster

簡易的なクラスタシステムの作成
kindクラスタの作成
以下のようなyamlファイルを作成する
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
以下のコマンドを実行し、クラスタを作成する
kind create cluster --config kind.yaml --name kube-guide
kubectlで、Contextを変更する
kubectl config use-context kind-kube-guide

Podを作成・削除
以下のyamlファイルを作成する。
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx:1.25.1
以下のコマンドを実行してcreateする。
kubectl create -f nginx-pod.yaml
Podの作成結果を確認する。
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 90s
Podを削除する。
kubectl delete -f nginx-pod.yaml
作成したPodを変更したい場合はyamlを編集してapply
サブコマンドを実行する。createの時もapplyを実行した方がいいみたい。
kubectl apply -f nginx-pod.yaml

ポートフォワーディング
デバッグ時等の一時的にPodのポートに転送する。
kubectl apply -f nginx-pod.yaml
kubectl port-forward nginx-pod 8080:80
別のコンソールから8080にアクセスする。
$ curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

ログの確認
$ kubectl logs nginx-pod
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/07/02 01:59:32 [notice] 1#1: using the "epoll" event method
2023/07/02 01:59:32 [notice] 1#1: nginx/1.25.1
2023/07/02 01:59:32 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2023/07/02 01:59:32 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2
2023/07/02 01:59:32 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/07/02 01:59:32 [notice] 1#1: start worker processes
2023/07/02 01:59:32 [notice] 1#1: start worker process 32
2023/07/02 01:59:32 [notice] 1#1: start worker process 33
2023/07/02 01:59:32 [notice] 1#1: start worker process 34
2023/07/02 01:59:32 [notice] 1#1: start worker process 35
2023/07/02 01:59:32 [notice] 1#1: start worker process 36
2023/07/02 01:59:32 [notice] 1#1: start worker process 37
2023/07/02 01:59:32 [notice] 1#1: start worker process 38
2023/07/02 01:59:32 [notice] 1#1: start worker process 39
2023/07/02 01:59:32 [notice] 1#1: start worker process 40
2023/07/02 01:59:32 [notice] 1#1: start worker process 41
2023/07/02 01:59:32 [notice] 1#1: start worker process 42
2023/07/02 01:59:32 [notice] 1#1: start worker process 43
2023/07/02 01:59:32 [notice] 1#1: start worker process 44
2023/07/02 01:59:32 [notice] 1#1: start worker process 45
2023/07/02 01:59:32 [notice] 1#1: start worker process 46
2023/07/02 01:59:32 [notice] 1#1: start worker process 47
2023/07/02 01:59:32 [notice] 1#1: start worker process 48
2023/07/02 01:59:32 [notice] 1#1: start worker process 49
2023/07/02 01:59:32 [notice] 1#1: start worker process 50
2023/07/02 01:59:32 [notice] 1#1: start worker process 51
127.0.0.1 - - [02/Jul/2023:02:00:15 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
前項でのアクセス・ログが出力されている。

NodePortへの接続
このスクラップは2023/10/23にクローズされました