gcloud, kubectl コマンドまとめ

※ 随時、追記 🥝

# gcloud

Ref. https://cloud.google.com/sdk/gcloud/reference/container/clusters/

```bash
gcloud config list
gcloud config set project <PROJECT-ID>
gcloud config get-value project


# Build & Push
PROJECT_ID="$(gcloud config get-value project)"
docker build -t gcr.io/${PROJECT_ID}/hello-node:v1 .
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-node:v1


# Cluster
gcloud container clusters create <CLUSTER-NAME> --num-nodes=2
gcloud container clusters list
gcloud container clusters delete <CLUSTER-NAME>
# ノード数変更（デフォルトは3）
gcloud container clusters resize <CLUSTER-NAME> --size=2

# cluster の選択
gcloud container clusters get-credentials <CLUSTER-NAME> \
    --zone asia-northeast1-c --project <Project-Name>


# SQL
gcloud sql instances list
gcloud sql instances describe <INSTANCE-NAME>
gcloud sql instances describe <INSTANCE-NAME> | grep connectionName


# Compute
gcloud compute instances list
gcloud compute ssh <NODE>

# kubectl
gcloud components update kubectl
gcloud components remove kubectl
```


```
$ gcloud compute project-info describe --project {project_name}
```



# kubectl

Ref. https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/

```bash
# version
kubectl version

# Create pod
kubectl run <NAME> --images=<IMAGE:TAG> --port <PORT>

# Expose
kubectl expose deployment <NAME> --type=LoadBalancer --port <PORT>

# Sample
kubectl run hello-world --image=gcr.io/${PROJECT_ID}/hello-node:v1 --port 8080
kubectl expose deployment hello-world --type=LoadBalancer --port 8080

# Create
kubectl create -f <yaml-file>

# 確認
kubectl get all
kubectl get nodes
kubectl get services
kubectl get rc
kubectl get pods
kubectl get pods -o wide

kubectl describe service <SERVICE-NAME>
kubectl describe pod <POD-NAME> 

# Delete
kubectl delete service <SERVICE-NAME>
kubectl delete rc <Replication-Controller-NAME>
kubectl delete pod <POD-NAME>

# Secret
kubectl create secret generic <SECRET-NAME> --from-file=<PATH>
kubectl get secrets
kubectl describe secrets <SECRET-NAME>
kubectl delete secrets <SECRET-NAME>

# Job
kubectl get pods -a
kubectl get jobs -o name


kubectl set image <CONTAINER-NAME>=<IMAGE-NAME>:latest
kubectl edit deployment <DEPLOYMENT-NAME>
```

```bash
# log
$ kubectl logs <POD-NAME>
$ kubectl logs <POD-NAME> <CONTAINER-NAME>
```

```bash
# volume
$ kubectl get PersistentVolumeClaim
```

```bash
# Spec 確認
$ kubectl describe nodes
$ gcloud container clusters describe <cluster>
$ gcloud compute instances describe <instance>
```


```bash
# 直接 Pod 内のメインコンテナに入る
$ kubectl exec -it <pod> bash

# コンテナ指定
$ kubectl exec -it -p <pod> -c <container> bash

# ex)
$ kubectl exec -it -p tq-deploy-1662469220-hjfm3 -c mysql bash
```


### 応用的なもの

```bash
# Get pod status
$ kubectl get pods --selector=job-name=pi -o jsonpath='{.items[*].status.containerStatuses[*].state.terminated.reason}'

# Get Service Controller EXTERNAL-IP
$ kubectl get svc service-yml-pull-request-1 -o jsonpath='{.status.loadBalancer.ingress[*].ip}'
```

## まとめて消す

```
kubectl get deployments -o name | xargs kubectl delete
kubectl get svc -o name | grep -v kubernetes | xargs kubectl delete
```

これで済んだ

```bash
kubectl delete service,deployment --all
```

# Cloud SQL Proxy

Ref. https://cloud.google.com/sql/docs/mysql/connect-admin-proxy?hl=ja

```bash:on_Mac
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
chmod +x cloud_sql_proxy
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 \
                  -credential_file=<PATH_TO_KEY_FILE> &
mysql -u <USER> -p --host 127.0.0.1
```

# GCPのコンテナからダンプデータをローカルに持ってくる例

```bash
# まずは、コンテナに入る
$ kubectl exec -it tq-deploy-3070501890-1234 bash

# mysql-client と zip が入ってなかったら入れる
$ apt-get install mysql-client zip

# ダンプする
$ mysqldump -h 127.0.0.1 -u root -ppassword {DB_name} > /tmp/20170824.dump

$ tar cvf data.tar /tmp/20170824.dump

# コンテナからでてローカルから以下でコピーできる
$ kubectl cp tq-deploy-3070501890-1234:/tmp/data.tar /local_path/data.tar
```

# References

- セットアップ
    - https://cloud.google.com/sdk/downloads#mac
    - https://cloud.google.com/container-engine/docs/before-you-begin
- 料金
  - https://cloud.google.com/container-registry/docs/pricing
- node サンプル
  - https://cloud.google.com/container-engine/docs/tutorials/hello-node
- ubuntu にいれる
    - gcloud: https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu
- kubectl いれる
    - https://kubernetes.io/docs/tasks/tools/install-kubectl/