gcloud CLI のインストール(Mac)

gcloud CLI のインストール(Mac)

Macへのgcloud CLIのインストールについて説明します。

目次

gcloud CLI とは

gcloud CLI(gcloudコマンド)は、Google Cloud(GCP)のリソースの作成や管理を行うCLIのツールです。WebブラウザでアクセスしてのGUI操作ではなく、ターミナルソフトでCLI操作が行えるようになります。

gcloud CLI のインストール

homebrewによるインストール

ターミナルで下記のコマンドを実行します。

brew install --cask google-cloud-sdk
Terminal[~]: brew install --cask google-cloud-sdk
〜〜 中略 〜〜
==> Downloading https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz
######################################################################## 100.0%
〜〜 中略 〜〜
==> Installing Cask google-cloud-sdk
==> Running installer script 'google-cloud-sdk/install.sh'
〜〜 中略 〜〜
┌─────────────────────────────────────────────────────────────────────────────┐
│                     These components will be installed.                     │
├─────────────────────────────────────────────────────┬────────────┬──────────┤
│                         Name                        │  Version   │   Size   │
├─────────────────────────────────────────────────────┼────────────┼──────────┤
│ BigQuery Command Line Tool                          │     2.0.74 │  1.0 MiB │
│ BigQuery Command Line Tool (Platform Specific)      │     2.0.73 │  < 1 MiB │
│ Cloud Storage Command Line Tool                     │       5.10 │ 19.3 MiB │
│ Cloud Storage Command Line Tool (Platform Specific) │        5.6 │  < 1 MiB │
│ Google Cloud CLI Core Libraries (Platform Specific) │ 2022.01.28 │  < 1 MiB │
│ anthoscli                                           │     0.2.26 │ 47.8 MiB │
│ gcloud cli dependencies                             │ 2021.04.16 │  < 1 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 Storage Command Line Tool              ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: Default set of gcloud commands               ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: Google Cloud CLI Core Libraries (Platform... ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: anthoscli                                    ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: anthoscli                                    ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Installing: gcloud cli dependencies                      ═╣
╚════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════╗
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...
..................................................................................................................................................................................................................................................................................................................................................................done.

Update done!


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

==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc] in your profile to enable shell command completion for gcloud.
==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH.

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


==> Linking Binary 'bq' to '/usr/local/bin/bq'
==> Linking Binary 'docker-credential-gcloud' to '/usr/local/bin/docker-credential-gcloud'
==> Linking Binary 'gcloud' to '/usr/local/bin/gcloud'
==> Linking Binary 'git-credential-gcloud.sh' to '/usr/local/bin/git-credential-gcloud'
==> Linking Binary 'gsutil' to '/usr/local/bin/gsutil'
==> Linking Binary 'anthoscli' to '/usr/local/bin/anthoscli'
🍺  google-cloud-sdk was successfully installed!

.zshrcの設定

インストール時に指定された通り、.zshrcに追記します。

vi ~/.zshrc
source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"
source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc"

.zshrcを再読み込みします。

source ~/.zshrc

インストール確認

下記のコマンドを実行し、バージョン情報が表示されればインストールは完了です。

gcloud --version
Terminal[~]: gcloud --version
Google Cloud SDK 384.0.0
bq 2.0.74
core 2022.05.02
gsutil 5.10

gcloud CLI の初期設定

ログイン認証

下記のコマンドを実行し、GCPのログイン認証を行います。コマンドを実行すると、Webブラウザが起動するため、ログインを行います。

gcloud auth login
Terminal[~]: gcloud auth login
Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=XXXXXXXXXXX.apps.googleusercontent.com&redirect_uri=httpXXXXXXXXXX


You are now logged in as [XXXXXXXXXXXXXX].
Your current project is [None].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

プロジェクト設定

操作対象とするGCP上のプロジェクトを設定します。

まず、プロジェクトIDを確認します。

gcloud projects list
Terminal[~]: gcloud projects list
PROJECT_ID                   NAME                  PROJECT_NUMBER
gcp-test-project-001-XXXXXX  GCP-Test-Project-001  XXXXXXXXXXX
micro-citadel-XXXXXX         My First Project      XXXXXXXXXXX

プロジェクトを設定します。

gcloud config set project [PROJECT_ID]
Terminal[~]: gcloud config set project gcp-test-project-001-XXXXXX
Updated property [core/project].

設定内容を確認します。

gcloud config list
Terminal[~]: gcloud config list
[core]
account = XXXXXXXXXXXXXX
disable_usage_reporting = True
project = gcp-test-project-001-XXXXXX

Your active configuration is: [default]

参照コマンド実行

例として、Compute Engine(VMインスタンス)を確認するコマンドを実行します。

gcloud compute instances list
Terminal[~]: gcloud compute instances list
NAME                 ZONE               MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
vm-gcp-vpn-test-001  asia-northeast1-b  e2-micro                   10.0.0.100                RUNNING

以上で、gcloud CLI のインストール(Mac)の説明は完了です!

  • URLをコピーしました!
  • URLをコピーしました!
目次