Docker Desktop for MacのKubernetesを使う

## 概要
Docker Desktop for MacにKubernetesが用意されているので、使ってみる。

## 目次
- [Kubernetesハンズオン目次](https://qiita.com/Esfahan/items/4fcf51622fed20dffb88)

## Installation
以下のサイトの「Get Docker Desktop for Mac (Edge)」からDockerをダウンロードしてインストールする。
https://hub.docker.com/editions/community/docker-ce-desktop-mac

<img width="777" alt="Screen Shot 2019-06-08 at 11.32.41.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/eec7457a-c9c5-2983-5dab-17c78a2c0901.png">


## kubernetesをインストール
「Enable kubernetes」と「Show system containers(advanced)」の2つにチェックをいれ、Applyをクリック
<img width="612" alt="Screen Shot 2019-06-09 at 18.15.50.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/a84991d1-53c8-589c-ad00-bde8f27f3e2f.png">


installをクリック
<img width="612" alt="Screen Shot 2019-06-09 at 18.15.55.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/e9c6c93a-1182-6c41-3fdd-efaeaf318718.png">


installが完了するまで待つ
<img width="612" alt="Screen Shot 2019-06-09 at 18.15.59.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/a85f44bc-97f1-555a-a2fe-41b5aa535986.png">


install完了
<img width="612" alt="Screen Shot 2019-06-09 at 18.22.43.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/00f269e7-0810-0e13-2df8-882ff56fd3a4.png">

## kubernetesのcontextをdocker-for-desktopに切り替える


```shell-session
$ kubectl config use-context docker-for-desktop
```

もしくは

<img width="462" alt="Screen Shot 2019-06-09 at 18.29.33.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/a198ce86-7091-23db-bf0f-d68dbea23183.png">



## 確認
### Dockerイメージが生成されたことを確認

```shell-session
$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
docker/kube-compose-controller       v0.4.22             cf23dccf65a6        7 weeks ago         35.3MB
docker/kube-compose-api-server       v0.4.22             6c3de501d7f3        7 weeks ago         49.9MB
k8s.gcr.io/kube-proxy                v1.14.1             20a2d7035165        2 months ago        82.1MB
k8s.gcr.io/kube-apiserver            v1.14.1             cfaa4ad74c37        2 months ago        210MB
k8s.gcr.io/kube-scheduler            v1.14.1             8931473d5bdb        2 months ago        81.6MB
k8s.gcr.io/kube-controller-manager   v1.14.1             efb3887b411d        2 months ago        158MB
k8s.gcr.io/coredns                   1.3.1               eb516548c180        4 months ago        40.3MB
k8s.gcr.io/etcd                      3.3.10              2c4adeb21b4f        6 months ago        258MB
k8s.gcr.io/pause                     3.1                 da86e6ba6ca1        17 months ago       742kB
```

### Dockerコンテナが起動していることを確認

```shell-session
$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS               NAMES
3b188096b0bd        eb516548c180           "/coredns -conf /etc…"   3 minutes ago       Up 3 minutes                            k8s_coredns_coredns-fb8b8dccf-ng878_kube-system_6a107d1b-8a97-11e9-a84c-025000000001_2
359156fdde44        eb516548c180           "/coredns -conf /etc…"   4 minutes ago       Up 4 minutes                            k8s_coredns_coredns-fb8b8dccf-w95g9_kube-system_6a0c81f4-8a97-11e9-a84c-025000000001_2
abdcb4770d0d        6c3de501d7f3           "/api-server --kubec…"   4 minutes ago       Up 4 minutes                            k8s_compose_compose-api-6c5bf98cc7-8glcv_docker_94be499a-8a97-11e9-a84c-025000000001_2
e31c3d7678cb        cf23dccf65a6           "/compose-controller…"   4 minutes ago       Up 4 minutes                            k8s_compose_compose-6b69ff6b9d-fq42g_docker_94cbe1a4-8a97-11e9-a84c-025000000001_1
c2904b68e97d        20a2d7035165           "/usr/local/bin/kube…"   4 minutes ago       Up 4 minutes                            k8s_kube-proxy_kube-proxy-l7rw8_kube-system_6a73bc5a-8a97-11e9-a84c-025000000001_1
396ab9225126        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_compose-6b69ff6b9d-fq42g_docker_94cbe1a4-8a97-11e9-a84c-025000000001_1
2ff13073b6bc        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_kube-proxy-l7rw8_kube-system_6a73bc5a-8a97-11e9-a84c-025000000001_1
64c88a251710        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_compose-api-6c5bf98cc7-8glcv_docker_94be499a-8a97-11e9-a84c-025000000001_1
cd6545b47574        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_coredns-fb8b8dccf-ng878_kube-system_6a107d1b-8a97-11e9-a84c-025000000001_1
34d746d57933        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_coredns-fb8b8dccf-w95g9_kube-system_6a0c81f4-8a97-11e9-a84c-025000000001_1
c212db9a78fa        cfaa4ad74c37           "kube-apiserver --ad…"   4 minutes ago       Up 4 minutes                            k8s_kube-apiserver_kube-apiserver-docker-desktop_kube-system_ba13c4f05e12c81986f81b0bea31be9f_1
42a9ff8cd93c        8931473d5bdb           "kube-scheduler --bi…"   4 minutes ago       Up 4 minutes                            k8s_kube-scheduler_kube-scheduler-docker-desktop_kube-system_f44110a0ca540009109bfc32a7eb0baa_1
b85631911c84        efb3887b411d           "kube-controller-man…"   4 minutes ago       Up 4 minutes                            k8s_kube-controller-manager_kube-controller-manager-docker-desktop_kube-system_40f1833ee150c47d0f33cecb02681b1a_1
e8219053ed99        2c4adeb21b4f           "etcd --advertise-cl…"   4 minutes ago       Up 4 minutes                            k8s_etcd_etcd-docker-desktop_kube-system_3773efb8e009876ddfa2c10173dba95e_1
cbeea4516f57        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_kube-apiserver-docker-desktop_kube-system_ba13c4f05e12c81986f81b0bea31be9f_1
ec40c90c38ae        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_kube-scheduler-docker-desktop_kube-system_f44110a0ca540009109bfc32a7eb0baa_1
550ce07216c4        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_kube-controller-manager-docker-desktop_kube-system_40f1833ee150c47d0f33cecb02681b1a_1
880e9f77a396        k8s.gcr.io/pause:3.1   "/pause"                 4 minutes ago       Up 4 minutes                            k8s_POD_etcd-docker-desktop_kube-system_3773efb8e009876ddfa2c10173dba95e_1
```

### k8sのpodを確認

```shell-session
$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                     READY   STATUS    RESTARTS   AGE
docker        compose-6b69ff6b9d-fq42g                 1/1     Running   1          12m
docker        compose-api-6c5bf98cc7-8glcv             1/1     Running   2          12m
kube-system   coredns-fb8b8dccf-ng878                  1/1     Running   2          13m
kube-system   coredns-fb8b8dccf-w95g9                  1/1     Running   2          13m
kube-system   etcd-docker-desktop                      1/1     Running   1          12m
kube-system   kube-apiserver-docker-desktop            1/1     Running   1          12m
kube-system   kube-controller-manager-docker-desktop   1/1     Running   1          12m
kube-system   kube-proxy-l7rw8                         1/1     Running   1          13m
kube-system   kube-scheduler-docker-desktop            1/1     Running   1          12m
```

### k8sのserviceを確認

```shell-session
$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   13m
```

## ハマった点
### Unable to connect to the server: EOF
`kubectl get pods`で以下のエラーが出る。

```shell-session
$ kubectl get pods --all-namespaces
Unable to connect to the server: EOF
```

おそらく、以下の画像のように右下の部分が、「Kubernetes is starting...」のままで、「Kubernetes is running」になっていない。
<img width="612" alt="Screen Shot 2019-06-09 at 18.22.43 copy.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/f5522be4-0a41-cf15-633c-0275e0bd8f78.png">


Docker for MacのAdvancedの設定で、リソースの上限を変更する。
Apply & Restartをクリック。

<img width="612" alt="Screen Shot 2019-06-09 at 18.25.06.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/c373ce09-13d0-ab67-3284-935604f4cd4f.png">


これで、「Kubernetes is running」になり、エラーも出なくなった。

```shell-session
$ kubectl cluster-info
Kubernetes master is running at https://localhost:6443
KubeDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

これでも「Kubernetes is running」にならない場合は、次項を参照。

### いつまで待ってもKubernetes is runningにならない場合
["Kubernetes is starting…" state never ends #2990](https://github.com/docker/for-mac/issues/2990#issuecomment-445597581)

~/.kubeを削除

```
$ rm -rf ~/.kube
```

右上のResetをクリック ->  Reset kubernetes clusterをクリック
<img width="632" alt="Screen Shot 2019-06-09 at 18.26.27.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/105936/7b584f03-88f5-fc2d-146d-a8f92797eee6.png">


これでkubernetesのインストールをやり直す。

- 参考：
    - [kubectlで「Unable to connect to the server」](https://qiita.com/si-masuda/items/e312270b5f87630e4d8f)
    - [[helm] elasticsearchをinstallしたらkubectlが「Unable to connect to the server:」しか返さなくなった](https://qiita.com/secondly/items/9c0ecd9c3ce583f6e0d3)

## 参考
- [Docker for MacでKubernetes インストールからデプロイまで](https://qiita.com/uni-3/items/a4f2afa0973bfc35b498)
- [[docker]Kubernetes無知なので既にリリースされてたDocker for Macで立ち上げてみた](https://blog.adachin.me/archives/7101)
- [kubernetesをminikubeで触ってみたメモ](https://qiita.com/sin_tanaka/items/53e158dabe2644278796)
- [Docker for MacでKubernetes構築](https://qiita.com/nakayamam/items/a9855444cdc4310e0231)