gcloudコマンドでVMインスタンス作成

Contents


はじめに

gcloudコマンドでVMインスタンス作成のメモです。

環境

Macbook ProにGoogle Cloud SDKを入れてコマンド実行します。
項目バージョン
macOS11.1(Big Sur)
Python2.7.16
Python33.9.0
Google Cloud SDK319.0.0

VMインスタンスのスペック

無料枠で使えるスペックで作ってみる。 GCPの無料枠
項目
インスタンスf1-micro
リージョンus-west1(オレゴン)
ゾーンus-west1-a
ホスト名vm-test01
OSCentOS7
標準永続ディスク30GB
内部IPdefault(自動)
外部IP静的IP(ホスト名と同じ名前で付与)
Shielded VM有効

gcloudコマンド操作

Compute Engineで利用可能な公開イメージを確認する。
$ gcloud compute images list | awk 'NR==1 || /centos-7/'
 NAME                                                  PROJECT              FAMILY                            DEPRECATED  STATUS
 centos-7-v20201216                                    centos-cloud         centos-7                                      READY
イメージがShielded VM機能をサポートしているか確認する。
$ gcloud compute images describe centos-7-v20201112 --project centos-cloud | grep UEFI_COMPATIBLE
- type: UEFI_COMPATIBLE
外部IPアドレスを予約する。
$ gcloud compute addresses create vm-test01 --region us-west1
予約した外部IPアドレスを確認する。
$ gcloud compute addresses describe vm-test01 --region us-west1
 address: 35.199.xxx.xxx
 addressType: EXTERNAL
 creationTimestamp: '2020-12-23T10:47:44.643-08:00'
 description: ''
 id: 'xxxxxxxxxxxxxxxxxx'
 kind: compute#address
 name: vm-test01
 networkTier: PREMIUM
 region: https://www.googleapis.com/compute/v1/projects/xxxxxxx-xxxxx-xxxxxx/regions/us-west1
 selfLink: https://www.googleapis.com/compute/v1/projects/xxxxxxx-xxxxx-xxxxxx/regions/us-west1/addresses/vm-test01
 status: RESERVED
リージョンと対応するゾーンを確認する。
$ gcloud compute zones list | awk 'NR==1 || /us-west1/'
NAME                       REGION                   STATUS  NEXT_MAINTENANCE  TURNDOWN_DATE
us-west1-b                 us-west1                 UP
us-west1-c                 us-west1                 UP
us-west1-a                 us-west1                 UP
準備が整ったのでVMインスタンス作成を実行してみる。
$ gcloud compute instances create vm-test01 \
   --image=centos-7-v20201216 --boot-disk-size=30GB --image-project=centos-cloud \
   --machine-type=f1-micro \
   --shielded-secure-boot \
   --address="35.199.xxx.xxx" \
   --zone=us-west1-a 
リストで作成したVMインスタンスを確認
$ gcloud compute instances list
 NAME       ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP      EXTERNAL_IP     STATUS
 vm-test01  us-west1-a  f1-micro                   10.138.xxx.xxx   35.199.xxx.xxx  RUNNING
gcloudコマンドでSSHログインしてみる。(初回はSSH鍵の作成が要求される)
$ gcloud compute ssh --zone "us-west1-a" "vm-test01" --project "xxxxxxx-xxxxx-xxxxxx"
 Updating project ssh metadata…⠹Updated [https://www.googleapis.com/compute/v1/projects/xxxxxxx-xxxxx-xxxxxx].
 Updating project ssh metadata…done.
 Waiting for SSH key to propagate.
 Warning: Permanently added 'compute.xxxxxxxxxxxxxxxxxxx' (ECDSA) to the list of known hosts.
 Enter passphrase for key '/Users/*****/.ssh/google_compute_engine':
 Enter passphrase for key '/Users/*****/.ssh/google_compute_engine':
 [*****@vm-test01 ~]$
以上