1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSL上でkindを使用したKubernetes環境構築

Last updated at Posted at 2025-01-18
  • 概要

    • Windows 10 HomeでWSL2環境にてMulti-nodeのKubernetes検証用の環境構築する
    • kubectlコマンドが使えるようにする
    • 必要なモジュールはバイナリファイルを使用する

前提条件

  • WSLが有効になっていること

事前準備

  • 使用しているWSLのLinux環境の確認する
cat /etc/os-release
  • 実行結果
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
  • パッケージのアップデートをする
sudo apt update && sudo apt upgrade -y
  • cgroup v2が有効になっているか確認する
stat -fc %T /sys/fs/cgroup/

実行結果

tmpfs
  • .wslconfigを作成してcgroup v2が有効になるようにKernelパラメータを設定する
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
  • cgroup v2が有効になっているか、再度確認する
stat -fc %T /sys/fs/cgroup/

実行結果

cgroup2fs

Go言語をインストール

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

  • 作業ディレクトリを作成し、作業ディレクトリに移動する

cd ~ && mkdir work && cd work/
  • 2025年1月18日現在、Go言語の最新版が1.23.5をインストールする

  • Go言語バイナリファイルをダウンロードする

wget https://go.dev/dl/go1.23.5.linux-amd64.tar.gz
  • ダウンロードしたファイルが破損等していないか確認する
sha256sum go1.23.5.linux-amd64.tar.gz
  • SHA256 Checksumの結果をGo言語のダウンロードページに記載されている値と比較する

実行結果

cbcad4a6482107c7c7926df1608106c189417163428200ce357695cc7e01d091  go1.23.5.linux-amd64.tar.gz
  • Go言語のアーカイブファイルを/usr/local/src/に展開する
sudo tar Cxzvf /usr/local/src/ go1.23.5.linux-amd64.tar.gz
  • Go言語のコマンドを使用するためにPATHを通す
echo 'export PATH=$PATH:/usr/local/src/go/bin' | tee -a ~/.bashrc
  • .bashrcに設定が反映されているか確認する
tail -n1 ~/.bashrc
  • 現在使用しているシェルに環境変数を反映する
source ~/.bashrc
  • インストールされているGo言語のバージョン確認する
go version

実行結果

go version go1.23.5 linux/amd64

nerdctlのインストール

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

  • 今回はnerdctlのnerdctl-fullバイナリファイルをダウンロードする

wget https://github.com/containerd/nerdctl/releases/download/v2.0.2/nerdctl-full-2.0.2-linux-amd64.tar.gz
  • SHA256SUMSを確認するファイルをダウンロードする
wget https://github.com/containerd/nerdctl/releases/download/v2.0.2/SHA256SUMS
  • sha256sumコマンドを使用してダウンロードしたファイルが破損していないか確認する
echo "$(grep nerdctl-full-2.0.2-linux-amd64.tar.gz SHA256SUMS)" | sha256sum --check

実行結果

nerdctl-full-2.0.2-linux-amd64.tar.gz: OK
  • nerdctlに記載しているあるインストール手順でアーカイブファイルを展開する
sudo tar Cxzvvf /usr/local nerdctl-full-2.0.2-linux-amd64.tar.gz
  • 今回はrootlessでインストールする
containerd-rootless-setuptool.sh install
  • "newuidmap"パッケージがないとインストールが失敗した
[INFO] Checking RootlessKit functionality
[rootlesskit:parent] error: failed to setup UID/GID map: newuidmap 513 [0 1000 1 1 100000 65536] failed: : exec: "newuidmap": executable file not found in $PATH
[ERROR] RootlessKit failed, see the error messages and https://rootlesscontaine.rs/getting-started/common/ .
  • Dockerの公式ページを確認したところ、uidmapパッケージが必要と確認できたのでuidmapをインストールする
sudo apt install uidmap -y
  • 再度、インストールを実行、スクリプトが正常に終了したことを確認する。実行結果は長いため割愛する
containerd-rootless-setuptool.sh install
  • nerdctlを使用してhello-worldコンテナを実行できるか確認する
nerdctl run hello-world
  • iptables関連のファイルがないため実行失敗した
docker.io/library/hello-world:latest:                                             resolved       |++++++++++++++++++++++++++++++++++++++|
index-sha256:1b7a37f2a0e26e55ba2916e0c53bfbe60d9bd43e390e31aacd25cb3581ed74e6:    done           |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:e2fc4e5012d16e7fe466f5291c476431beaa1f9b90a5c2125b493ed28e2aba57: done           |++++++++++++++++++++++++++++++++++++++|
config-sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a:   done           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c1ec31eb59444d78df06a974d155e597c894ab4cda84f08294145e845394988e:    done           |++++++++++++++++++++++++++++++++++++++|
elapsed: 4.0 s                                                                    total:  12.5 K (3.1 KiB/s)

FATA[0004] failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error running createRuntime hook #0: exit status 1, stdout: , stderr: time="2025-01-18T16:45:01+09:00" level=fatal msg="failed to call cni.Setup: plugin type=\"bridge\" failed (add): failed to locate iptables: exec: \"iptables\": executable file not found in $PATH"
  • iptablesパッケージをインストールする
sudo apt install iptables -y
  • containerd.serviceの自動起動確認する
sudo systemctl is-enabled containerd.service
  • containerd.serviceを自動起動するように設定変更する
sudo systemctl enable containerd.service
  • 再度、実行して"Hello from Docker!"が表示されることを確認する
nerdctl run hello-world

kubectlのインストール

  • kubectlをダウンロードする
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  • SHA256SUMSを確認するファイルをダウンロードする
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
  • ダウンロードしたファイルが破損等していないか確認する
echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check

実行結果

kubectl: OK
  • kubectlをインストールする
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
  • kubectlのバージョンを確認する
kubectl version --client

実行結果

Client Version: v1.32.1
Kustomize Version: v5.5.0

kindのインストール

  • kindをダウンロードする
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.26.0/kind-linux-amd64
  • SHA256SUMSを確認するファイルをダウンロードする
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.26.0/kind-linux-amd64.sha256sum
  • ファイルが破損していないか確認する
echo "$(cat kind-linux-amd64.sha256sum)" | sha256sum --check

実行結果

kind-linux-amd64: OK
  • kindバイナリファイルに実行権限を付与する
chmod +x ./kind-linux-amd64
  • kindバイナリファイルを/usr/local/bin/に移動する
sudo mv ./kind-linux-amd64 /usr/local/bin/kind
  • kindのバージョンを確認する
kind --version

実行結果

kind version 0.26.0
  • kindコマンドでクラスタを構築する
kind create cluster

実行結果

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

Not sure what to do next? 😅  Check out https://kind.sigs.k8s.io/docs/user/quick-start/
  • kindコマンドでノードを確認する
kind get nodes

実行結果

kind-control-plane
  • kubectlコマンドでノードを確認する
kubectl get nodes

実行結果

NAME                 STATUS   ROLES           AGE    VERSION
kind-control-plane   Ready    control-plane   2m5s   v1.32.0
  • nerdctl psコマンドで確認する
nerdctl ps

実行結果

CONTAINER ID    IMAGE                                                                                                     COMMAND                   CREATED               STATUS    PORTS                        NAMES
d016a64b876d    docker.io/kindest/node:v1.32.0@sha256:c48c62eac5da28cdadcf560d1d8616cfa6783b58f0d94cf63ad1bf49600cb027    "/usr/local/bin/entr…"    About a minute ago    Up        127.0.0.1:41419->6443/tcp    kind-control-plane

参考URL

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?