Google Cloud SDK/Cli 使用

发布时间: 更新时间: 总字数:780 阅读时间:2m 作者: IP上海 分享 网址

Google Cloud SDK/Cli 使用

google apis-explorer

在线 api 调试工具:

https://developers.google.com/apis-explorer

安装google-cloud-sdk

google-cli 是 google-cloud-sdk

Mac

brew install google-cloud-sdk

使用

初始化

gcloud init  # 按照提示操作
gcloud init --skip-diagnostics

...Your current project has been set to: [gcp-poc-2020].

Your project default Compute Engine zone has been set to [asia-east2-a].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [asia-east2].
You can change it by running [gcloud config set compute/region NAME].

Created a default .boto configuration file at [/Users/xiexianbin/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use xxianbin@gmail.com by default
* Commands will reference project `gcp-poc-2020` by default
* Compute Engine commands will use region `asia-east2` by default
* Compute Engine commands will use zone `asia-east2-a` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting

使用

查看配置

➜  ~ gcloud info
Google Cloud SDK [313.0.1]

Platform: [Mac OS X, x86_64] uname_result(system='Darwin', node='xiexianbin.local', release='19.6.0', version='Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64', machine='x86_64', processor='i386')
Locale: ('en_US', 'UTF-8')
Python Version: [3.8.6 (default, Oct  8 2020, 14:06:32)  [Clang 12.0.0 (clang-1200.0.32.2)]]
Python Location: [/usr/local/bin/python3]
Site Packages: [Disabled]

Installation Root: [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk]
Installed Components:
  gsutil: [4.53]
  core: [2020.10.07]
  bq: [2.0.61]
System PATH: [/usr/local/sbin:/Library/Java/JavaVirtualMachines/jdk-11.0.3.jdk/Contents/Home//bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/go/bin:/Users/xiexianbin/work/code/go/bin:/usr/local/gradle-6.0.1/bin:/usr/local/apache-maven-3.6.3/bin]
Python PATH: [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/third_party:/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib:/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python38.zip:/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8:/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload]
Cloud SDK on PATH: [False]
Kubectl on PATH: [/usr/local/bin/kubectl]

Installation Properties: [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/properties]
User Config Directory: [/Users/xiexianbin/.config/gcloud]
Active Configuration Name: [default]
Active Configuration Path: [/Users/xiexianbin/.config/gcloud/configurations/config_default]

Account: [xxianbin@gmail.com]
Project: [gcp-poc-2020]

Current Properties:
  [compute]
    zone: [asia-east2-a]
    region: [asia-east2]
  [core]
    account: [xxianbin@gmail.com]
    disable_usage_reporting: [True]
    project: [gcp-poc-2020]
  [proxy]
    address: [127.0.0.1]
    port: [8001]
    type: [http]

Logs Directory: [/Users/xiexianbin/.config/gcloud/logs]
Last Log File: [/Users/xiexianbin/.config/gcloud/logs/2020.10.25/11.35.46.353053.log]

git: [git version 2.28.0]
ssh: [OpenSSH_8.1p1, LibreSSL 2.7.3]

➜  ~ gcloud config list
[compute]
region = asia-east2
zone = asia-east2-a
[core]
account = xxianbin@gmail.com
disable_usage_reporting = True
project = gcp-poc-2020
[proxy]
address = 127.0.0.1
port = 8001
type = http

Your active configuration is: [default]

查看项目

gcloud projects list

查看实例

gcloud compute instances list --filter="zone:asia-east2-a"

示例

Google Kubernetes Engine

gcloud beta container \
  --project "gcp-poc-2020" \
  clusters create "cluster-1" \
  --zone "asia-east2-a" \
  --no-enable-basic-auth \
  --cluster-version "1.16.13-gke.401" \
  --machine-type "e2-medium" \
  --image-type "COS" \
  --disk-type "pd-standard" \
  --disk-size "20" \
  --metadata disable-legacy-endpoints=true \
  --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
  --num-nodes "1" \
  --enable-stackdriver-kubernetes \
  --enable-ip-alias \
  --network "projects/gcp-poc-2020/global/networks/default" \
  --subnetwork "projects/gcp-poc-2020/regions/asia-east2/subnetworks/default" \
  --default-max-pods-per-node "30" \
  --no-enable-master-authorized-networks \
  --addons HorizontalPodAutoscaling,HttpLoadBalancing \
  --enable-autoupgrade \
  --enable-autorepair \
  --max-surge-upgrade 1 \
  --max-unavailable-upgrade 0
gcloud container clusters get-credentials cluster-1 --zone asia-east2-a --project gcp-poc-2020
gcloud beta container \
  --project "gcp-poc-2020" \
  clusters delete "cluster-1"

镜像仓库

$ gcloud container images list --page-size=500 --repository gcr.io/ml-pipeline

$ gcloud container images list --project google-containers --log-http

$ gcloud container images list-tags gcr.io/google-containers/kube-apiserver --limit=999999 --sort-by=TIMESTAMP --filter="timestamp.datetime < '2022-02-01'" --log-http

$ gcloud container images list-tags gcr.io/google-containers/kube-apiserver --limit=1 --filter "tags:*" --format=yaml
---
digest: sha256:e33e1717da50fca491d535811a71c3dbb5d519d8448b97fa9f25f522c5b1c528
tags:
- v1.12.0-beta.1
timestamp:
  datetime: '2018-09-05 11:55:52+08:00'
  day: 5
  hour: 11
  microsecond: 0
  minute: 55
  month: 9
  second: 52
  year: 2018

$ gcloud container images list-tags gcr.io/google-containers/kube-apiserver --limit=1 --filter "tags:* AND timestamp.datetime > '2021-12-31'" --format=yaml

$ gcloud container images list-tags k8s.gcr.io/kube-apiserver --limit=1 --filter "tags:* AND timestamp.datetime > '2021-12-31'" --format=yaml

$ gcloud container images list-tags k8s.gcr.io/kube-apiserver --limit=1 --format=yaml
---
digest: sha256:3dcf0be8f62116b381a5f6186d38808fcf28b947216cb68169063fc694a785b8
tags: []
timestamp:
  datetime: '2022-02-16 21:15:01+08:00'
  day: 16
  hour: 21
  microsecond: 0
  minute: 15
  month: 2
  second: 1
  year: 2022

$ gcloud container images list-tags k8s.gcr.io/kube-apiserver --limit=1 --filter "tags:*" --format=yaml
---
digest: sha256:e33e1717da50fca491d535811a71c3dbb5d519d8448b97fa9f25f522c5b1c528
tags:
- v1.12.0-beta.1
timestamp:
  datetime: '2018-09-05 11:55:52+08:00'
  day: 5
  hour: 11
  microsecond: 0
  minute: 55
  month: 9
  second: 52
  year: 2018

Compute Engine

gcloud beta compute \
  --project=gcp-poc-2020 \
  instances create instance-1 \
  --zone=asia-east2-a \
  --machine-type=e2-medium \
  --subnet=default \
  --network-tier=PREMIUM \
  --maintenance-policy=MIGRATE \
  --service-account=664104358634-compute@developer.gserviceaccount.com \
  --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append \
  --image=centos-7-v20201014 \
  --image-project=centos-cloud \
  --boot-disk-size=20GB \
  --boot-disk-type=pd-standard \
  --boot-disk-device-name=instance-1 \
  --no-shielded-secure-boot \
  --shielded-vtpm \
  --shielded-integrity-monitoring \
  --reservation-affinity=any
gcloud compute instances list
gcloud compute instances start <instance-name>

FAQ

依赖问题

AttributeError: module 'importlib' has no attribute 'util'

解决方式:

export CLOUDSDK_PYTHON=python2
gcloud components update

参考

  1. https://cloud.google.com/sdk/docs/quickstart
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数