LoginSignup
2
5

More than 1 year has passed since last update.

WinK(WindowsでKubernetes on WSL2) ~環境構築編~

Last updated at Posted at 2022-06-05

WSL2 として利用可能な Ubuntu22.04 では systemd が利用できるようになったようなので、Windows 10 上で Docker Desktop for Windows を使わないで Kubernetes 環境を構築する方法を記載する。
これを勝手に WinK と名付ける。

これまでにもいくつか WSL2 上で Kubernetes 環境を構築する方法を記載している記事はあったが、どれも私の環境にはマッチしなかった。今回私の Windows 環境で Kubernetes を立ち上げられるまでを記しておく。
ちなみに環境は、Windows10、また Ubuntu22.04 を WSL2 上で利用する。

※プロンプトの説明
 PS>:Windows上のPowerShellのプロンプトを指す。
 $:WSLとして起動しているLinuxのプロンプトを指す。

0.Windwos Store から Ubuntu22.04 をインストールしておく。
また、Docker Desktop for Windows を終了しておく。

1.wslの中身確認

PS > wsl --list --verbose
	  NAME                   STATE           VERSION
	* Debian                 Stopped         2
	  Ubuntu-20.04           Stopped         2
	  Distrod                Stopped         2
	  docker-desktop-data    Stopped         2

2.Ubuntu-22.04起動
アカウント設定や初期設定、最後に以下を実施

$ sudo apt update && sudo apt upgrade

3.Ubuntu-22.04 をデフォルトへ

PS > wsl --list --verbose
	  NAME                   STATE           VERSION
	* Debian                 Stopped         2
	  Ubuntu-22.04           Running         2
	  Ubuntu-20.04           Stopped         2
	  Distrod                Stopped         2
	  docker-desktop-data    Stopped         2
PS > wsl --set-default Ubuntu-22.04
PS > wsl --list --verbose
	  NAME                   STATE           VERSION
	* Ubuntu-22.04           Running         2
	  Debian                 Stopped         2
	  Ubuntu-20.04           Stopped         2
	  Distrod                Stopped         2
	  docker-desktop-data    Stopped         2

4.Ubuntu-22.04 を起動

PS > wsl -d ubuntu-22.04

5.repository 構成
必要なライブラリをインストールし、Dockerのoffcial なGPGキーを登録。

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

6.WSLへdockerインストール
6.1.パッケージのインストール

$ sudo apt-get install ca-certificates curl gnupg lsb-release

6.2.Dockerのofficial_GPG_key追加

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

6.3.Dockerリポジトリ情報の登録

$echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

6.4. パッケージ一覧再更新

$ sudo apt update

6.5. Docker Engine のインストール

	sudo apt-get install docker-ce docker-ce-cli containerd.io

6.6. Docker Group にユーザを所属させる

6.7. Docker service 起動設定

cat <<EOF >> ~/.bashrc
# For Docker service start
sudo /etc/init.d/docker start
EOF

6.8. 一旦ログアウトし、すぐログイン

$ exit
PS > wsl -d Ubuntu-22.04

6.9.Dockerがネットワークの初期化に失敗して起動しないためiptables-legacyを使用するように変更

$ sudo update-alternatives --config iptables   → 問い合わせでは1を指定
$ touch /etc/fstab
$ sudo touch /etc/fstab
$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
$ sudo service docker start
$ sudo service docker status

7.kubectl インストール

$ curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
$ chmod +x kubectl
$ sudo mv ./kubectl /usr/local/bin/kubectl
$ kubectl version --client
  WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
  Client Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.1", GitCommit:"3ddd0f45aa91e2f30c70734b175631bec5b5825a", GitTreeState:"clean", BuildDate:"2022-05-24T12:26:19Z", GoVersion:"go1.18.2", Compiler:"gc", Platform:"linux/amd64"}
  Kustomize Version: v4.5.4

8.minikube インストール

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
$ sudo usermod -aG docker $USER && newgrp docker

9.minikube 開始

$ minikube start --driver=docker

次回から、この環境を使ってK8sアプリを構築してみる。

参考記事
WSL2 に Docker Engine をインストール(Docker Desktop なし)
今更ながらWSL2にminikube入れてみた

2
5
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
2
5