Ubuntu 18.04 に kubernetes をインストールする

# 動機
手元に Kubernetes 環境が欲しかった
Docerk for Mac で使えるけど、自宅のマシンたちに入れてみたかった
# 情報源
参考にした情報
基本的にここの手順に従った
[kubernetes.io kubeadm を使ったセットアップ](https://kubernetes.io/docs/setup/independent/install-kubeadm/)

# docker を入れる
ubuntu のリポジトリにあるものをとりあえず使う

```
sudo apt-get install docker.io
# systemctl で登録する
sudo systemctl enable docker
```

# kubernetes のキーとリポジトリを登録する

```
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
```

# kubeadm を入れる

```
sudo apt update
sudo apt-get install kubeadm
```

# swap をオフにする

```
sudo swapoff -a
```

# flannel 用にオプション付きで init する

```
sudo kubeadm init --pod-network-cidr=10.114.0.0/16
```

インストール後の作業が表示されるので、実行する
他のスレイブノードを追加する方法も表示される

```
## 略
 Your Kubernetes master has initialized successfully!

 To start using your cluster, you need to run the following as a regular user:

   mkdir -p $HOME/.kube
   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
   sudo chown $(id -u):$(id -g) $HOME/.kube/config
 
 You should now deploy a pod network to the cluster.↲
 Run "kubectl apply -f [podnetwork].yaml" with one of the options listed 
   https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:
kubeadm join 10.114.51.231:6443 --token xxxxxx.xxxxxxxxxxxxx --discovery-token-ca-cert-hash sha256:xxxxxxx
```

この時点では node は NotReady です

```
# この時点での get nodes
kubectl get nodes
NAME      STATUS     ROLES     AGE       VERSION
deer      NotReady   master    5m        v1.11.2

# この時点での get pods
kubectl get pods --all-namespaces=true
NAMESPACE     NAME                           READY     STATUS    RESTARTS   AGE
kube-system   coredns-78fcdf6894-hptnd       0/1       Pending   0          1m
kube-system   coredns-78fcdf6894-q6996       0/1       Pending   0          1m
kube-system   etcd-deer                      1/1       Running   0          45s
kube-system   kube-apiserver-deer            1/1       Running   0          37s
kube-system   kube-controller-manager-deer   1/1       Running   0          39s
kube-system   kube-proxy-ksdvd               1/1       Running   0          1m
kube-system   kube-scheduler-deer            1/1       Running   0          23s
```

# flannel を入れる

flannel は pods 間通信のソフトウェア

参考:
[kube-flannel.yaml](https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml)

```
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
```

この時点の状態

```
kubectl get nodes
NAME      STATUS    ROLES     AGE       VERSION
deer      Ready     master    12m       v1.11.2
kubectl get pods --all-namespaces=true -o wide
NAMESPACE     NAME                           READY     STATUS    RESTARTS   AGE       IP             NODE      NOMINATED NODE
kube-system   coredns-78fcdf6894-hptnd       1/1       Running   0          14m       10.114.0.3     deer      <none>
kube-system   coredns-78fcdf6894-q6996       1/1       Running   0          14m       10.114.0.2     deer      <none>
kube-system   etcd-deer                      1/1       Running   0          13m       10.114.51.28   deer      <none>
kube-system   kube-apiserver-deer            1/1       Running   0          13m       10.114.51.28   deer      <none>
kube-system   kube-controller-manager-deer   1/1       Running   0          13m       10.114.51.28   deer      <none>
kube-system   kube-flannel-ds-amd64-r5lxs    1/1       Running   0          2m        10.114.51.28   deer      <none>
kube-system   kube-proxy-ksdvd               1/1       Running   0          14m       10.114.51.28   deer      <none>
kube-system   kube-scheduler-deer            1/1       Running   0          13m       10.114.51.28   deer      <none>
```

# dashboard のインストール
参考
[kubernetes/dashboard](https://github.com/kubernetes/dashboard/wiki/Installation)

```
# 証明書を作る
mkdir $HOME/certs
cd $HOME/certs
openssl genrsa 2048 > dashboard.key
openssl req -new -key dashboard.key > dashboard.csr
openssl x509 -days 3650 -req -signkey dashboard.key < dashboard.csr > dashboard.crt
# 作った証明書から secret を作る
kubectl create secret generic kubernetes-dashboard-certs --from-file=$HOME/certs -n kube-system
secret/kubernetes-dashboard-certs created
# yaml から apply する
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

# クラスターの状況
kubectl cluster-info
Kubernetes master is running at https://10.114.51.28:6443
KubeDNS is running at https://10.114.51.28:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

# この時点の get pods
kubectl get pods -o wide --all-namespaces=true
NAMESPACE     NAME                                   READY     STATUS    RESTARTS   AGE       IP             NODE      NOMINATED NODE
kube-system   coredns-78fcdf6894-hptnd               1/1       Running   0          33m       10.114.0.3     deer      <none>
kube-system   coredns-78fcdf6894-q6996               1/1       Running   0          33m       10.114.0.2     deer      <none>
kube-system   etcd-deer                              1/1       Running   0          33m       10.114.51.28   deer      <none>
kube-system   kube-apiserver-deer                    1/1       Running   0          33m       10.114.51.28   deer      <none>
kube-system   kube-controller-manager-deer           1/1       Running   0          33m       10.114.51.28   deer      <none>
kube-system   kube-flannel-ds-amd64-r5lxs            1/1       Running   0          22m       10.114.51.28   deer      <none>
kube-system   kube-proxy-ksdvd                       1/1       Running   0          33m       10.114.51.28   deer      <none>
kube-system   kube-scheduler-deer                    1/1       Running   0          33m       10.114.51.28   deer      <none>
kube-system   kubernetes-dashboard-6948bdb78-l86vf   1/1       Running   0          6m        10.114.0.4     deer      <none>

# kubectl proxy 起動
kubectl proxy
Starting to serve on 127.0.0.1:8001
```

ブラウザからダッシュボードにアクセス

```
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
```