Google Cloud SDKの初期化方法

GCP(Google Cloud Platform)のGoogle Cloud SDKの初期化方法です。

インストール

インストール要件

  • Python 2.7.x

対話型インストーラを使用して、Linux、macOSに最新バージョンをインストール

$ curl https://sdk.cloud.google.com | bash
$ exec -l $SHELL
$ gcloud -v
Google Cloud SDK 185.0.0
bq 2.0.28
core 2018.01.12
gsutil 4.28

その他のインストール方法はこちら

認証と承認

Google Cloud SDKは認証と承認にOAuth2を使用しています。
承認タイプはユーザー アカウント(3-legged OAuth)とサービス アカウント(2-legged OAuth)の2種類あります。

承認タイプ

タイプ 説明
ユーザー アカウント Cloud SDK ツールをコマンドラインで使用する場合や、スクリプト化して 1 台のマシンで使用する場合にお勧めします。
サービス アカウント Cloud SDK を本番環境へのマシンのデプロイの一環としてインストールして設定する場合や、すべてのユーザーが root にアクセスできる Google Compute Engine 仮想マシン インスタンスで使用する場合にお勧めします。

Cloud SDK ツールの承認

ユーザー アカウントを使用

ブラウザがあるマシンでの初期化

手順

  1. Google Cloud SDKをインストールしたマシンで「gcloud init」を実行します。
  2. ブラウザが立ち上がります。
  3. アカウントを選択し、Google Cloud SDKのアクセスを許可します。
  4. プロジェクトを選択するか、新規作成します。
$ gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

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 (1/1 checks) passed.

You must log in to continue. Would you like to log in (Y/n)?  y

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?...


You are logged in as: [...].

Pick cloud project to use: 
 [1] project1
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1

Your current project has been set to: [project1].

...

ブラウザがないマシンでの初期化

手順

  1. Google Cloud SDKをインストールしたマシンで「gcloud init –console-only」を実行します。
  2. URL「https://accounts.google.com/o/oauth2/auth?…」をローカルマシンのブラウザで開きます。
  3. アカウントを選択し、Google Cloud SDKのアクセスを許可します。
  4. コードが表示されるので、コンソールに貼り付けます。
  5. プロジェクトを選択するか、新規作成します。
$ gcloud init --console-only
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

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 (1/1 checks) passed.

You must log in to continue. Would you like to log in (Y/n)?  Y

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?...


Enter verification code: ...
You are logged in as: [...].

Pick cloud project to use:
 [1] project1
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list
item):  1

Your current project has been set to: [project1].

...

サービスアカウントを使用

  1. 「IAM と管理」 – 「サービス アカウント」 – 「サービス アカウントを作成」からサービスアカウントを作成して、鍵ファイルをダウンロードします。
  2. 「gcloud auth activate-service-account –key-file」を実行します。–key-fileにはダウンロードした鍵ファイルを指定します。
$ gcloud auth activate-service-account --key-file ...
Activated service account credentials for: [...]

タイトルとURLをコピーしました