关于java:在GCP上创建GCE的三种方式ConsolegcloudTerraform

46次阅读

共计 2120 个字符,预计需要花费 6 分钟才能阅读完成。

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 \
--service-account=admin-for-all@pkslow.iam.gserviceaccount.com \
--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  = "admin-for-all@pkslow.iam.gserviceaccount.com"
    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/p…

正文完
 0