快速建立私有 GCE Instance 並整合 Cloud NAT
前言
私有 GCE Instance 能夠減少被駭客攻擊的機會, 因為在做網路配置的時候, 不會特別分派外部 IP 給這類型的 VM, 所有存取 VM 的行為都必須走內網, 但也因為沒有對外的 IP, 所以 VM 裡也沒辦法透過網際網路去下載並且使用第三方的套件
解法: 整合 Cloud NAT
透過 Cloud NAT 位址轉譯的功能, 將 VM 的內部 IP 對應到一組共用的對外 IP
使用 Terraform 建立 VPC, Subnet, GCE VM
variable "project" {
default = "test"
}
variable "network_name" {
default = "net-123"
}
variable "subnet_name" {
default = "subnet-123"
}
resource "google_compute_network" "vpc_network" {
project = var.project
name = var.network_name
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "vpc_subnet" {
project = var.project
name = var.subnet_name
ip_cidr_range = "10.51.0.0/23"
region = "us-central1"
network = google_compute_network.vpc_network.id
}
resource "google_service_account" "bastion_host_vm_sc" {
project = var.project
account_id = "bastion-host-vm-sc-01"
display_name = "bastion_host_vm Service Account"
}
resource "google_project_iam_member" "svr_iam_member" {
project = var.project
role = "roles/iam.serviceAccountActor"
member = "serviceAccount:${google_service_account.bastion_host_vm_sc.email}"
}
resource "google_compute_instance" "bastion_host_vm" {
project = var.project
name = "my-vm"
machine_type = "n2-standard-4"
zone = "us-central1-c"
tags = ["bastion"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
scratch_disk {
interface = "SCSI"
}
network_interface {
network = google_compute_network.vpc_network.id
subnetwork = google_compute_subnetwork.vpc_subnet.id
}
service_account {
email = google_service_account.bastion_host_vm_sc.email
scopes = ["cloud-platform"]
}
allow_stopping_for_update = true
metadata_startup_script = <<-EOF
#!/bin/bash
apt-get install google-cloud-sdk-gke-gcloud-auth-plugin
apt-get install kubectl
EOF
}
建立防火牆允許 ssh 連線, 必須允許 icmp 讓 IAP TCP forwarding 可以正常運作
resource "google_compute_firewall" "fw" {
project = var.project
name = "${var.network_name}-fw"
network = google_compute_network.vpc_network.name
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["22"]
}
source_tags = []
target_tags = ["bastion"]
}
測試 VM
以 IAP Tunnel 連看看 VM
$ gcloud beta compute --project test ssh --zone us-central1-c "my-vm" --tunnel-through-iap
andylai@my-vm:~$ ping google.com
PING google.com (209.85.234.138) 56(84) bytes of data.
因為對外網路沒通, 所以下 ping 指令不會有任何結果
建立 Cloud NAT
resource "google_compute_router" "bastion_vm_router" {
name = "bastion-vm-router"
region = google_compute_subnetwork.vpc_subnet.region
network = google_compute_network.vpc_network.id
}
resource "google_compute_router_nat" "bastion_vm_nat" {
name = "bastion-vm-router-nat"
router = google_compute_router.bastion_vm_router.name
region = google_compute_router.bastion_vm_router.region
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
log_config {
enable = true
filter = "ERRORS_ONLY"
}
}
在測試一次
$ gcloud beta compute --project test ssh --zone us-central1-c "my-vm" --tunnel-through-iap
andylai@my-vm:~$ ping google.com
PING google.com (74.125.69.102) 56(84) bytes of data.
64 bytes from iq-in-f102.1e100.net (74.125.69.102): icmp_seq=1 ttl=115 time=0.891 ms
留言
張貼留言