gcloud

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GCloud Skill

GCloud CLI 使用技能

Use the
gcloud
CLI to manage Google Cloud Platform resources and services.
使用
gcloud
CLI管理Google Cloud Platform(GCP)的资源与服务。

Authentication

身份验证

Check current auth:
bash
gcloud auth list
Login interactively:
bash
gcloud auth login
Login with service account:
bash
gcloud auth activate-service-account --key-file=key.json
Application default credentials:
bash
gcloud auth application-default login
查看当前身份验证信息:
bash
gcloud auth list
交互式登录:
bash
gcloud auth login
使用服务账号登录:
bash
gcloud auth activate-service-account --key-file=key.json
应用默认凭据:
bash
gcloud auth application-default login

Project & Configuration

项目与配置

List projects:
bash
gcloud projects list
Set default project:
bash
gcloud config set project PROJECT_ID
Show current config:
bash
gcloud config list
Create named configuration:
bash
gcloud config configurations create my-config
gcloud config configurations activate my-config
Set default region/zone:
bash
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-a
列出项目:
bash
gcloud projects list
设置默认项目:
bash
gcloud config set project PROJECT_ID
查看当前配置:
bash
gcloud config list
创建命名配置:
bash
gcloud config configurations create my-config
gcloud config configurations activate my-config
设置默认区域/可用区:
bash
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-a

Compute Engine (VMs)

计算引擎(VM)

List instances:
bash
gcloud compute instances list
Create instance:
bash
gcloud compute instances create my-vm \
  --zone=us-central1-a \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud
SSH to instance:
bash
gcloud compute ssh my-vm --zone=us-central1-a
Stop/start instance:
bash
gcloud compute instances stop my-vm --zone=us-central1-a
gcloud compute instances start my-vm --zone=us-central1-a
Delete instance:
bash
gcloud compute instances delete my-vm --zone=us-central1-a
列出实例:
bash
gcloud compute instances list
创建实例:
bash
gcloud compute instances create my-vm \
  --zone=us-central1-a \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud
通过SSH连接实例:
bash
gcloud compute ssh my-vm --zone=us-central1-a
停止/启动实例:
bash
gcloud compute instances stop my-vm --zone=us-central1-a
gcloud compute instances start my-vm --zone=us-central1-a
删除实例:
bash
gcloud compute instances delete my-vm --zone=us-central1-a

Cloud Run

Cloud Run

List services:
bash
gcloud run services list
Deploy from source:
bash
gcloud run deploy my-service --source . --region=us-central1
Deploy container:
bash
gcloud run deploy my-service \
  --image=gcr.io/PROJECT/IMAGE \
  --region=us-central1 \
  --allow-unauthenticated
View logs:
bash
gcloud run services logs read my-service --region=us-central1
Update traffic split:
bash
gcloud run services update-traffic my-service \
  --to-revisions=LATEST=100 \
  --region=us-central1
列出服务:
bash
gcloud run services list
从源码部署:
bash
gcloud run deploy my-service --source . --region=us-central1
部署容器:
bash
gcloud run deploy my-service \
  --image=gcr.io/PROJECT/IMAGE \
  --region=us-central1 \
  --allow-unauthenticated
查看日志:
bash
gcloud run services logs read my-service --region=us-central1
更新流量分配:
bash
gcloud run services update-traffic my-service \
  --to-revisions=LATEST=100 \
  --region=us-central1

Cloud Functions

Cloud Functions

List functions:
bash
gcloud functions list
Deploy function (2nd gen):
bash
gcloud functions deploy my-function \
  --gen2 \
  --runtime=nodejs20 \
  --region=us-central1 \
  --trigger-http \
  --entry-point=handler \
  --source=.
View logs:
bash
gcloud functions logs read my-function --region=us-central1
Delete function:
bash
gcloud functions delete my-function --region=us-central1
列出函数:
bash
gcloud functions list
部署函数(第二代):
bash
gcloud functions deploy my-function \
  --gen2 \
  --runtime=nodejs20 \
  --region=us-central1 \
  --trigger-http \
  --entry-point=handler \
  --source=.
查看日志:
bash
gcloud functions logs read my-function --region=us-central1
删除函数:
bash
gcloud functions delete my-function --region=us-central1

Google Kubernetes Engine (GKE)

Google Kubernetes Engine (GKE)

List clusters:
bash
gcloud container clusters list
Get credentials for kubectl:
bash
gcloud container clusters get-credentials my-cluster \
  --zone=us-central1-a
Create cluster:
bash
gcloud container clusters create my-cluster \
  --zone=us-central1-a \
  --num-nodes=3
Resize node pool:
bash
gcloud container clusters resize my-cluster \
  --node-pool=default-pool \
  --num-nodes=5 \
  --zone=us-central1-a
列出集群:
bash
gcloud container clusters list
获取kubectl凭据:
bash
gcloud container clusters get-credentials my-cluster \
  --zone=us-central1-a
创建集群:
bash
gcloud container clusters create my-cluster \
  --zone=us-central1-a \
  --num-nodes=3
调整节点池大小:
bash
gcloud container clusters resize my-cluster \
  --node-pool=default-pool \
  --num-nodes=5 \
  --zone=us-central1-a

Cloud Storage

云存储

List buckets:
bash
gcloud storage buckets list
Create bucket:
bash
gcloud storage buckets create gs://my-bucket --location=us-central1
List objects:
bash
gcloud storage ls gs://my-bucket/
Copy files:
bash
undefined
列出存储桶:
bash
gcloud storage buckets list
创建存储桶:
bash
gcloud storage buckets create gs://my-bucket --location=us-central1
列出对象:
bash
gcloud storage ls gs://my-bucket/
复制文件:
bash
undefined

Upload

上传

gcloud storage cp local-file.txt gs://my-bucket/
gcloud storage cp local-file.txt gs://my-bucket/

Download

下载

gcloud storage cp gs://my-bucket/file.txt ./
gcloud storage cp gs://my-bucket/file.txt ./

Recursive

递归复制

gcloud storage cp -r ./local-dir gs://my-bucket/

Sync directory:
```bash
gcloud storage rsync -r ./local-dir gs://my-bucket/remote-dir
gcloud storage cp -r ./local-dir gs://my-bucket/

同步目录:
```bash
gcloud storage rsync -r ./local-dir gs://my-bucket/remote-dir

Cloud SQL

Cloud SQL

List instances:
bash
gcloud sql instances list
Create instance:
bash
gcloud sql instances create my-instance \
  --database-version=POSTGRES_15 \
  --tier=db-f1-micro \
  --region=us-central1
Connect via proxy:
bash
gcloud sql connect my-instance --user=postgres
Create database:
bash
gcloud sql databases create mydb --instance=my-instance
列出实例:
bash
gcloud sql instances list
创建实例:
bash
gcloud sql instances create my-instance \
  --database-version=POSTGRES_15 \
  --tier=db-f1-micro \
  --region=us-central1
通过代理连接:
bash
gcloud sql connect my-instance --user=postgres
创建数据库:
bash
gcloud sql databases create mydb --instance=my-instance

BigQuery

BigQuery

List datasets:
bash
bq ls
Run query:
bash
bq query --use_legacy_sql=false 'SELECT * FROM dataset.table LIMIT 10'
Create dataset:
bash
bq mk --dataset my_dataset
Load data:
bash
bq load --source_format=CSV my_dataset.my_table gs://bucket/data.csv
列出数据集:
bash
bq ls
运行查询:
bash
bq query --use_legacy_sql=false 'SELECT * FROM dataset.table LIMIT 10'
创建数据集:
bash
bq mk --dataset my_dataset
加载数据:
bash
bq load --source_format=CSV my_dataset.my_table gs://bucket/data.csv

Pub/Sub

Pub/Sub

List topics:
bash
gcloud pubsub topics list
Create topic:
bash
gcloud pubsub topics create my-topic
Publish message:
bash
gcloud pubsub topics publish my-topic --message="Hello"
Create subscription:
bash
gcloud pubsub subscriptions create my-sub --topic=my-topic
Pull messages:
bash
gcloud pubsub subscriptions pull my-sub --auto-ack
列出主题:
bash
gcloud pubsub topics list
创建主题:
bash
gcloud pubsub topics create my-topic
发布消息:
bash
gcloud pubsub topics publish my-topic --message="Hello"
创建订阅:
bash
gcloud pubsub subscriptions create my-sub --topic=my-topic
拉取消息:
bash
gcloud pubsub subscriptions pull my-sub --auto-ack

Secret Manager

密钥管理器

List secrets:
bash
gcloud secrets list
Create secret:
bash
echo -n "my-secret-value" | gcloud secrets create my-secret --data-file=-
Access secret:
bash
gcloud secrets versions access latest --secret=my-secret
Add new version:
bash
echo -n "new-value" | gcloud secrets versions add my-secret --data-file=-
列出密钥:
bash
gcloud secrets list
创建密钥:
bash
echo -n "my-secret-value" | gcloud secrets create my-secret --data-file=-
访问密钥:
bash
gcloud secrets versions access latest --secret=my-secret
添加新版本:
bash
echo -n "new-value" | gcloud secrets versions add my-secret --data-file=-

IAM

IAM

List service accounts:
bash
gcloud iam service-accounts list
Create service account:
bash
gcloud iam service-accounts create my-sa \
  --display-name="My Service Account"
Create key:
bash
gcloud iam service-accounts keys create key.json \
  --iam-account=my-sa@PROJECT.iam.gserviceaccount.com
Add IAM binding:
bash
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:my-sa@PROJECT.iam.gserviceaccount.com" \
  --role="roles/storage.admin"
列出服务账号:
bash
gcloud iam service-accounts list
创建服务账号:
bash
gcloud iam service-accounts create my-sa \
  --display-name="My Service Account"
创建密钥:
bash
gcloud iam service-accounts keys create key.json \
  --iam-account=my-sa@PROJECT.iam.gserviceaccount.com
添加IAM绑定:
bash
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:my-sa@PROJECT.iam.gserviceaccount.com" \
  --role="roles/storage.admin"

Cloud Build

Cloud Build

Submit build:
bash
gcloud builds submit --tag gcr.io/PROJECT/IMAGE
List builds:
bash
gcloud builds list
View build logs:
bash
gcloud builds log BUILD_ID
提交构建:
bash
gcloud builds submit --tag gcr.io/PROJECT/IMAGE
列出构建任务:
bash
gcloud builds list
查看构建日志:
bash
gcloud builds log BUILD_ID

Artifact Registry

制品仓库

List repositories:
bash
gcloud artifacts repositories list
Configure Docker:
bash
gcloud auth configure-docker us-central1-docker.pkg.dev
列出仓库:
bash
gcloud artifacts repositories list
配置Docker:
bash
gcloud auth configure-docker us-central1-docker.pkg.dev

Logging

日志服务

Read logs:
bash
gcloud logging read "resource.type=cloud_run_revision" --limit=50
Tail logs:
bash
gcloud logging tail "resource.type=gce_instance"
读取日志:
bash
gcloud logging read "resource.type=cloud_run_revision" --limit=50
实时查看日志:
bash
gcloud logging tail "resource.type=gce_instance"

App Engine

App Engine

Deploy app:
bash
gcloud app deploy
View logs:
bash
gcloud app logs tail
Browse app:
bash
gcloud app browse
部署应用:
bash
gcloud app deploy
查看日志:
bash
gcloud app logs tail
浏览应用:
bash
gcloud app browse

Useful Flags

实用参数

Format as JSON:
bash
gcloud compute instances list --format=json
Format as table with specific columns:
bash
gcloud compute instances list --format="table(name,zone,status)"
Filter results:
bash
gcloud compute instances list --filter="status=RUNNING"
Quiet mode (no prompts):
bash
gcloud compute instances delete my-vm --quiet
以JSON格式输出:
bash
gcloud compute instances list --format=json
以表格格式输出指定列:
bash
gcloud compute instances list --format="table(name,zone,status)"
过滤结果:
bash
gcloud compute instances list --filter="status=RUNNING"
静默模式(无提示):
bash
gcloud compute instances delete my-vm --quiet

Cheat Sheet

速查表

Quick reference:
bash
gcloud cheat-sheet
Interactive shell:
bash
gcloud interactive
快速参考:
bash
gcloud cheat-sheet
交互式Shell:
bash
gcloud interactive