1 簡介 如果要選擇GCP為雲平臺,則經常需要創建GCE(Google Compute Engine),有以下幾種方式: (1) 在瀏覽器創建 (2) 命令 gcloud (3) Terraform 在開始之前,可以查看:《初始化一個GCP項目並用gcloud訪問操作》。 2 GCP Console ...
1 簡介
如果要選擇GCP為雲平臺,則經常需要創建GCE(Google Compute Engine),有以下幾種方式:
(1) 在瀏覽器創建
(2) 命令 gcloud
(3) Terraform
在開始之前,可以查看:《初始化一個GCP項目並用gcloud訪問操作》。
2 GCP Console
登陸操作界面,點擊創建按鈕,然後選擇好參數即可:
會顯示出對應的價格。
3 gcloud命令
在操作界面創建時,可以直接查看對應的gcould命令:
我們直接運行就可以創建了:
$ gcloud compute instances create pkslow-vm \
--project=pkslow \
--zone=us-west1-a \
--machine-type=e2-micro \
--network-interface=network-tier=PREMIUM,subnet=default \
--maintenance-policy=MIGRATE \
[email protected] \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--tags=http-server,https-server \
--create-disk=auto-delete=yes,boot=yes,device-name=instance-1,image=projects/centos-cloud/global/images/centos-8-v20211105,mode=rw,size=20,type=projects/pkslow/zones/us-west1-a/diskTypes/pd-standard \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--reservation-affinity=any
Created [https://www.googleapis.com/compute/v1/projects/pkslow/zones/us-west1-a/instances/pkslow-vm].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
pkslow-vm us-west1-a e2-micro 10.138.0.5 34.145.124.xxx RUNNING
10.138.0.5 34.145.124.xxx RUNNING
檢查是否創建成功:
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
pkslow-vm us-west1-a e2-micro
4 Terraform
當然,最佳實踐是使用Terraform來管理,代碼簡單易懂,具體如下:
provider "google" {
project = "pkslow"
}
resource "google_compute_instance" "test" {
name = "pkslow-test"
machine_type = "e2-micro"
zone = "us-west1-a"
tags = ["http-server", "https-server"]
boot_disk {
initialize_params {
image = "projects/centos-cloud/global/images/centos-8-v20211105"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral public IP
}
}
metadata = {
foo = "bar"
}
metadata_startup_script = "echo hi > /test.txt"
service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = "[email protected]"
scopes = ["cloud-platform"]
}
}
檢查是否創建成功:
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
pkslow-test us-west1-a e2-micro 10.138.0.6 34.83.138.xxx RUNNING
pkslow-vm us-west1-a e2-micro 10.138.0.5 34.145.124.xxx RUNNING
也可以界面上查看:
5 代碼
代碼請查看GitHub: https://github.com/LarryDpk/pkslow-samples