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


<!-- wp:paragraph -->
<p>今回はコマンドでVMインスタンスを作成します。</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>gcloud initで認証した際の情報を表示して確認します。</li></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>注：xxxxxxは伏字です。
gcloud config list
&#91;core]
account = XXXXXX
disable_usage_reporting = True

Your active configuration is: &#91;default]</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>上記では、プロジェクトが設定されいないので、コマンドで設定します。</p>
<!-- /wp:paragraph -->

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code># 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]</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

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

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code># 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 &quot;operations/acf.p2-462935646055-b817e645-2652-4681-8c64-79a24369df81&quot; 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</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

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

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code>Enabling service [compute.googleapis.com] on project [992463307655]...
ERROR: (gcloud.compute.images.list) FAILED_PRECONDITION: Billing account for project &#39;992463307655&#39; is not found. Billing must be enabled for activation of service(s) &#39;compute.googleapis.com,compute.googleapis.com,compute.googleapis.com&#39; to proceed.
Help Token: XXXXX
- &#39;@type&#39;: 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
- &#39;@type&#39;: type.googleapis.com/google.rpc.ErrorInfo
  domain: serviceusage.googleapis.com/billing-enabled
  metadata:
    project: &#39;992463307655&#39;
    services: compute.googleapis.com,compute.googleapis.com,compute.googleapis.com
  reason: UREQ_PROJECT_BILLING_NOT_FOUND</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

<!-- wp:paragraph -->
<p>3. 利用するゾーンを設定します。</p>
<!-- /wp:paragraph -->

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code># gcloud config set compute/zone us-east4-a
Updated property [compute/zone].</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

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

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code>#  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
</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

<!-- wp:paragraph -->
<p>5. いよいよＶＭを作成します。（ ５秒ほどで作成されました。 ）</p>
<!-- /wp:paragraph -->

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code># 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</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

<!-- wp:paragraph -->
<p>6. 作成したVMにログインします。</p>
<!-- /wp:paragraph -->

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code> $ gcloud beta compute ssh --zone &quot;us-east4-a&quot; &quot;sdk-test-vm&quot;  --tunnel-through-iap --project &quot;sdk-test-2021-12-29&quot;
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 &#39;/home/kazuo/.ssh/google_compute_engine&#39;:
Enter passphrase for key &#39;/home/kazuo/.ssh/google_compute_engine&#39;:
[xxxxx@sdk-test-vm ~]$</code></pre></div>
<!-- /wp:loos-hcb/code-block -->

<!-- wp:paragraph -->
<p>rootユーザーで接続はできません。下記のエラーになります。</p>
<!-- /wp:paragraph -->

<!-- wp:loos-hcb/code-block {"langType":"bash","langName":"Bash"} -->
<div class="hcb_wrap"><pre class="prism undefined-numbers lang-bash" data-lang="Bash"><code>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].</code></pre></div>
<!-- /wp:loos-hcb/code-block -->