【问题标题】:Create private network with Terraform with starting script - Google Cloud Platform使用 Terraform 和启动脚本创建专用网络 - Google Cloud Platform
【发布时间】:2019-11-09 02:25:37
【问题描述】:

最近使用 GCP 开始使用 Terraform,我想完成一个练习:

  • 使用单个子网创建新的 VPC 网络。
  • 创建允许外部 RDP 流量到堡垒主机系统的防火墙规则。
  • 部署两个连接到 VPC 网络和默认网络的 Windows 服务器。
  • 创建一个指向启动脚本的虚拟机。
  • 配置防火墙规则以允许 HTTP 访问虚拟机。

这是我的解决方案:

  1. 创建一个名为securenetwork 的新VPC 网络,然后在securenetwork 中创建一个新的VPC 子网。配置网络和子网后,配置防火墙规则,允许从 Internet 到堡垒主机的入站 RDP 流量(TCP 端口 3389)。
# Create the securenetwork network
resource "google_compute_network" "securenetwork" {
  name                    = "securenetwork"
  auto_create_subnetworks = false
}

# Create securesubnet-us subnetwork
resource "google_compute_subnetwork" "securesubnet-eu" {
  name          = "securesubnet-eu"
  region        = "europe-west1"
  network       = "${google_compute_network.securenetwork.self_link}"
  ip_cidr_range = "10.130.0.0/20"
}

# Create a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on securenetwork
resource "google_compute_firewall" "securenetwork-allow-http-ssh-rdp-icmp" {
  name    = "securenetwork-allow-http-ssh-rdp-icmp"
  network = "${google_compute_network.securenetwork.self_link}"

  allow {
    protocol = "tcp"
    ports    = ["3389"]
  }

  allow {
    protocol = "icmp"
  }
}

# Create the vm-securehost instance
module "vm-securehost" {
  source              = "./instance/securehost"
  instance_name       = "vm-securehost"
  instance_zone       = "europe-west1-d"
  instance_subnetwork = "${google_compute_subnetwork.securesubnet-eu.self_link}"
  instance_network = "${google_compute_network.securenetwork.self_link}"
}

# Create the vm-bastionhost instance
module "vm-bastionhost" {
  source              = "./instance/bastionhost"
  instance_name       = "vm-bastionhost"
  instance_zone       = "europe-west1-d"
  instance_subnetwork = "${google_compute_subnetwork.securesubnet-eu.self_link}"
  instance_network = "${google_compute_network.securenetwork.self_link}"
}
  1. 部署 Windows 实例

    • 一个名为 vm-securehost 的 Windows 2016 服务器实例,具有两个网络接口。将第一个网络接口配置为与新 VPC 子网的仅内部连接,并将第二个网络接口配置为与默认 VPC 网络的仅内部连接。这是安全服务器。
variable "instance_name" {}
variable "instance_zone" {}

variable "instance_type" {
  default = "n1-standard-1"
}

variable "instance_subnetwork" {}
variable "instance_network" {}

resource "google_compute_instance" "vm_instance" {
  name         = "${var.instance_name}"
  zone         = "${var.instance_zone}"
  machine_type = "${var.instance_type}"

  boot_disk {
    initialize_params {
      image = "windows-cloud/windows-2016"
    }
  }

  network_interface {
    subnetwork = "${var.instance_subnetwork}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
    }
  }
}
  • 另一个名为 vm-bastionhost 的 Windows 2016 服务器实例具有两个网络接口。将第一个网络接口配置为使用临时公共(外部 NAT)地址连接到新的 VPC 子网,并将第二个网络接口配置为与默认 VPC 网络的仅内部连接。这是跳转框或堡垒主机。
variable "instance_name" {}
variable "instance_zone" {}

variable "instance_type" {
  default = "n1-standard-1"
}

variable "instance_subnetwork" {}
variable "instance_network" {}

resource "google_compute_address" "default" {
  name = "default"
  region = "europe-west1"
}

resource "google_compute_instance" "vm_instance" {
  name         = "${var.instance_name}"
  zone         = "${var.instance_zone}"
  machine_type = "${var.instance_type}"

  boot_disk {
    initialize_params {
      image = "windows-cloud/windows-2016"
    }
  }

  network_interface {
    subnetwork = "${var.instance_subnetwork}"
    network = "${var.instance_network}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
      nat_ip = "${google_compute_address.default.address}"
    }
  }
}

我的问题:

  • 如何配置没有公共 IP 地址的名为 vm-securehost 的 Windows 计算实例?
  • 如何配置名为 vm-securehost 的 Windows 计算实例在启动时运行 Microsoft IIS Web 服务器软件?
  • 感谢您对解决方案的任何评论

【问题讨论】:

    标签: windows google-cloud-platform terraform vpc startupscript


    【解决方案1】:

    要创建一个没有任何外部 IP 地址的虚拟机,请省略 terraform 脚本中的“访问配置”参数,因为它负责创建外部 IP 地址。

    要在启动时在您的 vm 上运行 Microsoft IIS Web 服务器软件,请在您的 vm 创建块中添加以下参数(不包括引号) - 'metadata_startup_script = import-module servermanager && add-windowsfeature web-server -includeallsubfeature'

    有关该问题的详细信息,请参阅以下链接 -

    https://cloud.google.com/compute/docs/tutorials/basic-webserver-iis

    https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#metadata_startup_script

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-07
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多