Google Compute Engine事始め

佐藤さんの講演を聴いて、プロモーションコードをゲットしたことだし*1、GCEを少し試してみることにした。中身を見ずに「Google Compute Engine入門」という書籍も買ってみたのだけど、すでに内容が古くなっている気が。。。本書ではgutilというCLIツールを使っているのだけど、すでにgutilからgcloudというツールに乗り換えることが推奨されている。

この辺を見ながらgcloudを含むSDKをインストール。

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

GCEを使うためのOAuthの設定のためにgcloud auth loginを実行する。実行すると勝手にブラウザが認証用ページに飛び、よしなにしてくれる。

$ gcloud auth login

Developers consoleでプロジェクトを作り、早速インスタンスを立ち上げてみる。asia-east1-cってゾーンがあるね。東アジアリージョンは比較的新しいので、IvyBridgeのサーバを使っているようだ。自宅からだとpingで50ms(どこかで台湾と読んだが、本当か?)。OSはCentOS 7を選択。コマンドラインから実行する場合は、次のようになる。

$ gcloud compute instances create instance-1 --machine-type f1-micro --image centos-7 --zone asia-east1-c --project  shaped-producer-XXX
NAME       ZONE         MACHINE_TYPE INTERNAL_IP   EXTERNAL_IP     STATUS
instance-1 asia-east1-c f1-micro     10.240.192.83 XXX.XXX.XXX.XXX RUNNING

なお、選択可能なインスタンスタイプ(machine-types)は次のコマンドで調べることができる。OSイメージはEC2ほど選択肢はない。

$ gcloud compute machine-types list --zone asia-east1-c --project shaped-producer-XXX
NAME           ZONE         CPUS MEMORY_GB DEPRECATED
f1-micro       asia-east1-c 1     0.60
g1-small       asia-east1-c 1     1.70
n1-highcpu-16  asia-east1-c 16   14.40
n1-highcpu-2   asia-east1-c 2     1.80
n1-highcpu-4   asia-east1-c 4     3.60
n1-highcpu-8   asia-east1-c 8     7.20
n1-highmem-16  asia-east1-c 16   104.00
n1-highmem-2   asia-east1-c 2    13.00
n1-highmem-4   asia-east1-c 4    26.00
n1-highmem-8   asia-east1-c 8    52.00
n1-standard-1  asia-east1-c 1     3.75
n1-standard-16 asia-east1-c 16   60.00
n1-standard-2  asia-east1-c 2     7.50
n1-standard-4  asia-east1-c 4    15.00
n1-standard-8  asia-east1-c 8    30.00

(Webコンソールから)インスタンスが起動すると、ダッシュボードにインスタンスが表示されるので、SSHというボタンを押す。すると、「gcloud compute --project "shaped-producer-XXX" ssh --zone "asia-east1-c" "instance-1"」のような文字列がポップアップされるので、これをターミナルにコピペする。SSH公開鍵を登録してない場合は、新たに生成されるようだ。このようなSSHのラッパーのほか、ファイルを転送するためのpush/pullコマンドなども用意されている。

$ gcloud compute --project "shaped-producer-XXX" ssh --zone "asia-east1-c" "instance-1"
WARNING: You do not have an SSH key for Google Compute Engine.
WARNING: [/usr/bin/ssh-keygen] will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/oraccha/.ssh/google_compute_engine.
Your public key has been saved in /Users/oraccha/.ssh/google_compute_engine.pub.
The key fingerprint is:
3d:8c:58:eb:c7:6d:9a:ae:af:59:e6:db:81:d6:64:e6 oraccha@MBA11.local
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        .        |
|       o =       |
|      . S + +    |
|       . . X     |
|        . B E    |
|         B = .   |
|        +=B..    |
+-----------------+
Updated [https://www.googleapis.com/compute/v1/projects/shaped-producer-XXX].
Warning: Permanently added 'XXX.XXX.XXX.XXX' (RSA) to the list of known hosts.
Saving password to keychain failed
Identity added: /Users/oraccha/.ssh/google_compute_engine (/Users/oraccha/.ssh/google_compute_engine)
Warning: Permanently added 'XXX.XXX.XXX.XXX' (RSA) to the list of known hosts.
[oraccha@instance-1 ~]$ uname -a
Linux instance-1 3.10.0-123.6.3.el7.x86_64 #1 SMP Wed Aug 6 21:12:36 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

ちゃんとKVMで動いているようね。

$ systemd-detect-virt 
kvm

ちょっとネットワーク周りを見てみると、eth0にはプライベートアドレスが割り当てられているから、NATでルーティングされているようね。MTUが1460バイト。40バイトはどこへ行ったのでしょう。やはり、GoogleのSDN「Andromeda」がらみかなぁ。あと、デバドラはvirtio_net。

$ ip a show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP qlen 1000
    link/ether 42:01:0a:f0:c0:53 brd ff:ff:ff:ff:ff:ff
    inet 10.240.192.83/32 brd 10.240.192.83 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4001:aff:fef0:c053/64 scope link 
       valid_lft forever preferred_lft forever

$ ip r
default via 10.240.0.1 dev eth0  proto static  metric 1024 
10.240.0.1 dev eth0  proto static  scope link  metric 1 

インスタンスの生成・削除ももちろんgcloudコマンドから操作可能。あと、gcloud config set project XXXを実行しておけば、いちいち--projectオプションを指定する必要はない。

$ gcloud compute instances delete instance-1 --zone asia-east1-c
The following instances will be deleted. Attached disks configured to 
be auto-deleted will be deleted unless they are attached to any other 
instances. Deleting a disk is irreversible and any data on the disk 
will be lost.
 - [instance-1] in [asia-east1-c]

Do you want to continue (Y/n)?  

Deleted [https://www.googleapis.com/compute/v1/projects/shaped-producer-XXX/zones/asia-east1-c/instances/instance-1].

確かにEC2と比較すると、CLI周りはよくできているな。

Google Compute Engine入門

Google Compute Engine入門

*1:https://cloud.google.com/developers/starterpack/からプロモーションコードを入力することで500 USD分無料で使えるようになる。