【初心者】Kubernetesを試す

# 目的
kubenetes 公式ドキュメントの[チュートリアル](https://kubernetes.io/ja/docs/tutorials/hello-minikube/)で全体の流れを理解する

# 手段
Minikube と Katacoda を使用して、Kubernetes 上でサンプルアプリケーションを動かす

#### さっそくやってみる

```
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4
```
Pod を管理する Deployment を作成する。

```
kubectl get deployments
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
hello-node   1/1     1            1           4s
```
deployment の名前が hello-node なんですよね。たぶん。

```
kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
hello-node-7567d9fdc9-p8wkk   1/1     Running   0          12s
```
Pod が作成されているREADY は 1/1 なので起動完了。

```
$ kubectl get events
LAST SEEN   TYPE     REASON                    OBJECT                             MESSAGE
70s         Normal   Scheduled                 pod/hello-node-7567d9fdc9-p8wkk    Successfully assigned default/hello-node-7567d9fdc9-p8wkk to minikube
69s         Normal   Pulling                   pod/hello-node-7567d9fdc9-p8wkk    Pulling image "k8s.gcr.io/echoserver:1.4"
69s         Normal   Pulled                    pod/hello-node-7567d9fdc9-p8wkk    Successfully pulled image "k8s.gcr.io/echoserver:1.4" in 455.910523ms
69s         Normal   Created                   pod/hello-node-7567d9fdc9-p8wkk    Created container echoserver
69s         Normal   Started                   pod/hello-node-7567d9fdc9-p8wkk    Started container echoserver
70s         Normal   SuccessfulCreate          replicaset/hello-node-7567d9fdc9   Created pod: hello-node-7567d9fdc9-p8wkk
70s         Normal   ScalingReplicaSet         deployment/hello-node              Scaled up replica set hello-node-7567d9fdc9 to 1
2m58s       Normal   Starting                  node/minikube                      Starting kubelet.
2m58s       Normal   NodeHasSufficientMemory   node/minikube                      Node minikube status is now: NodeHasSufficientMemory
2m58s       Normal   NodeHasNoDiskPressure     node/minikube                      Node minikube status is now: NodeHasNoDiskPressure
2m58s       Normal   NodeHasSufficientPID      node/minikube                      Node minikube status is now: NodeHasSufficientPID
2m58s       Normal   NodeAllocatableEnforced   node/minikube                      Updated Node Allocatable limit across pods
2m43s       Normal   Starting                  node/minikube                      Starting kubelet.
2m43s       Normal   NodeHasSufficientMemory   node/minikube                      Node minikube status is now: NodeHasSufficientMemory
2m43s       Normal   NodeHasNoDiskPressure     node/minikube                      Node minikube status is now: NodeHasNoDiskPressure
2m43s       Normal   NodeHasSufficientPID      node/minikube                      Node minikube status is now: NodeHasSufficientPID
2m43s       Normal   NodeAllocatableEnforced   node/minikube                      Updated Node Allocatable limit across pods
2m43s       Normal   NodeReady                 node/minikube                      Node minikube status is now: NodeReady
2m34s       Normal   RegisteredNode            node/minikube                      Node minikube event: Registered Node minikube in Controller
2m32s       Normal   Starting                  node/minikube                      Starting kube-proxy.
```
クラスターイベントの確認できるみたいです。

```yaml
$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /root/.minikube/ca.crt
    extensions:
    - extension:
        last-update: Fri, 17 Jun 2022 01:42:02 UTC
        provider: minikube.sigs.k8s.io
        version: v1.18.0
      name: cluster_info
    server: https://10.0.0.6:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        last-update: Fri, 17 Jun 2022 01:42:02 UTC
        provider: minikube.sigs.k8s.io
        version: v1.18.0
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /root/.minikube/profiles/minikube/client.crt
    client-key: /root/.minikube/profiles/minikube/client.key
```
あれだけ事前に学習した Podtemplate が出てこないと少し悲しいです。。。

```
$ kubectl expose deployment hello-node --type=LoadBalancer --port=8080
service/hello-node exposed
```
Pod はKubenetersクラスター内部のIPアドレスからのみアクセス可能。外部からアクセスするために Service として Pod を公開する必要があるようです。

```
$ kubectl get services
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.96.181.83   <pending>     8080:30559/TCP   2m2s
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          9m30s
```
LoadBalancer が追加されていますね。

```
$ minikube service hello-node
|-----------|------------|-------------|-----------------------|
| NAMESPACE |    NAME    | TARGET PORT |          URL          |
|-----------|------------|-------------|-----------------------|
| default   | hello-node |        8080 | http://10.0.0.6:30559 |
|-----------|------------|-------------|-----------------------|
* Opening service default/hello-node in default browser...
Minikube Dashboard is not supported via the interactive terminal experience.

Please click the 'Preview Port 30000' link above to access the dashboard.
This will now exit. Please continue with the rest of the tutorial.

X Exiting due to HOST_BROWSER: exit status 1
* 
* If the above advice does not help, please let us know: 
  - https://github.com/kubernetes/minikube/issues/new/choose
```
URL(http://10.0.0.6:30559)は先ほどService登録したコンテナのアクセス先みたいです。
ブラウザでアクセスすると以下のレスがあります。

```
CLIENT VALUES:
client_address=172.x.x.x
command=GET
real path=/
query=nil
request_version=1.1
request_uri=http://02c62c65b48f4aaaa4c21049734a3c5e-167772166-30559-elsy05.environments.katacoda.com:8080/

SERVER VALUES:
server_version=nginx: 1.10.0 - lua: 10001

HEADERS RECEIVED:
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding=gzip
accept-language=ja,en-US;q=0.9,en;q=0.8
host=02c62c65b48f4aaaa4c21049734a3c5e-167772166-30559-elsy05.environments.katacoda.com
referer=https://02c62c65b48f4aaaa4c21049734a3c5e-167772166-3000-elsy05.environments.katacoda.com/
sec-ch-ua=" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
sec-ch-ua-mobile=?0
sec-ch-ua-platform="Windows"
sec-fetch-dest=document
sec-fetch-mode=navigate
sec-fetch-site=same-site
sec-fetch-user=?1
upgrade-insecure-requests=1
user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
via=1.1 google
x-cloud-trace-context=2a663debe2d1ac0f3d3007d248ffc619/7429275015060579359
x-forwarded-for=x.x.x.x
x-forwarded-proto=https
x-katacoda-host=elsy05
x-real-ip=x.x.x.x
BODY:
-no body in request-

```
$ minikube addons list
|-----------------------------|----------|--------------|
|         ADDON NAME          | PROFILE  |    STATUS    |
|-----------------------------|----------|--------------|
| ambassador                  | minikube | disabled     |
| auto-pause                  | minikube | disabled     |
| csi-hostpath-driver         | minikube | disabled     |
| dashboard                   | minikube | enabled ✅   |
| default-storageclass        | minikube | enabled ✅   |
| efk                         | minikube | disabled     |
| freshpod                    | minikube | disabled     |
| gcp-auth                    | minikube | disabled     |
| gvisor                      | minikube | disabled     |
| helm-tiller                 | minikube | disabled     |
| ingress                     | minikube | disabled     |
| ingress-dns                 | minikube | disabled     |
| istio                       | minikube | disabled     |
| istio-provisioner           | minikube | disabled     |
| kubevirt                    | minikube | disabled     |
| logviewer                   | minikube | disabled     |
| metallb                     | minikube | disabled     |
| metrics-server              | minikube | enabled ✅   |
| nvidia-driver-installer     | minikube | disabled     |
| nvidia-gpu-device-plugin    | minikube | disabled     |
| olm                         | minikube | disabled     |
| pod-security-policy         | minikube | disabled     |
| registry                    | minikube | disabled     |
| registry-aliases            | minikube | disabled     |
| registry-creds              | minikube | disabled     |
| storage-provisioner         | minikube | enabled ✅   |
| storage-provisioner-gluster | minikube | disabled     |
| volumesnapshots             | minikube | disabled     |
|-----------------------------|----------|--------------|
```
Minikube のビルドインのアドオンを一覧表示します。

$ minikube addons enable metrics-server
  - Using image k8s.gcr.io/metrics-server-amd64:v0.2.1
* The 'metrics-server' addon is enabled
```
アドオンを有効化してみます。

```
$ kubectl get pod,svc -n kube-system
NAME                                   READY   STATUS    RESTARTS   AGE
pod/coredns-74ff55c5b-svnk6            1/1     Running   0          17m
pod/etcd-minikube                      1/1     Running   0          18m
pod/kube-apiserver-minikube            1/1     Running   0          18m
pod/kube-controller-manager-minikube   1/1     Running   0          18m
pod/kube-proxy-vd2ch                   1/1     Running   0          17m
pod/kube-scheduler-minikube            1/1     Running   0          18m
pod/metrics-server-56c4f8c9d6-v4wqr    1/1     Running   0          17m
pod/storage-provisioner                1/1     Running   0          18m

NAME                     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
service/kube-dns         ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   18m
service/metrics-server   ClusterIP   10.97.239.28   <none>        443/TCP                  18m
```
Pod とサービスを確認します。なんかハンズオンで良く見た画面表示ですね。

```
$ minikube addons disable metrics-server
  - Using image k8s.gcr.io/metrics-server-amd64:v0.2.1
* "The 'metrics-server' addon is disabled
```
アドオンの無効化もコマンドで実行します。

```
$ kubectl delete service hello-node
service "hello-node" deleted
$ kubectl delete deployment hello-node
deployment.apps "hello-node" deleted
```
クラスターに作成したリソースをクリーンアップします。

```
$ minikube stop
* Stopping node "minikube"  ...
* 1 nodes stopped.
```
最後は minikube にさよならを告げます。


# おわりに
なんとなく流れのイメージはつかめた気がします。

YAML でマニュフェスト書いて、Pod 作ってサービス登録、アクセス確認。

以上終了的なイメージでおります。