LoginSignup
85

More than 5 years have passed since last update.

Google Cloud Platformをローカルから利用するための準備

Last updated at Posted at 2015-09-21

前提条件

  • GCP利用開始手続き済み
    • 1つのプロジェクト、1つのGCEインスタンスを作成済
  • ローカルはMac OS X 10.10.5

Cloud SDKのインストール

GCPをシェルから利用するためのSDKをインストールします。

gcloud Tool Guide - Cloud SDK — Google Cloud Platform

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

上記コマンドでインストール開始。

Installation directory (this will create a google-cloud-sdk subdirectory) (/Users/user): 

/Users/[ユーザ名] にインストールしていいか聞かれるので、変更する場合は任意のディレクトリを入力。

To help improve the quality of this product, we collect anonymized data on how
the SDK is used. 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 --scope=user disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)?  n

改善のためにレポートを送ってよいか。
私は「いいえ」を選択。

Modify profile to update your $PATH and enable shell command 
completion? (Y/n)? y

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/user/.bash_profile]: 

パスを通してタブ補完を有効にするか。
「はい」の場合は書き換え対象のrcファイルを聞かれる。

$ exec -l $SHELL
$ gcloud version
Google Cloud SDK 0.9.78

bq 2.0.18
bq-nix 2.0.18
core 2015.09.11
core-nix 2015.09.03
gcloud 2015.09.11
gsutil 4.15
gsutil-nix 4.14

シェルを再起動して完了。

アカウントへの紐付け

gcloudコマンドをGCPのアカウントに紐付けます。

$ gcloud auth login
Your browser has been opened to visit:

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

gcloud auth login を事項すると認証ページがブラウザに表示されるので、GCPを使用するアカウントでログインを行います。

Saved Application Default Credentials.

You are now logged in as [認証に使用したアカウント].
Your current project is [None].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

認証を行うと上記のメッセージが表示され、認証が完了します。

$ gcloud auth list
Credentialed accounts:
 - [認証に使用したアカウント] (active)

To set the active account, run:
  $ gcloud config set account ``ACCOUNT''

gcloud auth list で認証済みのアカウントのリストが見られます。
現在使用中のアカウントには (active) がついています。
複数のアカウントを利用している場合は gcloud config set accountACCOUNT'' で使用するアカウントの切り替えができるようです。

これ、ちょっと変わった認証方法ですが、
ID、パスの入力必要ないしシンプルなのでどこかで真似したいですね。

SSH接続の準備

GCEはブラウザからSSH接続できたりするんですが、
通常のSSH接続もできないと不便なので、そのための準備もしておきます。

$ ssh-keygen -t rsa -C "任意のアカウント"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa): /Users/user/.ssh/id_rsa.gcp
...
$ pbcopy < ~/.ssh/id_rsa.gcp.pub 

ssh-keygen -t rsa -C で鍵を生成し、クリップボードにコピペして、
GCEインスタンスの詳細画面にある「SSHキー」の項目に追加します。

Kobito.MdvY1d.png

GCEの場合、これだけでインスタンス内にユーザが作成されるので、
これでログインの準備は完了です。

$ ssh -i ~/.ssh/id_rsa.gcp [任意のアカウント]@[インスタンスの外部IP]

所感

SDKの認証や公開鍵の登録(からのインスタンス内ユーザ作成)など、
シンプルで使いやすいプラットフォームだと感じました。

AWSに比べると、ユーザ向けの(Google Appsなどの)プロダクトのユーザビリティに寄せてあるように思います。

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
85