GKE作業コマンドメモ

## 概要

* GKE作業をするときコピペするための個人的なコマンドメモ
* SDKやサービスアクセス権限などはすべて整っている前提


## 詳細

### GKE作業コマンドメモ

#### 作業準備

```
$ sudo gcloud components update kubectl
$ export PROJECT_ID="$(gcloud config get-value project -q)"
$ export GCR_HOST="asia.gcr.io"
$ export DOCKER_IMAGE="sample-app-server"
$ export IMAGE_TAG="v1"
$ export CONTAINER_NAME=${DOCKER_IMAGE}
$ export CLUSTER_NAME="sample-cluster"
$ export COMPUTE_ZONE="asia-northeast1-c"
$ export MACHINE_TYPE="n1-standard-1"
$ gcloud config set project ${PROJECT_ID}
$ gcloud config set container/cluster ${CLUSTER_NAME}
$ gcloud config set compute/zone ${COMPUTE_ZONE}

または上記をまとめた処理を実行する
例）$ bash env.sh
```

#### Dockerfile単体チェック

```
作業コマンドメモ
$ cd ~/docker/<Dockerfile_dir>/
$ docker build ./ -t ${GCR_HOST}/${DOCKER_IMAGE}:${IMAGE_TAG}
$ docker images
$ docker run -p 8080:80 --rm --privileged -d -it ${GCR_HOST}/${DOCKER_IMAGE}:${IMAGE_TAG} /sbin/init
$ docker ps
$ docker exec -it <container_id> /bin/bash
$ ps -aux | grep -e nginx -e php-fpm
# exit
$ curl http://0.0.0.0:8080/phpinfo.php
$ lynx http://0.0.0.0:8080/phpinfo.php
$ docker stop <container_id>
```

#### クラスター作成

```
$ gcloud container clusters create ${CLUSTER_NAME} --zone ${COMPUTE_ZONE} --machine-type="${MACHINE_TYPE}" --num-nodes=3
$ gcloud container clusters get-credentials ${CLUSTER_NAME}
$ gcloud compute instances list

##kubectl get pods
##kubectl create deployment hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1
##kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080
##kubectl get service
```

#### コンテナイメージ作成登録

```
$ cloudshell edit Dockerfile
$ docker build -t asia.gcr.io/${PROJECT_ID}/${DOCKER_IMAGE}:${IMAGE_TAG} .
$ docker push ${GCR_HOST}/${PROJECT_ID}/${DOCKER_IMAGE}:${IMAGE_TAG}
$ gcloud container images list-tags asia.gcr.io/${PROJECT_ID}/${DOCKER_IMAGE}
```

#### コンテナ起動

```
$ docker run -d -p 8080:80 asia.gcr.io/${PROJECT_ID}/${DOCKER_IMAGE}:${IMAGE_TAG}
$ curl http://0.0.0.0:8080/phpinfo.php
$ lynx http://0.0.0.0:8080/phpinfo.php
$ docker rm <container_id>
$ docker ps
$ docker kill <container_id>
$ docker rm <container_id>
```

#### コンテナデプロイ

```
$ wget https://raw.githubusercontent.com/GoogleCloudPlatform/gcp-getting-started-lab-jp/master/fundamental/gke/sample-app-server/sample-app-server-deployment.yaml
※ファイル名変更

$ cloudshell edit sample-webapp-deployment.yaml
$ kubectl apply -f sample-webapp-deployment.yaml
$ kubectl get deployment
$ kubectl get pods -o wide
$ wget https://raw.githubusercontent.com/GoogleCloudPlatform/gcp-getting-started-lab-jp/master/fundamental/gke/sample-app-server/hoge-app-server-service.yaml
$ cloudshell edit sample-app-server-service.yaml
$ kubectl apply -f sample-app-server-service.yaml
$ kubectl get svc
# podにログインして確認
$ kubectl exec -it web-xxxxxxxxx /bin/bash
$ kubectl exec -it app-xxxxxxxxx /bin/bash
$ curl <EXTERNAL_IP>:8080
```

* sample-webapp-server-deployment.yaml

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      run: web
  template:
    metadata:
      labels:
        run: web
    spec:
      containers:
      - name: web
        image: asia.gcr.io/${PROJECT_ID}/sample-web-server:v1
        ports:
        - containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 3
  selector:
    matchLabels:
      run: app
  template:
    metadata:
      labels:
        run: app
    spec:
      containers:
      - name: app
        image: asia.gcr.io/${PROJECT_ID}/sample-app-server:v1
        ports:
        - containerPort: 9000
```

* sample-app-server-service.yaml

```
apiVersion: v1
kind: Service
metadata:
  labels:
    name: sample-app-server
  name:sample-app-server
spec:
  ports:
  - port: 8080
  selector:
    name: sample-app-server
  type: LoadBalancer
```


#### ポッドスケール

```
$ kubectl scale deployment/hoge-app-server --replicas=3
$ kubectl get pods
```

#### コンテナ削除

```
$ kubectl delete svc/hoge-app-server
$ kubectl delete deployment/hoge-app-server
```