MicroK8sを使ってみる

# はじめに

[MicroK8s](https://microk8s.io/ "MicroK8s")について、個人的に興味ありつつもまだ触れてなかったので、どんな感じなのか動かしてみた。

既に好き勝手いじれる環境がある場合は特に必要ないと思うが、
使い捨てでいいからちょっとしたことを確認する場合は使えそう。

# 環境

- AWS
  - VM instance type :  t2.medium
     - core : 2
     - memory : 4GB
  - OS
     - ubuntu : 16.04.5
  - snap : 2.36.1

```
$ snap --version
snap    2.36.1
snapd   2.36.1
series  16
ubuntu  16.04
kernel  4.4.0-1072-aws
$
```

# 各種コマンド

## snap install

まずはインストールしないと何も始まらない。
snapコマンドで、以下のように簡単に入れられる。

```
$ sudo snap install microk8s --classic
microk8s v1.12.2 from Canonical? installed
$
```

## snap info

入れたあと、情報を確認してみる。
stable, beta, edgeなど複数バージョンが表示されるが、snapはバージョンの切り替えができるようだ。

```
$ snap info microk8s
name:      microk8s
summary:   Kubernetes for workstations and appliances
publisher: Canonical?
contact:   https://github.com/ubuntu/microk8s
license:   unset
description: |
  MicroK8s is a small, fast, secure, single node Kubernetes that installs on
  just about any Linux box. Use it for offline development, prototyping,
  testing, or use it on a VM as a small, cheap, reliable k8s for CI/CD. It's
  also a great k8s for appliances - develop your IoT apps for k8s and deploy
  them to MicroK8s on your boxes.
commands:
  - microk8s.config
  - microk8s.disable
  - microk8s.docker
  - microk8s.enable
  - microk8s.inspect
  - microk8s.istioctl
  - microk8s.kubectl
  - microk8s.reset
services:
  microk8s.daemon-apiserver:          simple, enabled, active
  microk8s.daemon-controller-manager: simple, enabled, active
  microk8s.daemon-docker:             simple, enabled, active
  microk8s.daemon-etcd:               simple, enabled, active
  microk8s.daemon-kubelet:            simple, enabled, active
  microk8s.daemon-proxy:              simple, enabled, active
  microk8s.daemon-scheduler:          simple, enabled, active
snap-id:      EaXqgt1lyCaxKaQCU349mlodBkDCXRcg
tracking:     stable
refresh-date: today at 14:21 UTC
channels:
  stable:         v1.12.2  (266) 224MB classic <
  candidate:      v1.13.0  (340) 204MB classic
  beta:           v1.13.0  (340) 204MB classic
  edge:           v1.13.0  (340) 204MB classic
  1.12/stable:    v1.12.3  (336) 226MB classic
  1.12/candidate: v1.12.3  (336) 226MB classic
  1.12/beta:      v1.12.3  (336) 226MB classic
  1.12/edge:      v1.12.3  (336) 226MB classic
  1.11/stable:    v1.11.5  (322) 219MB classic
  1.11/candidate: v1.11.5  (322) 219MB classic
  1.11/beta:      v1.11.5  (322) 219MB classic
  1.11/edge:      v1.11.5  (322) 219MB classic
  1.10/stable:    v1.10.11 (321) 175MB classic
  1.10/candidate: v1.10.11 (321) 175MB classic
  1.10/beta:      v1.10.11 (321) 175MB classic
  1.10/edge:      v1.10.11 (321) 175MB classic
installed:        v1.12.2  (266) 224MB classic
$
```

## microk8s.config

上記のcommandsを実行してみる。
まずはmicrok8s.config。アドレスは適当に変えてある。

```
$ sudo microk8s.config
apiVersion: v1
clusters:
- cluster:
    server: http://192.168.1.1:8080
  name: microk8s-cluster
contexts:
- context:
    cluster: microk8s-cluster
    user: admin
  name: microk8s
current-context: microk8s
kind: Config
preferences: {}
users:
- name: admin
  user:
    username: admin
```

## microk8s.docker

dockerコマンドは、頭に「microk8s.」をつけて実行。

```
$ sudo microk8s.docker -v
Docker version 17.03.2-ce, build f5ec1e2
$
```

## microk8s.startとstop

[quick start](https://microk8s.io/#quick-start "quick start")にはstart, stopについて記載があったが、なぜかコマンド補完で現れない。

もしやと思って実行してみたが、どうも無いようだ。
enable, disableは機能のon/offのようなので、起動・停止はできないのだろうか？

```
$ sudo microk8s.stop
sudo: microk8s.stop: command not found
$ sudo microk8s.start
sudo: microk8s.start: command not found
$
```

## microk8s.enable

add-onの有効化をする。
[document](https://microk8s.io/docs/ "document")によると、デフォルトでは「barebone」として以下のコンポーネントがインストールされる。

- barebone
  - api-server
  - controller-manager
  - scheduler
  - kubelet
  - cni
  - kube-proxy

上記以外のadd-onとして以下のようなものが指定できるようだ。
dashboardやprivate registryだけでなく、GPUやIstioまで選べるのですね。

- add-on
  - dns
  - dashboard
  - storage
  - ingress
  - gpu
  - istio
  - registry
  - metrics-server

たとえばdashboardを有効化すると以下のような感じ。

```
$ sudo microk8s.enable dashboard
Enabling dashboard
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
service/monitoring-grafana created
service/monitoring-influxdb created
service/heapster created
deployment.extensions/monitoring-influxdb-grafana-v4 created
serviceaccount/heapster created
configmap/heapster-config created
configmap/eventer-config created
deployment.extensions/heapster-v1.5.2 created
dashboard enabled
$
```

## microk8s.kubectl

おなじみのkubectlコマンドは、頭に「microk8s.」をつけて実行できる。

```
$ sudo microk8s.kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-24T06:54:59Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-24T06:43:59Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
$
```

たとえば上記のようにdashboardを有効化したあとに、名前空間kube-systemのpodを見てみよう。

```
$ sudo microk8s.enable dashboard
Enabling dashboard
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
service/monitoring-grafana created
service/monitoring-influxdb created
service/heapster created
deployment.extensions/monitoring-influxdb-grafana-v4 created
serviceaccount/heapster created
configmap/heapster-config created
configmap/eventer-config created
deployment.extensions/heapster-v1.5.2 created
dashboard enabled
$ sudo microk8s.kubectl get pods
No resources found.
$ sudo microk8s.kubectl get pods -n kube-system
NAME                                              READY   STATUS        RESTARTS   AGE
heapster-v1.5.2-74f7978789-vxjpt                  0/4     Terminating   0          13s
heapster-v1.5.2-7bb8ccfdf9-k782d                  4/4     Running       0          2s
kubernetes-dashboard-67d4c89764-hx84q             1/1     Running       0          13s
monitoring-influxdb-grafana-v4-8467db6558-swt4j   2/2     Running       0          13s
$
```

## microk8s.disable

上記enableの逆。add-onを無効化。

```
$ sudo microk8s.disable dashboard
Disabling dashboard
secret "kubernetes-dashboard-certs" deleted
serviceaccount "kubernetes-dashboard" deleted
deployment.apps "kubernetes-dashboard" deleted
service "kubernetes-dashboard" deleted
service "monitoring-grafana" deleted
service "monitoring-influxdb" deleted
service "heapster" deleted
deployment.extensions "monitoring-influxdb-grafana-v4" deleted
serviceaccount "heapster" deleted
configmap "heapster-config" deleted
configmap "eventer-config" deleted
deployment.extensions "heapster-v1.5.2" deleted
dashboard disabled
$
```

## microk8s.inspect

サービスが起動しているか？など、その名のとおり検査するコマンド。

```
$ sudo microk8s.inspect
Inspecting services
  Service snap.microk8s.daemon-docker is running
  Service snap.microk8s.daemon-apiserver is running
  Service snap.microk8s.daemon-proxy is running
  Service snap.microk8s.daemon-kubelet is running
  Service snap.microk8s.daemon-scheduler is running
  Service snap.microk8s.daemon-controller-manager is running
  Service snap.microk8s.daemon-etcd is running
  Copy service arguments to the final report tarball
Inspecting AppArmor configuration
Gathering system info
  Copy network configuration to the final report tarball
  Copy processes list to the final report tarball
  Copy snap list to the final report tarball
  Inspect kubernetes cluster

 WARNING:  IPtables FORWARD policy is DROP. Consider enabling traffic forwarding with: sudo iptables -P FORWARD ACCEPT
Building the report tarball
  Report tarball is at /var/snap/microk8s/266/inspection-report-20181206_150403.tar.gz
$
```

## microk8s.istioctl

[Istio](https://istio.io/ "Istio") のコマンドラインツールも実行できる。

```
$ sudo microk8s.istioctl version
Version: 1.0.0
GitRevision: 3a136c90ec5e308f236e0d7ebb5c4c5e405217f4
User: root@71a9470ea93c
Hub: gcr.io/istio-release
GolangVersion: go1.10.1
BuildStatus: Clean

$
$ sudo microk8s.istioctl

Istio configuration command line utility.

Create, list, modify, and delete configuration resources in the Istio
system.

Available routing and traffic management configuration types:

        [virtualservice gateway destinationrule serviceentry httpapispec httpapispecbinding quotaspec quotaspecbinding servicerole servicerolebinding policy]

See https://istio.io/docs/reference/ for an overview of Istio routing.

Usage:
  istioctl [command]
・・以下略・・
$
```

## microk8s.reset

microk8s自身を削除する前にこれを打って、起動しているpodのお掃除をする。

```
$ sudo microk8s.reset
Calling clean_cluster
Cleaning resources in namespace default
endpoints "kubernetes" deleted
event "xxxxx" deleted
・・・作成されたイベントやエンドポイントなどたくさんリソースが削除される・・・・
secret "default-token-cmtpq" deleted
serviceaccount "default" deleted
service "kubernetes" deleted
Cleaning resources in namespace kube-public
secret "default-token-xbkqj" deleted
serviceaccount "default" deleted
Cleaning resources in namespace kube-system
configmap "extension-apiserver-authentication" deleted
endpoints "kube-controller-manager" deleted
endpoints "kube-scheduler" deleted
event "kube-controller-manager.156dc4560f231500" deleted
event "kube-scheduler.156dc4563d68f9e8" deleted
secret "default-token-p7kst" deleted
serviceaccount "default" deleted
Waiting for kubernetes resources to be released
$
```

## snap remove

microk8s自身を削除する。
上記のresetを実施してリソースのお掃除後にこれを実行。

```
$ sudo microk8s.reset
・・・略・・・
$ sudo snap remove microk8s
microk8s removed
$
```

# まとめ

- [microk8s](https://microk8s.io/ "microk8s")をインストールして、いくつかコマンドを実行してみた。
- [kubeadm](https://kubernetes.io/docs/setup/independent/install-kubeadm/ "kubeadm")や[minikube](https://kubernetes.io/docs/setup/minikube/ "minikube")などインストールのコマンドはいくつかあるが、microk8sは最も楽に味見環境を作れるものの1つ。
- add-onのenableと合わせて使えば、kubernetesとその関連ツールを少し試してみる、という目的では十分使えそう。