Google Cloud の無料枠 VM インスタンスを Cloud Shell で作ってみた

 


e2-micro VM インスタンスを us-west1 リージョンに 30GB のディスクで作成したいと思います。


優先するリージョンを設定します。

$ gcloud config set compute/region us-west1
Updated property [compute/region].

優先するゾーンを設定します。

$ gcloud config set compute/zone us-west1-a
Updated property [compute/zone].

まずは最小限のパラメーターで VM インスタンスを作成してみます。

$ gcloud compute instances create instance-1 
  --machine-type=e2-micro
Created [https://www.googleapis.com/compute/v1/projects/[project-id]/zones/us-west1-a/instances/instance-1].
NAME: instance-1
ZONE: us-west1-a
MACHINE_TYPE: e2-micro
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.3
EXTERNAL_IP: 35.199.161.177
STATUS: RUNNING

ディスクサイズを確認します。

$ gcloud compute disks list
NAME: instance-1
LOCATION: us-west1-a
LOCATION_SCOPE: zone
SIZE_GB: 10
TYPE: pd-standard
STATUS: READY

ディスクサイズが 10GB だったので一度 VM インスタンスを削除します。

$ gcloud compute instances delete instance-1
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or
the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
 - [instance-1] in [us-west1-a]

Do you want to continue (Y/n)?

Deleted [https://www.googleapis.com/compute/v1/projects/[project-id]/zones/us-west1-a/instances/instance-1].

ディスクサイズ 30GB で VM インスタンスを作成します。

$ gcloud compute instances create instance-1 
  --machine-type=e2-micro 
  --boot-disk-size=30GB
WARNING: You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/compute/docs/disks#performance.
Created [https://www.googleapis.com/compute/v1/projects/mnrst-373922/zones/us-west1-a/instances/instance-1].
WARNING: Some requests generated warnings:
 - Disk size: '30 GB' is larger than image size: '10 GB'. You might need to resize the root repartition manually if the operating system does not support automatic resizing. See https://cloud.google.com/compute/docs/disks/add-persistent-disk#resize_pd for details.

NAME: instance-1
ZONE: us-west1-a
MACHINE_TYPE: e2-micro
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.4
EXTERNAL_IP: 35.199.161.177
STATUS: RUNNING

VM インスタンスに SSH 接続します。

$ gcloud compute ssh instance-1
Warning: Permanently added 'compute.2596553607603557873' (ECDSA) to the list of known hosts.
Linux instance-1 5.10.0-19-cloud-amd64 #1 SMP Debian 5.10.149-2 (2022-10-21) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

VM インスタンス内でルートパーティションのディスクサイズを確認します。

$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  479M     0  479M   0% /dev
tmpfs          tmpfs      98M  368K   98M   1% /run
/dev/sda1      ext4       30G  1.7G   27G   7% /
tmpfs          tmpfs     488M     0  488M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
/dev/sda15     vfat      124M  5.9M  118M   5% /boot/efi
tmpfs          tmpfs      98M     0   98M   0% /run/user/1000

OS がディスクの自動サイズ変更に対応しているようで、自動的に 10GB の OS イメージから 30GB のパーティションにリサイズされていました。


VPC ネットワークのファイアウォールルールを確認します。

$ gcloud compute firewall-rules list
NAME: default-allow-icmp
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: icmp
DENY:
DISABLED: False

NAME: default-allow-internal
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:0-65535,udp:0-65535,icmp
DENY:
DISABLED: False

NAME: default-allow-rdp
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:3389
DENY:
DISABLED: False

NAME: default-allow-ssh
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:22
DENY:
DISABLED: False

To show all fields of the firewall, please show in JSON format: --format=json
To show all fields in table format, please see the examples in --help.

default-allow-ssh のソース IP アドレスを制限します。

$ gcloud compute firewall-rules update default-allow-ssh 
  --source-ranges $(curl -s inet-ip.info)
Updated [https://www.googleapis.com/compute/v1/projects/[project-id]/global/firewalls/default-allow-ssh].

default-allow-ssh の設定を確認します。

$ gcloud compute firewall-rules list 
  --format=json 
  --filter="name=default-allow-ssh"
[
  {
    "allowed": [
      {
        "IPProtocol": "tcp",
        "ports": [
          "22"
        ]
      }
    ],
    "creationTimestamp": "2023-01-28T02:30:16.449-08:00",
    "description": "Allow SSH from anywhere",
    "direction": "INGRESS",
    "disabled": false,
    "id": "8518019168853379031",
    "kind": "compute#firewall",
    "logConfig": {
      "enable": false
    },
    "name": "default-allow-ssh",
    "network": "https://www.googleapis.com/compute/v1/projects/[project-id]/global/networks/default",
    "priority": 65534,
    "selfLink": "https://www.googleapis.com/compute/v1/projects/[project-id]/global/firewalls/default-allow-ssh",
    "sourceRanges": [
      "34.81.171.114"
    ]
  }
]

これでソース IP アドレスを制限した無料枠の VM インスタンスを作る事ができました。


※参考サイト

  • https://cloud.google.com/free/docs/free-cloud-features?hl=ja#free-tier-usage-limits
  • https://cloud.google.com/shell/docs/run-gcloud-commands?hl=ja
  • https://cloud.google.com/sdk/gcloud/reference/compute/instances/create
  • https://cloud.google.com/sdk/gcloud/reference/compute/firewall-rules/update
  • ブログを読んで頂きありがとうございます。誰かの何かの参考になれば幸いです。

    タグ: , ,