Kubernetesやるぞ
今までKubernetesの勉強用にDocker for Desktopを使ってたけど,
なんか公式のチュートリアルがMinikubeを使ってるのでやっぱり入れてみた.
動作環境
- masOS Mojave 10.14
やったこと
- Minikubeのインストール
- Kubernetesのセットアップ
Minikubeのインストール
Kubernetes公式の手順(Install Minikube)をやっていく.
まずはじめに仮想化が有効になっているかチェックする.VMX
の文字に色がついてたらOKらしい.
$ sysctl -a | grep -E --color 'machdep.cpu.features|VMX'
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
kubectl
はDocker for Desktop
をインストールしたときに一緒に入れてるので大丈夫なはず.
一応公式の手順はここ.
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.6", GitCommit:"96fac5cd13a5dc064f7d9f4f23030a6aeface6cc", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:49Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.6", GitCommit:"96fac5cd13a5dc064f7d9f4f23030a6aeface6cc", GitTreeState:"clean", BuildDate:"2019-08-19T11:05:16Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
また, VMのハイパーバイザーはDockerの旧Get StartedでVirtualBox
をインストール済み.
一応ここからダウンロードできる.
Homebrew
を使ってMinikube
をインストールする.
$ brew cask install minikube
...
==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 3601, done.
remote: Counting objects: 100% (3601/3601), done.
remote: Compressing objects: 100% (3593/3593), done.
remote: Total 3601 (delta 25), reused 576 (delta 6), pack-reused 0
Receiving objects: 100% (3601/3601), 1.20 MiB | 1.44 MiB/s, done.
Resolving deltas: 100% (25/25), done.
Tapped 1 command and 3489 casks (3,606 files, 3.9MB).
==> Satisfying dependencies
All Formula dependencies satisfied.
==> Downloading https://storage.googleapis.com/minikube/releases/v1.5.0/minikube-darwin-amd64
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'minikube'.
==> Installing Cask minikube
==> Creating Caskroom at /usr/local/Caskroom
==> We'll set permissions properly so we won't need sudo in the future.
Password:
==> Linking Binary 'minikube-darwin-amd64' to '/usr/local/bin/minikube'.
🍺 minikube was successfully installed!
$ minikube version
minikube version: v1.5.0
commit: d1151d93385a70c5a03775e166e94067791fe2d9
早速Minikube
を起動する.
$ minikube start
😄 minikube v1.5.0 on Darwin 10.14
✨ Automatically selected the 'hyperkit' driver (alternates: [virtualbox])
💾 Downloading driver docker-machine-driver-hyperkit:
> docker-machine-driver-hyperkit.sha256: 65 B / 65 B [---] 100.00% ? p/s 0s
> docker-machine-driver-hyperkit: 10.79 MiB / 10.79 MiB 100.00% 3.25 MiB p
🔑 The 'hyperkit' driver requires elevated permissions. The following commands will be executed:
$ sudo chown root:wheel /Users/uzimihsr/.minikube/bin/docker-machine-driver-hyperkit
$ sudo chmod u+s /Users/uzimihsr/.minikube/bin/docker-machine-driver-hyperkit
Password:
💿 Downloading VM boot image ...
> minikube-v1.5.0.iso.sha256: 65 B / 65 B [--------------] 100.00% ? p/s 0s
> minikube-v1.5.0.iso: 143.77 MiB / 143.77 MiB [-] 100.00% 58.33 MiB p/s 2s
🔥 Creating hyperkit VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
🐳 Preparing Kubernetes v1.16.2 on Docker 18.09.9 ...
💾 Downloading kubeadm v1.16.2
💾 Downloading kubelet v1.16.2
🚜 Pulling images ...
🚀 Launching Kubernetes ...
⌛ Waiting for: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"
⚠️ /usr/local/bin/kubectl is version 1.14.6, and is incompatible with Kubernetes 1.16.2. You will need to update /usr/local/bin/kubectl or use 'minikube kubectl' to connect with this cluster
ちゃんと起動したけどなんか勝手にHyperkit
が使われちゃってるので一旦止める.
$ minikube delete
🔥 Deleting "minikube" in hyperkit ...
💔 The "minikube" cluster has been deleted.
🔥 Successfully deleted profile "minikube"
使用するハイパーバイザは--vm-driver
フラグを指定すればいいらしい.
$ minikube start --vm-driver=virtualbox
😄 minikube v1.5.0 on Darwin 10.14
🔥 Creating virtualbox VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
🐳 Preparing Kubernetes v1.16.2 on Docker 18.09.9 ...
🚜 Pulling images ...
🚀 Launching Kubernetes ...
⌛ Waiting for: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"
⚠️ /usr/local/bin/kubectl is version 1.14.6, and is incompatible with Kubernetes 1.16.2. You will need to update /usr/local/bin/kubectl or use 'minikube kubectl' to connect with this cluster
今度はちゃんとVirtualBox
を使ってくれた.
よく見たらkubectl
のバージョンが古くて警告されてる. バージョンを上げない場合はminikube kubectl
を使う必要があるみたい.
なんかログを見た感じmacBook上にVMを作成してそれをノードとしてKubernetes
クラスタにしてるっぽい?docker-machine
を使ってSwarm
クラスタを作ったときと似たことをやっている?
後で余裕があったらちゃんと調べておく(たぶんやらない).
とりあえずMinikube
のインストールは完了.
Kubernetesのセットアップ
Kubernetes公式の手順(Installing Kubernetes with Minikube)のQuickstartをやっていく.
minikube start
は既に実行済みなので割愛.
早速kubectl
でサンプル用Deployment
を作成してみる.
$ minikube kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
Error: unknown flag: --image
Usage:
minikube kubectl [flags] [options]
Use "minikube kubectl options" for a list of global command-line options (applies to all commands).
$ kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
deployment.apps/hello-minikube created
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
hello-minikube 1/1 1 1 46s
さっきの警告どおりminikube kubectl
使うと--image
が使えないぞ的なエラーになった. なぜ…?
まあ普通にkubectl
が使えるのであまり気にしない.
この hello-minikube Deployment
の8080番ポートを公開するためのService
を作成する.
$ kubectl expose deployment hello-minikube --type=NodePort --port=8080
service/hello-minikube exposed
hello-minikube のPod
を確認する.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-797f975945-ftf9z 1/1 Running 0 6m38s
Service
のURLを取得する.
$ minikube service hello-minikube --url
http://192.168.99.103:31394
取得したURLをブラウザで開く.
クラスタの詳細情報が表示される.
動作確認ができたので, Service
とDeployment
を削除する.
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/hello-minikube-797f975945-ftf9z 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-minikube NodePort 10.109.38.18 <none> 8080:31394/TCP 13m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-minikube 1/1 1 1 17m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-minikube-797f975945 1 1 1 17m
$ kubectl delete service hello-minikube
service "hello-minikube" deleted
$ kubectl delete deployment hello-minikube
deployment.apps "hello-minikube" deleted
Minikube
クラスタを停止, 削除する.
$ minikube stop
✋ Stopping "minikube" in virtualbox ...
🛑 "minikube" stopped.
$ minikube delete
🔥 Deleting "minikube" in virtualbox ...
💔 The "minikube" cluster has been deleted.
🔥 Successfully deleted profile "minikube"
とりあえずMinikube
でKubernetes
クラスタを構築してアプリをデプロイできた.
次はHello Minikubeあたりからチュートリアルを進めていく.