LoginSignup
2
0

More than 3 years have passed since last update.

VirtualBox 上の ubuntu18.04LTS で kind ( Kubernetes IN Docker ) を試す

Last updated at Posted at 2019-08-22

家でもKubernetesを少し触りたいなと思い、最近流行りのkindを試す。
( この試行の前にWindows上にDocker ToolBoxを入れてそれでkindを試そうとしたけどMSYS2環境にGolangをセットアップするところで挫折したのは内緒だ。)

TL;DR

  • Ubuntu Desktop 18.04.3 LTS なら関連OSSはOSのPKGをインストールすればOK
  • 公式ドキュメント通り実行すれば基本的には問題なくKubernets環境を構築できる。
  • 一般ユーザでsudo使って試したけど、sudo suでrootになって進めた方が多分いい。

1. 環境

1.1 物理環境

  • OS: Windows 10 Home
  • 仮想化ソフトウェア: VirtualBox 6.0.10

1.2 仮想環境

  • OS: Ubuntu Desktop 18.04.3 LTS
  • CPU: 1
  • メモリ: 2GB
  • Disk: 30GB

2. インフラ構築

2.1 VirtualBoxのインストール

VirtualBoxのダウンロードサイトからインストーラをダウンロードしてexeを叩けばインストールできます。

2.2 Ubuntuのインストール

  1. UbuntuのダウンロードサイトからISOイメージをダウンロード
  2. VirtualBoxで「新規」を選択し、仮想マシン(タイプ:Linux, バージョン:Ubuntu(64-bit))を作成します
  3. 仮想マシンを起動してダウンロードしたISOイメージファイルを利用してインストールします。
  4. インストール後の最初のログインでたぶんパッケージの更新するか聞かれるので更新しておきます。
  5. VirtualBoxのGuest Additionもインストールしておきます。

3. 関連OSSのセットアップ

3.1 Dockerのインストール

"IN Docker"なのでもちろんDockerをインストールする必要があります。
Dockerがインストールされているのかなと試しに叩ていてみたらインストール方法を教えてもらえました。

$ docker

Command 'docker' not found, but can be installed with:

sudo snap install docker     # version 18.06.1-ce, or
sudo apt  install docker.io

See 'snap info docker' for additional versions.

snapとaptの二つの方法が提示されましたが、Webを検索するとsnapでインストールできる場合はsnapでインストールした方がいいという記事を見つけたので、今回はsnapでインストールです。

$ sudo snap install docker
docker 18.06.1-ce from Canonical✓ installed

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3.2 Go言語のインストール

Go言語も同様にgoコマンドを叩いてインストール方法を教えてもらいます。

$ go

Command 'go' not found, but can be installed with:

sudo snap install go         # version 1.12.9, or
sudo apt  install golang-go
sudo apt  install gccgo-go 

See 'snap info go' for additional versions.

言われたとおりに叩いたらエラー発生。

$ sudo snap install go 
error: This revision of snap "go" was published using classic confinement and
       thus may perform arbitrary system changes outside of the security
       sandbox that snaps are usually confined to, which may put your system
       at risk.

       If you understand and want to proceed repeat the command including
       --classic.

言われた通り--classicオプションを付けてリトライしたら、今度はインストール成功。

$ sudo snap install --classic go 
go 1.12.9 from Michael Hudson-Doyle (mwhudson) installed

3.3 kubectlのインストール

これまで同様にインストール。

$ kubectl

Command 'kubectl' not found, but can be installed with:

sudo snap install kubectl


$ sudo snap install kubectl
error: This revision of snap "kubectl" was published using classic confinement
       and thus may perform arbitrary system changes outside of the security
       sandbox that snaps are usually confined to, which may put your system
       at risk.

       If you understand and want to proceed repeat the command including
       --classic.

$ sudo snap install --classic kubectl
kubectl 1.15.2 from Canonical✓ installed

3.4 Gitのインストール

Gitはsnapに無いようなのでaptでインストール

$ git

Command 'git' not found, but can be installed with:

sudo apt install git

$ sudo apt install git
パッケージリストを読み込んでいます... 完了
<省略>
git (1:2.17.1-1ubuntu0.4) を設定しています ...

4. kindを試す

4.1 kindのインストール

https://github.com/kubernetes-sigs/kind に記載されている通り叩いてインストール

$ GO111MODULE="on" go get sigs.k8s.io/kind@v0.4.0
go: finding sigs.k8s.io/kind v0.4.0
<省略>
go: extracting golang.org/x/text v0.3.0

4.2 Kubernetesクラスタの作成

root権限が無いとエラーで失敗します(エラーメッセージが分かりづらい)。

$ $(go env GOPATH)/bin/kind create cluster
Error: could not list clusters: failed to list nodes: exit status 1

sudo で root 権限で実行すると特に問題なくクラスタ作成に成功。

$ sudo $(go env GOPATH)/bin/kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.15.0) 🖼 
 ✓ Preparing nodes 📦 
 ✓ Creating kubeadm config 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Cluster creation complete. You can now use the cluster with:

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info

4.3 作成した Kubernetes クラスタの確認

kindコマンド実行結果のKUBECONFIG環境変数の設定方法はkindコマンドにPATHが通っている前提になっているので、PATHを通してから設定。

$ export PATH=$PATH:$(go env GOPATH)/bin
$ export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"

kubectlでクラスタ情報を参照しようしたら権限が無く失敗・・・。

$ kubectl cluster-info
error: Error loading config file "/home/user/.kube/kind-config-kind": open /home/user/.kube/kind-config-kind: permission denied

kindコマンドをsudoで実行したのでroot権限になってしまったんだなと思い、sudo付き実行したら、今度は環境変数が引き継がれないみたいで失敗・・・。

$ sudo kubectl cluster-info
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
The connection to the server localhost:8080 was refused - did you specify the right host or port?

--kubeconfigオプションでkubeconfigファイルを指定すれば無事成功。

$ sudo kubectl cluster-info --kubeconfig=$KUBECONFIG
Kubernetes master is running at https://127.0.0.1:43147
KubeDNS is running at https://127.0.0.1:43147/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Podの配備とか試してみようかなと思ったけど力尽きたのでまずはここまで。

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