GKE基本のチュートリアル



[基本のチュートリアル](https://cloud.google.com/kubernetes-engine/docs/quickstart#standard)
[【GCP入門編・第7回】知らなきゃ損！ Google Container Engine (GKE) での Dockerイメージを使ったコンテナの起動方法！](https://www.topgate.co.jp/gcp29-cloud-load-balancing)
[Kubernetes: Deployment の仕組み](https://qiita.com/tkusumi/items/01cd18c59b742eebdc6a)
[GKE のオートスケール 5種類を概観する](https://tech.quickguard.jp/posts/gke-autoscale-overview/#%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%BF%E3%82%AA%E3%83%BC%E3%83%88%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%A9%E3%83%BC-ca)
[クラスタの自動スケーリング](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-autoscaler)

#1. GKE APIを有効にする

![スクリーンショット 2021-12-18 13.20.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/239101/15f2e5f3-b84b-4469-74ac-26452c2915b9.png)

```
$ gcloud services enable container.googleapis.com
```

![スクリーンショット 2021-12-20 14.12.05.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/239101/3fa8eafc-0971-c075-0aa8-09b0ad4c6923.png)


#2. gcloudツールのデフォルト設定を構成する

```
$ gcloud config set project gkeproject44592
$ gcloud config set compute/zone asia-northeast1-a
$ gcloud config set compute/region asia-northeast1
$ gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT                            PROJECT          COMPUTE_DEFAULT_ZONE  COMPUTE_DEFAULT_REGION
default  True       xxxxx@xxxxxx.co.jp  gkeproject44592  asia-northeast1-a     asia-northeast1
```

#3. GKEクラスタを作成する

```
# 2 ノードの Standard クラスタを作成
$ gcloud container clusters create hello-cluster --machine-type=e2-micro --num-nodes=2 
WARNING: Starting in January 2021, clusters will use the Regular release channel by default when `--cluster-version`, `--release-channel`, `--no-enable-autoupgrade`, and `--no-enable-autorepair` flags are not specified.
WARNING: Currently VPC-native is the default mode during cluster creation for versions greater than 1.21.0-gke.1500. To create advanced routes based clusters, please pass the `--no-enable-ip-alias` flag
WARNING: Starting with version 1.18, clusters will have shielded GKE nodes by default.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s). 
WARNING: Starting with version 1.19, newly created clusters and node-pools will have COS_CONTAINERD as the default node image when no image type is specified.
Creating cluster hello-cluster in asia-northeast1-a...done.                                                                                                  
Created [https://container.googleapis.com/v1/projects/gkeproject44592/zones/asia-northeast1-a/clusters/hello-cluster].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/asia-northeast1-a/hello-cluster?project=gkeproject44592
kubeconfig entry generated for hello-cluster.
NAME           LOCATION           MASTER_VERSION   MASTER_IP     MACHINE_TYPE  NODE_VERSION     NUM_NODES  STATUS
hello-cluster  asia-northeast1-a  1.21.5-gke.1302  34.84.88.186  e2-micro      1.21.5-gke.1302  1          RUNNING
$ 
```

#4. クラスタの認証情報を取得する
```
$ gcloud container clusters get-credentials hello-cluster
Fetching cluster endpoint and auth data.
kubeconfig entry generated for hello-cluster.
```

#5. アプリケーションをクラスタにデプロイする
作成したクラスタに、コンテナ化されたアプリケーションをデプロイします。

#5.1. Deployment を作成する

deployment.yamlを準備

```
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello-server
  name: hello-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-server
  template:
    metadata:
      labels:
        app: hello-server
    spec:
      containers:
      - image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
        name: hello-server
```

Deployment を作成する

```
$ kubectl apply -f ./deployment.yaml
deployment.apps/hello-server created

$ kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
hello-server-6b955f5585-nx2lk   1/1     Running   0          3m37s


# replicas: 2に変更して、applyするとPodが2個になります。

$ kubectl get deployments,replicasets,pods --selector app=hello-server
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-server   2/2     2            2           20m

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-server-6b955f5585   2         2         2       20m

NAME                                READY   STATUS    RESTARTS   AGE
pod/hello-server-6b955f5585-6llhv   1/1     Running   0          4m52s
pod/hello-server-6b955f5585-nx2lk   1/1     Running   0          20m
```
#5.2. Service を作成する

service.yamlを準備

```
apiVersion: v1
kind: Service
metadata:
  name: hello-server
  labels:
    app: hello-server
spec:
  ports:
  - nodePort: 30934
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: hello-server
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: 34.84.98.47
```

Service を作成する

```
$  kubectl apply -f ./service.yaml
service/hello-server created

$ kubectl get deployments,replicasets,pods,service --selector app=hello-server
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-server   2/2     2            2           6m58s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-server-6b955f5585   2         2         2       6m58s

NAME                                READY   STATUS    RESTARTS   AGE
pod/hello-server-6b955f5585-57qx8   1/1     Running   0          6m57s
pod/hello-server-6b955f5585-nrwj6   1/1     Running   0          6m58s

NAME                   TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)        AGE
service/hello-server   LoadBalancer   10.120.1.3   35.194.96.107   80:30934/TCP   6m39s
```

#5.xx. Deployment を作成する（マニフェスト無し）

Deployment を作成する

[マニュフェスト(YAML)を作らず、kubectl コマンドで Pod や Deployment、Service を作成する方法のメモ(kubectl run/kubectl create/kubectl scale/kubectl expose)](https://qiita.com/toshihirock/items/8d79fa32c61c921c0b3b)

```
$ kubectl create deployment hello-server \
    --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
```

Deployment を公開する。serviceの作成

```
$ kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080
```

```
$ kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
hello-server-5bd6b6875f-s84l5   1/1     Running   0          10s

$ kubectl get nodes
NAME                                           STATUS   ROLES    AGE    VERSION
gke-hello-cluster-default-pool-f641c7d8-1lpc   Ready    <none>   7m     v1.21.5-gke.1302
gke-hello-cluster-default-pool-f641c7d8-cvbm   Ready    <none>   7m1s   v1.21.5-gke.1302

$ kubectl get service hello-server
NAME           TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
hello-server   LoadBalancer   10.120.10.136   34.146.35.65   80:31692/TCP   94s
```

#6. 外部 IP アドレスと公開ポートを指定して、ウェブブラウザでアプリケーションを表示します

```
http://34.146.35.65
```
![スクリーンショット 2021-12-19 11.53.48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/239101/84f4340a-fdb1-54fa-0392-b51ea2d04ec6.png)