インフラエンジニアbacchiのわかったことまとめ

bacchi.me

WSL 上でkind を使用した Kubernetes 環境を構築する(Docker Desktop 回避版)

ライセンスの都合で Docker Desktop が使えないので、nerdctl を用いて Kubernetes 環境を構築した際のメモです。

事前準備

パッケージをアップデートします。

sudo apt update && sudo apt upgrade -y

cgroup v2が有効になっているか確認します。

stat -fc %T /sys/fs/cgroup/

実行結果

bacchi@PC001:~$ stat -fc %T /sys/fs/cgroup/
tmpfs

cgroup2fs が表示されない場合、cgroup v2 が有効にななっていないので、.wslconfig を作成して cgroup v2 が有効になるように Kernel パラメータを設定します。

echo -e "[wsl2]\nkernelCommandLine = cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1" | tee .wslconfig

実行結果

bacchi@PC001:~$ echo -e "[wsl2]\nkernelCommandLine = cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1" | tee .wslconfig
[wsl2]
kernelCommandLine = cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1

設定を反映するために WSL から一旦抜けます。

exit

WSL の OS を停止する

wsl --shutdown

再度、WSL にログインします。

wsl --distribution Ubuntu-24.04

cgroup v2 が有効になっているか、再度確認ます。

stat -fc %T /sys/fs/cgroup/

実行結果

bacchi@PC001:~$ stat -fc %T /sys/fs/cgroup/
cgroup2fs

これで事前準備は完了です。

Go Lang と依存関係のパッケージのインストール

Kind をインストールする際の要件に go 1.17 以上と記載があったため Go 言語をインストールします。

apt でインストールできるバージョンが 1.22 なので apt でインストールします。

また、uidmap, iptables, rootlesskit, containerd のパッケージが必要なのでインストールします。

sudo apt install uidmap iptables rootlesskit containerd containernetworking-plugins golang -y
go version

実行結果

bacchi@PC001:~$ go version
go version go1.22.2 linux/amd64

nerdctl のインストール

kind のインストール条件に、docker、podman または nerdctl の記載があるため、今回は nerdctl をインストールします。

今回は nerdctl の nerdctl-full バイナリファイルをダウンロードし、rootless でインストールしていきます。

mkdir -p ~/.local/bin
wget https://github.com/containerd/nerdctl/releases/download/v2.2.1/nerdctl-2.2.1-linux-amd64.tar.gz
sudo tar Cxzvvf ~/.local/bin nerdctl-2.2.1-linux-amd64.tar.gz
export PATH=~/.local/bin:$PATH
cd ~/.local/bin
containerd-rootless-setuptool.sh install

インストールが完了したら、nerdctl を使用して hello-world コンテナを実行できるか確認します。

Hello from Docker! が表示されたら OK です。

nerdctl run hello-world

実行結果

bacchi@PC001:~/.local/bin$ nerdctl run docker.io/hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

kubectlのインストール

kubectl をインストールします。

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

インストールが完了したら、バージョンを確認します。

kubectl version --client

実行結果

bacchi@PC001:~$ kubectl version --client
Client Version: v1.35.0
Kustomize Version: v5.7.1

kind のインストール

kind をインストールします。

[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.31.0/kind-$(uname)-amd64
sudo mv ./kind /usr/local/bin/kind
chmod +x /usr/local/bin/kind

インストールが完了したら、バージョンを確認します。

kind --version

実行結果

bacchi@PC001:~$ kind --version
kind version 0.31.0

動作確認

kindコマンドでクラスタを構築する

以下のコマンドを実行し、クラスタを作成します。

kind create cluster

実行結果

bacchi@PC001:~$ kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.35.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

Have a nice day! 👋

ノードを確認する

kind get nodes コマンドでノードを確認します。

kind get nodes

実行結果

bacchi@PC001:~$ kind get nodes
kind-control-plane

kubectl get nodes コマンドでノードを確認します。

kubectl get nodes

実行結果

bacchi@PC001:~$ kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
kind-control-plane   Ready    control-plane   84s   v1.35.0

コンテナの確認

nerdctl ps コマンドでコンテナを確認します。

nodectl ps

実行結果

bacchi@PC001:~$ nerdctl ps
CONTAINER ID    IMAGE                                                                                                     COMMAND                   CREATED          STATUS    PORTS                        NAMES
c59b474113ab    docker.io/kindest/node:v1.35.0@sha256:452d707d4862f52530247495d180205e029056831160e22870e37e3f6c1ac31f    "/usr/local/bin/entr…"    2 minutes ago    Up        127.0.0.1:44003->6443/tcp    kind-control-plane
  • B!