星期六, 5月 18, 2019

建立 gcloud 設定與切換設定小記

建立 gcloud 設定與切換設定小記

OS: openSUSE Leap 15
Google Cloud SDK : 246.0.0

今天來進行 Google Cloud SDK ( gcloud ) 專案相關設定

之前的文章, 安裝 Google Cloud SDK

在安裝的過程當中有進行初始化動作
>gcloud  init

當時選擇了自己的第一個專案

建立完成之後, 查詢的方式為
> gcloud  config  configurations list
NAME     IS_ACTIVE  ACCOUNT          PROJECT  DEFAULT_ZONE DEFAULT_REGION
default  True  xxxxx@gmail.com  steadfast-task-228409  asia-east1-a asia-east1

但是在 GCP 上面, 專案又只會何止一個??
所以接下來就是要設定其他的專案然後實做如何切換

新增第二個專案, 再次執行
>gcloud  init

Welcome! This command will take you through the configuration of gcloud.

Settings from your current configuration [default] are:
compute:
 region: asia-east1
 zone: asia-east1-a
core:
 account: xxxxxx@gmail.com
 disable_usage_reporting: 'True'
 project: steadfast-task-238409

Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice:   2

這邊點選 2 建立新專案

Enter configuration name. Names start with a lower case letter and
contain only lower case letters a-z, digits 0-9, and hyphens '-':  sakanatest
Your current configuration has been set to: [sakanatest]

You can skip diagnostics next time by using the following flag:
 gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                                                                               
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

Choose the account you would like to use to perform operations for
this configuration:
[1] xxxxxx@gmail.com
[2] Log in with a new account
Please enter your numeric choice:  1

輸入設定檔名稱以及選擇登入帳號

Pick cloud project to use:
[1] employee-mgr-sakana
[2] sakanatest
[3] Create a new project
Please enter numeric choice or text value (must exactly match list
item):  2

選取專案名稱

到此, 設定完成, 再次觀察設定

> gcloud  config  configurations  list

NAME        IS_ACTIVE ACCOUNT              PROJECT DEFAULT_ZONE  DEFAULT_REGION
default        False xxxxxx@gmail.com  steadfast-task-228409 asia-east1-a  asia-east1
sakanatest  True       xxxxxx@gmail.com  sakanatest

目前作用中的是 sakanatest 這個設定檔, 對應的專案是 sakanatest
切換設定檔的方式

> gcloud  config  configurations activate default

Activated [default].
  • activate 後面接設定檔的名稱

再次觀察
> gcloud  config  configurations  list

NAME        IS_ACTIVE ACCOUNT              PROJECT DEFAULT_ZONE  DEFAULT_REGION
default        True      xxxxxx@gmail.com  steadfast-task-228409  asia-east1-a asia-east1
sakanatest False       xxxxxx@gmail.com sakanatest


建立 openSUSE Leap 15 instances 測試一下
> gcloud  compute  instances create  test20190518  --image-project  opensuse-cloud --image opensuse-leap-15-v20181106 --zone  asia-east1-a

列出 compute engine list
> gcloud  compute  instances list

使用 gcloud 連線 ssh 到 GCE
> gcloud  compute  ssh  test20190518

  • 會在 ~/.ssh 目錄建立SSH 金鑰 google_compute_engine  / google_compute_engine.pub 以及 google_compute_known_hosts
  • 可以在專案的中繼資料  -- > SSH 金鑰中觀察到


好處就是不需要用瀏覽器的方式連線

後記:
今天在查詢 google could sdk 版本的時候被通知可以升級版本
指令如下
> gcloud  components  update

下完指令就可以輕鬆升級 google cloud sdk 了

今天就先進行專案設定切換, ssh 連線 VM
再往前進一步

~ enjoy it

Reference:

沒有留言: