

在GCP上创建GCE的三种方式(Console,gcloud,Terraform)
source link: https://www.pkslow.com/archives/create-gcp-gce
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如果要选择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
也可以界面上查看:
代码请查看GitHub: https://github.com/LarryDpk/pkslow-samples
Reference:
Terraform GCP Compute Instance
Code for all: GitHub
欢迎关注微信公众号<南瓜慢说>,将持续为你更新...
Recommendations:
Cloud Native
Terraform
Container: Docker/Kubernetes
Spring Boot / Spring Cloud
Https
如何制定切实可行的计划并好好执行
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK