0
0

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 3 years have passed since last update.

Google Platform SDKでVMインスタンスを構築(コマンドによるサーバー構築)

Posted at

今回はコマンドでVMインスタンスを作成します。

  1. gcloud initで認証した際の情報を表示して確認します。
注:xxxxxxは伏字です。
gcloud config list
[core]
account = XXXXXX
disable_usage_reporting = True

Your active configuration is: [default]

上記では、プロジェクトが設定されいないので、コマンドで設定します。

# gcloud config set project sdk-test-2021-12-28
Updated property [core/project].
#
# gcloud config list
[core]
account = xxxxx@gmail.com
disable_usage_reporting = True
project = sdk-test-2021-12-28
Your active configuration is: [default]

2. 利用するゾーン(地域)の設定をしましょう。 日本は若干高めのレートに設定されていますので、アメリカがが良いと思います。まずは、ゾーンの一覧を表示しましょう。コマンド実行時にGoogle Cloud APIが有効になっていないと 有効にするか確認されます。 

# gcloud compute zones list
API [compute.googleapis.com] not enabled on project [462935646055]. Would you like to enable and retry (this will take a few minutes)? (y/N)?  y
Enabling service [compute.googleapis.com] on project [462935646055]...
Operation "operations/acf.p2-462935646055-b817e645-2652-4681-8c64-79a24369df81" finished successfully.
NAME                       REGION                   STATUS  NEXT_MAINTENANCE  TURNDOWN_DATE
us-east1-b                 us-east1                 UP
us-east1-c                 us-east1                 UP
us-east1-d                 us-east1                 UP
us-east4-c                 us-east4                 UP
us-east4-b                 us-east4                 UP
us-east4-a                 us-east4                 UP
us-central1-c              us-central1              UP
us-central1-a              us-central1              UP

アカウントとプロジェクトに請求方法が設定されてないと下記のようなエラーになります。コンソールで請求先を設定しましょう。

Enabling service [compute.googleapis.com] on project [992463307655]...
ERROR: (gcloud.compute.images.list) FAILED_PRECONDITION: Billing account for project '992463307655' is not found. Billing must be enabled for activation of service(s) 'compute.googleapis.com,compute.googleapis.com,compute.googleapis.com' to proceed.
Help Token: XXXXX
- '@type': type.googleapis.com/google.rpc.PreconditionFailure
  violations:
  - subject: ?error_code=390001&project=992463307655&services=compute.googleapis.com&services=compute.googleapis.com&services=compute.googleapis.com
    type: googleapis.com/billing-enabled
- '@type': type.googleapis.com/google.rpc.ErrorInfo
  domain: serviceusage.googleapis.com/billing-enabled
  metadata:
    project: '992463307655'
    services: compute.googleapis.com,compute.googleapis.com,compute.googleapis.com
  reason: UREQ_PROJECT_BILLING_NOT_FOUND

3. 利用するゾーンを設定します。

# gcloud config set compute/zone us-east4-a
Updated property [compute/zone].

4. VMで使う イメージ(OS)とマシンタイプ(CPU数、メモリなど)を表示します。今回はCentOS7で最小のインスタンス f1-micro を使います。

#  gcloud compute images list | grep centos-7
centos-7-v20211214                                    centos-cloud         centos-7                                      READY
gcloud compute machine-types list | grep us-east4-a  | grep micro
e2-micro          us-east4-a                 2     1.00
f1-micro          us-east4-a                 1     0.60

5. いよいよVMを作成します。( 5秒ほどで作成されました。 )

# gcloud compute instances create sdk-test-vm \
   --image-family centos-7 \
   --image-project centos-cloud \
   --machine-type f1-micro
Created [https://www.googleapis.com/compute/v1/projects/sdk-test-2021-12-28/zones/us-east4-a/instances/sdk-test-vm].
NAME         ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
sdk-test-vm  us-east4-a  f1-micro                   xx.150.0.2   xx.194.90.134  RUNNING

6. 作成したVMにログインします。

 $ gcloud beta compute ssh --zone "us-east4-a" "sdk-test-vm"  --tunnel-through-iap --project "sdk-test-2021-12-29"
Updating project ssh metadata...?Updated [https://www.googleapis.com/compute/beta/projects/sdk-test-2021-12-29].
Updating project ssh metadata...done.
Waiting for SSH key to propagate.
Enter passphrase for key '/home/kazuo/.ssh/google_compute_engine':
Enter passphrase for key '/home/kazuo/.ssh/google_compute_engine':
[xxxxx@sdk-test-vm ~]$

rootユーザーで接続はできません。下記のエラーになります。

root@compute.6939899849049777957: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
Recommendation: To check for possible causes of SSH connectivity issues and get
recommendations, rerun the ssh command with the --troubleshoot option. Example:
gcloud alpha compute ssh example-instance --zone=us-central1-a --troubleshoot
ERROR: (gcloud.beta.compute.ssh) [/usr/bin/ssh] exited with return code [255].
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?