LarryDpk
发布于 2021-05-07 / 2982 阅读
0

GCP常用命令(gcloud、gsutil)

gcloud

Config

初始化配置:

gcloud init

列出配置信息:

gcloud projects list
gcloud auth list
gcloud config list

设置:

gcloud config set project 'projectId'
gcloud config set account 'ACCOUNT'

使用Json授权ServiceAccount:

gcloud auth activate-service-account --key-file xxxx.json

获取GKE Cluster鉴权信息:

gcloud container clusters get-credentials 'cluster-name' --region=xxx

安装Components:

gcloud components install beta

compute

创建:

$ gcloud compute instances create pkslow-vm \
--project=pkslow \
--zone=us-west1-a \
--machine-type=e2-micro \
--network-interface=network-tier=PREMIUM,subnet=default \
--maintenance-policy=MIGRATE \
--service-account=admin-for-all@pkslow.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--tags=http-server,https-server \
--create-disk=auto-delete=yes,boot=yes,device-name=instance-1,image=projects/centos-cloud/global/images/centos-8-v20211105,mode=rw,size=20,type=projects/pkslow/zones/us-west1-a/diskTypes/pd-standard \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--reservation-affinity=any

查看机器类型:

gcloud compute machine-types list | grep us-west

删除:

$ gcloud compute instances delete pkslow-vm --zone=us-west1-a

列出:

gcloud compute instances list
gcloud compute instance-groups list
gcloud compute instance-templates list

SSH连接:

gcloud compute ssh example-instance --zone=us-central1-a
gcloud compute ssh example-instance --zone=us-central1-a  --command="ps -ejH"

读取Console日志:

$ gcloud compute --project=pkslow instances get-serial-port-output pkslow-test --zone=us-west1-a --port=1

Pub/Sub相关

列出Topic和subscriptions:

gcloud pubsub topics list
gcloud pubsub subscriptions list

创建 ID 为 pkslow-topic 的主题:

gcloud pubsub topics create pkslow-topic

创建 ID 为 pkslow-sub 的订阅,并将其附加到 pkslow-topic

gcloud pubsub subscriptions create pkslow-sub --topic=pkslow-topic

# 支持ordering
gcloud pubsub subscriptions create pkslow-sub --topic=pkslow-topic --enable-message-ordering

更新Topics:

gcloud pubsub topics update pkslow-topic --topic-encryption-key=xxx

删除Topic/subscriptions:

gcloud pubsub topics delete pkslow-topic
gcloud pubsub subscriptions delete pkslow-sub

将消息发布到 pkslow-topic

gcloud pubsub topics publish pkslow-topic --message="hello"

接收来自 my-sub 的消息:

gcloud pubsub subscriptions pull pkslow-sub --auto-ack

gcloud 工具会将消息输出到命令行。

cloudsql

Instance相关:

# 列出
$ gcloud sql instances list

# 创建
$ gcloud sql instances create pkslow-instance --database-version=POSTGRES_9_6 --cpu=2 --memory=8GiB --zone=us-central1-a --root-password=password123

# 删除
gcloud sql instances delete pkslow-instance

DB相关:

# 列出
$ gcloud sql databases list --instance=pkslow-instance

# 创建
$ gcloud sql databases create DATABASE --instance=pkslow-instance

# 删除
$ gcloud sql databases delete DATABASE --instance=pkslow-instance

logging

# 读gce的日志
 $ gcloud logging read "resource.type=gce_instance" --limit=100
# 根据日志级别过滤
$ gcloud logging read "severity>=ERROR" --format=json
# 根据时间过滤
$ gcloud logging read 'timestamp<="2015-05-31T23:59:59Z" AND
 timestamp>="2015-05-31T00:00:00Z"'
 
 # 查看所有sink
 $ gcloud logging sinks list --limit=10
 # 创建sink
 $ gcloud logging sinks create my-gcs-sink storage.googleapis.com/my-bucket --log-filter='logName="projects/my-project/appengine.googleapis.com%2Fsyslog"'

其它

打开某个服务API:

gcloud services enable compute.googleapis.com

创建GKE:

gcloud container clusters create pkslow-k8s \
--zone us-west1-a \
--cluster-version 1.20.10-gke.1600 \
--machine-type n1-standard-1

获取集群认证:

$ gcloud container clusters get-credentials pkslow-k8s --zone=us-west1-a

删除GKE集群:

$ gcloud container clusters delete pkslow-k8s --zone us-west1-a

gsutil

创建Bucket:

$ gsutil mb -p pkslow -l us-west1 -b on gs://pkslow-terraform
Creating gs://pkslow-terraform/...

列出项目的buckets:

gsutil ls gs://

列出bucket文件:

gsutil ls gs://<bucketName>

复制文件:

gsutil cp localFile gs://xxx
gsutil cp gs://xxx localPath

Reference:

gcloud