114
92

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Cloud Platform その1Advent Calendar 2018

Day 14

GCPのgcloudコマンドのインストールと最初の認証までを初心者向けに細かく解説

Last updated at Posted at 2018-12-18

最近AWSだけではなく、マルチクラウドも当たり前につかえておかないと生きていけなそうなのでそろそろGCPを使い始めました。今回はGCPのリソースをCLIで操作できるgcloudコマンドのインストールと初期設定について初心者向けに細かく説明していきます。

gcloudコマンドをインストール

基本的には以下のコマンド一発で gcloud コマンドをインストールすることができます。
Google Cloud SDKをインストールするとついでに gcloudコマンドも使えるといった感じのようですね。

$ curl https://sdk.cloud.google.com | bash

それにしてもcurlでシェルスクリプト取得してインストールさせるなんておしゃれなことするな。Googleさん

$ curl https://sdk.cloud.google.com
#!/bin/bash

URL=https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash

function download {
  scratch="$(mktemp -d -t tmp.XXXXXXXXXX)" || exit
  script_file="$scratch/install_google_cloud_sdk.bash"

  echo "Downloading Google Cloud SDK install script: $URL"
  curl -# "$URL" > "$script_file" || exit
  chmod 775 "$script_file"

  echo "Running install script from: $script_file"
  "$script_file" "$@"
}

download "$@"

macOS の場合は Google Cloud SDK は Homebrew Cask からインストールできるようです。

$ brew cask install google-cloud-sdk

さっそくインストールしていきましょう。
途中で色々聞かれますが基本的にはYesを答えていけばよいです。Enterキー押しまくったらOKです。

$ curl https://sdk.cloud.google.com | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   443    0   443    0     0   1381      0 --:--:-- --:--:-- --:--:--  1909
Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash
######################################################################## 100.0%
Running install script from: /var/folders/0w/fhc_kcts3rz9r36c9qlx2vkr0000gn/T/tmp.XXXXXXXXXX.VPSuXP9n/install_google_cloud_sdk.bash
which curl
curl -# -f https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz
######################################################################## 100.0%

Installation directory (this will create a google-cloud-sdk subdirectory) (/Users/daisuke):
mkdir -p /Users/daisuke
tar -C /Users/daisuke -zxvf /var/folders/0w/fhc_kcts3rz9r36c9qlx2vkr0000gn/T/tmp.XXXXXXXXXX.nExxemcm/google-cloud-sdk.tar.gz
x google-cloud-sdk/
x google-cloud-sdk/lib/
x google-cloud-sdk/lib/third_party/
==========================================================
~ 略 ~
==========================================================
x google-cloud-sdk/.install/core.manifest
x google-cloud-sdk/.install/.download/

/Users/daisuke/google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)? # <--------------------ここで "Enter"


This will install all the core command line tools necessary for working with
the Google Cloud Platform.



Your current Cloud SDK version is: 228.0.0
Installing components from version: 228.0.0

┌────────────────────────────────────────────────────────────────────────────┐
│                    These components will be installed.                     │
├─────────────────────────────────────────────────────┬────────────┬─────────┤
│                         Name                        │  Version   │   Size  │
├─────────────────────────────────────────────────────┼────────────┼─────────┤
│ BigQuery Command Line Tool                          │     2.0.39 │ < 1 MiB │
│ BigQuery Command Line Tool (Platform Specific)      │     2.0.34 │ < 1 MiB │
│ Cloud SDK Core Libraries (Platform Specific)        │ 2018.09.24 │ < 1 MiB │
│ Cloud Storage Command Line Tool                     │       4.34 │ 3.5 MiB │
│ Cloud Storage Command Line Tool (Platform Specific) │       4.34 │ < 1 MiB │
│ Default set of gcloud commands                      │            │         │
│ gcloud cli dependencies                             │ 2018.08.03 │ 1.5 MiB │
└─────────────────────────────────────────────────────┴────────────┴─────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool                   ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Default set of gcloud commands               ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!


Modify profile to update your $PATH and enable shell command
completion?

Do you want to continue (Y/n)? # <--------------------ここで "Enter"
The Google Cloud SDK installer will now prompt you to update an rc
file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use
[/Users/daisuke/.bash_profile]: # <--------------------ここで "Enter"
Backing up [/Users/daisuke/.bash_profile] to [/Users/daisuke/.bash_profile.backup].
[/Users/daisuke/.bash_profile] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

.bash_profile内に以下が追記されていることを確認します。
ちなみにこのファイルたちは

.bash_profile
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/daisuke/google-cloud-sdk/path.bash.inc' ]; then . '/Users/daisuke/google-cloud-sdk/path.bash.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/daisuke/google-cloud-sdk/completion.bash.inc' ]; then . '/Users/daisuke/google-cloud-sdk/completion.bash.inc'; fi

あとはこの.bash_profileの設定を再読み込みして、gcloudコマンドが実行できることを確認すればOKです🎉

# 再読み込みして
$ source .bash_profile
# gcloudコマンドが叩ければ完了です
$ gcloud version
Google Cloud SDK 228.0.0
bq 2.0.39
core 2018.12.07
gsutil 4.34

初期設定

インストールが終わったら、認証情報などを設定するために gcloud init コマンドを実行しましょう。

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

You must log in to continue. Would you like to log in (Y/n)? # <------------- ここで "Enter"

ログインするように求めらるのでEnter(Y)を押すとログイン画面がブラウザで立ち上がります。

image.png

最後にこの画面までくれば認証は完了です。

image.png

次にプロジェクトを選べと言われますのでここはご自由に。
GCPをコンソール画面から使用したことがある人ならプロジェクトは当然作っているはずですね。
もしプロジェクトを作ったことがなければこのままCLIで作ってしまうこともできます。

You are logged in as: [gee.awa@gmail.com].

Pick cloud project to use:
 [1] global-phalanx-xxxxxxx
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list
item):  # <---------- 1 or 2を入力

あとはリージョンを選べと言われるのでここもご自由にどうぞ

Do you want to configure a default Compute Region and Zone? (Y/n)? <----------- ここで "Enter"

Which Google Compute Engine zone would you like to use as project
default?
If you do not specify a zone via a command line flag while working
with Compute Engine resources, the default is assumed.
 [1] us-east1-b
 [2] us-east1-c
 [3] us-east1-d
 [4] us-east4-c
 [5] us-east4-b
 [6] us-east4-a
 [7] us-central1-c
 [8] us-central1-a
 [9] us-central1-f
 [10] us-central1-b
 [11] us-west1-b
 [12] us-west1-c
 [13] us-west1-a
 [14] europe-west4-a
 [15] europe-west4-b
 [16] europe-west4-c
 [17] europe-west1-b
 [18] europe-west1-d
 [19] europe-west1-c
 [20] europe-west3-b
 [21] europe-west3-c
 [22] europe-west3-a
 [23] europe-west2-c
 [24] europe-west2-b
 [25] europe-west2-a
 [26] asia-east1-b
 [27] asia-east1-a
 [28] asia-east1-c
 [29] asia-southeast1-b
 [30] asia-southeast1-a
 [31] asia-southeast1-c
 [32] asia-northeast1-b
 [33] asia-northeast1-c
 [34] asia-northeast1-a
 [35] asia-south1-c
 [36] asia-south1-b
 [37] asia-south1-a
 [38] australia-southeast1-b
 [39] australia-southeast1-c
 [40] australia-southeast1-a
 [41] southamerica-east1-b
 [42] southamerica-east1-c
 [43] southamerica-east1-a
 [44] asia-east2-a
 [45] asia-east2-b
 [46] asia-east2-c
 [47] europe-north1-a
 [48] europe-north1-b
 [49] europe-north1-c
 [50] northamerica-northeast1-a
Did not print [6] options.
Too many options [56]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list
item):  # <--------------------------------------------------- 1 ~ 50で選ぶ

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

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

Created a default .boto configuration file at [/Users/daisuke/.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 gee.awa@gmail.com by default
* Commands will reference project `global-phalanx-225913` by default
* Compute Engine commands will use region `us-east1` by default
* Compute Engine commands will use zone `us-east1-b` 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ライフを送る準備が整いましたね。
クラウド生活をエンジョイしましょう!:smirk:

114
92
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
114
92

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?