Hello, GCP

最終更新日
公開日

Google Cloud に入門する

目次

本エントリでは、Google Cloud Platform (GCP) に入門する。 gcloud CLIを使って、Google Compute Engine (GCE) でVMを立て、nginxをinstallして、外部から疎通確認するところまでをやる。

GCPを操作する方法

などがある。 今回は、gcloud CLIを使ってみる。

インストール: https://cloud.google.com/sdk/docs/install

やる

$ gcloud info

$ gcloud components update
# 適当にYesしないように注意
# システムのpythonを勝手にいじられそうになった

$ gcloud init
# (以前実行済みなのを忘れていて、) 二回実行してしまってもok

$ gcloud projects list
$ gcloud projects create hello-gcp-nginx
$ gcloud config set project hello-gcp-nginx

$ gcloud config get compute/region
$ gcloud config set compute/region asia-northeast1
# tokyo region: asia-northeast1
# osaka region: asia-northeast2
$ gcloud config get compute/zone
$ gcloud config set compute/zone asia-northeast1-a
# tokyo zone: a,b,c
# 何が違うのか分からん

# 参考:
$ gcloud config unset compute/region,zone

# 確認
$ gcloud config list
$ gcloud projects list
$ gcloud projects describe <PROJECT_ID>
$ gcloud auth list

$ gcloud projects add-iam-policy-binding hello-gcp-nginx \
--member='user:hoge@example.com' \
--role='roles/compute.instanceAdmin.v1'

$ gcloud compute images list
$ gcloud compute instances list

# VM の作成 (GCE instance)
# e2-micro: 2vCPU, 1GB mem  (最小構成?)
# disk: debianとかだと10GBがmin. requirement
$ gcloud compute instances create server-vm-inst \
--image-project=debian-cloud \
--image-family=debian-13 \
--machine-type=e2-micro \
--boot-disk-size=10GB \
--zone=asia-northeast1-a \
--tags=server-vm-t

$ gcloud compute instances list
NAME            ZONE               MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
server-vm-inst  asia-northeast1-a  e2-micro                   xx.xxx.x.x   xx.xxx.xxx.xxx  RUNNING
# お〜〜〜〜感動

# 次に接続してみる
$ gcloud compute ssh server-vm-inst
# 最初はSSH鍵の初期化が走る

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
NAME="Debian GNU/Linux"
VERSION_ID="13"
VERSION="13 (trixie)"
VERSION_CODENAME=trixie
DEBIAN_VERSION_FULL=13.1
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
# いいね〜〜〜

# 次にnginxをinstallする
$ sudo apt update && sudo apt install nginx -y

# html: /var/www/html/index.nginx-debian.html
# conf: /etc/nginx/nginx.conf

# このままだと疎通できないので、firewallを設定する

# 手元マシンのglobal ip (v4)を確認
$ curl ifconfig.io -4
# xxx.xxx.xxx.xxx が得られる
# xxx.xxx.xxx.xxx/32 (netmaskが32bitなので、つまりこのipだけを指定)

# dir: INGRESS (InternetからGCPへ) / EGRESS (GCPからInternetへ)
# priority: 0-65535 (default: 1000) (数値が小さいほど優先度高)
# network: default (VPC network)
# action: ALLOW / DENY
$ gcloud compute firewall-rules create allow-http80-fw \
--direction=INGRESS \
--priority=1000 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=xxx.xx.xxx.xxx/32 \
--target-tags=server-vm-t

$ gcloud compute firewall-rules list
NAME                    NETWORK  DIRECTION  PRIORITY  ALLOW                         DENY  DISABLED
allow-http80-fw         default  INGRESS    1000      tcp:80                              False
default-allow-icmp      default  INGRESS    65534     icmp                                False
default-allow-internal  default  INGRESS    65534     tcp:0-65535,udp:0-65535,icmp        False
default-allow-rdp       default  INGRESS    65534     tcp:3389                            False
default-allow-ssh       default  INGRESS    65534     tcp:22                              False

# いよいよ疎通確認
# instanceのEXTERNAL_IPにアクセスする
# access to http://xx.xxx.xxx.xxx
# 良さそう。 楽しいね!!!
# ためしにスマホからアクセスすると、しっかり弾かれてる。firewallも正しそう

# ついでに、preemptible VM (spot instance)も立ててみる
# e2-microはそもそも無料?なので変わらん気もするけど、強いVMをテスト用に安く立てる時に使える
$ gcloud compute instances create server-vm-spot-inst \
--image-project=debian-cloud \
--image-family=debian-13 \
--machine-type=e2-micro \
--boot-disk-size=10GB \
--zone=asia-northeast1-a \
--tags=server-vm-t \
# --preemptible optionは deprecatedっぽい。代わりに SPOT optionを指定する
--provisioning-model=SPOT \
--instance-termination-action=DELETE

$ gcloud compute instances list
NAME                 ZONE               MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
server-vm-inst       asia-northeast1-a  e2-micro                   xx.xxx.x.x   xx.xxx.xxx.xxx  RUNNING
server-vm-spot-inst  asia-northeast1-a  e2-micro      true         xx.xxx.x.x   xx.xxx.xxx.xxx  RUNNING

$ gcloud compute instances describe server-vm-spot-inst
...
scheduling:
  automaticRestart: false
  instanceTerminationAction: DELETE
  onHostMaintenance: TERMINATE
  preemptible: true
  provisioningModel: SPOT
...

# 良さそう
# やっぱり、手を動かすのが大切だね。

参考