More than 5 years have passed since last update.

gcloud, kubectl コマンドまとめ

Last updated at Posted at 2017-04-14

※ 随時、追記 🥝


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

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>

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}


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

# 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>
# log
$ kubectl logs <POD-NAME>
$ kubectl logs <POD-NAME> <CONTAINER-NAME>
# volume
$ kubectl get PersistentVolumeClaim
# Spec 確認
$ kubectl describe nodes
$ gcloud container clusters describe <cluster>
$ gcloud compute instances describe <instance>
# 直接 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


# 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


kubectl delete service,deployment --all

Cloud SQL Proxy

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

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


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

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

# ダンプする
$ mysqldump -h -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



Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up