gcloud平台sdk使用

Google Cloud SDK 这套工具可用于管理托管在 Google Cloud Platform 上的资源和应用

工具包括: gcloud gsutil bq

这些工具也称为组件,它们是SDK自带的默认组件

gcloud是默认的** gcloud CLI **命令,用来与 Google Cloud Platform 进行交互的工具。

bqBigQuery 命令行工具,用于处理 Google BigQuery 中的数据的工具

gsutilCloud Storage 命令行工具,用于执行 Google Cloud Storage 的相关任务的工具。

coreCloud SDK 核心库,SDK 工具内部使用的库。

Cloud SDK 组件

查看组件列表

gcloud components list

安装组件

gcloud components install

比如安装kubectl组件

gcloud components install kubectl

更新组件

gcloud components update

移除组件

gcloud components remove

其他组件 app-engine-python,gcloud 应用 Python 扩展程序,适用于 Python 的 Google App Engine SDK 软件包。当您使用 gcloud app deploy 命令将 Python 应用部署到 App Engine 时,系统会安装此组件。

授权

  1. 服务账号授权
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 初始化gcloud
gcloud init

# 服务账号授权
# 进入Google Cloud Platform Console 创建账号
# https://console.cloud.google.com/iam-admin/serviceaccounts?_ga=2.134046219.-44660240.1561111218
# 创建账号输入账号名称 max-iki
# 输入服务账号说明 本地max机器iki用户
# 下一步
# 选择角色 选择项目所有者
# 授予用户访问此服务帐号的权限 输入google账号 wx11055@gmail.com
# 注意:创建密钥选择json
# 点击完成 会下载一个json文件
# 回到本机电脑

gcloud auth activate-service-account --key-file my-k8s-codelab-225201-959b862c2260.json
 
##会看到 Activated service account credentials for: [max-iki@my-k8s-codelab-225201.iam.gserviceaccount.com]
  1. 用户账号授权
1
2
3
4
5
# 命令行输入 弹出浏览器窗口 确认授权
gcloud auth login

# 设置用户
gcloud config set account <account>

核心命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 初始化命令
gcloud init --console-only

# 查看系统凭据账号
gcloud auth list

# 查看配置列表
gcloud config list

# 查看cloud sdk 安装实例和活动sdk配置信息
gcloud info

# 更多
gcloud help
# 比如
gcloud help compute instances create

# 显示有效配置
gcloud config configurations list

# 设置配置属性
# 设置项目ID 注意这里要填项目ID  像这样 my-k8s-codelab-225201
gcloud config set project my-k8s-codelab-225201
gcloud config unset disable_usage_reporting
# 设置时区
gcloud config set compute/zone asia-east2
# 查看配置属性
gcloud config configurations describe <NAME>

# 删除配置
gcloud config configurations delete <NAME>

# 设置代理
gcloud config set proxy/type http

gcloud config set proxy/address 127.0.0.1

gcloud config set proxy/port 59173

gcloud config set proxy/username <USERNAME>
gcloud config set proxy/password <PASSWORD>

# 取消设置属性
gcloud config unset proxy/type
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 查询地区在asia-east1-a 的实例
gcloud compute instances list --filter="zone:asia-east1-a"

# 以 JSON 格式列出标签与特定值匹配的项目
gcloud projects list --format="json" \
    --filter="labels.env=test AND labels.version=alpha"
    
# 列出使用本地时区指定了创建日期和时间的项目
gcloud projects list \
    --format="table(name, project_id, createTime.date(tz=LOCAL))"
    
# 以表格式列出在特定日期后创建的项目
gcloud projects list \
    --format="table(projectNumber,projectId,createTime)" \
    --filter="createTime.date('%Y-%m-%d', Z)='2016-05-11'"

API参考

1
2
3
4
5
6
7
# 进入项目目录

# 发布应用
gcloud app deploy

# 浏览应用
gcloud app browse