如何使用 Cloud VPN 跨專案存取私有 GKE 叢集 - 使用 Classic VPN
Classic VPN 使用 Route Based 的方式, 跟據 Routing Table 裡設定的路由規則, 將封包轉送到指定的目的地
以下示範在 project 01, project 02 各自建立 GKE, VM, 接著透過 Classic VPN 讓 us-east4 的 VM 可以 access project 01 下 us-central1 的 private GKE 叢集
Step 1. 建立 VPN Gateway
需要建立一個 VPN Gateway 作為 VPC 網路對外溝通的橋樑
variable "p1_project_id" {
default = "project-01"
}
variable "p1_region" {
default = "us-central1"
}
resource "google_compute_vpn_gateway" "p1_vpn_gw" {
project = var.p1_project_id
region = var.p1_region
name = "vpn-gw"
network = "projects/${var.p1_project_id}/global/networks/default"
}
Step 2. 建立 Routing 規則
需要一個對外 IP, 讓外部流量可以流進 VPN Gateway
resource "google_compute_address" "p1_vpn_ip" {
project = var.p1_project_id
region = var.p1_region
name = "p1-vpn-ip"
address_type = "EXTERNAL"
}
建立 Routing Rule, 將 Gateway, External IP 關聯在一起
resource "google_compute_forwarding_rule" "p1_vpn_gw_fr_esp" {
project = var.p1_project_id
region = var.p1_region
name = "p1-vpn-gw-fr-esp"
ip_protocol = "ESP"
ip_address = google_compute_address.p1_vpn_ip.address
target = google_compute_vpn_gateway.p1_vpn_gw.id
}
resource "google_compute_forwarding_rule" "p1_vpn_gw_fr_udp500" {
project = var.p1_project_id
region = var.p1_region
name = "p1-vpn-gw-fr-udp500"
ip_protocol = "UDP"
port_range = "500"
ip_address = google_compute_address.p1_vpn_ip.address
target = google_compute_vpn_gateway.p1_vpn_gw.id
}
resource "google_compute_forwarding_rule" "p1_vpn_gw_fr_udp4500" {
project = var.p1_project_id
region = var.p1_region
name = "p1-vpn-gw-fr-udp4500"
ip_protocol = "UDP"
port_range = "4500"
ip_address = google_compute_address.p1_vpn_ip.address
target = google_compute_vpn_gateway.p1_vpn_gw.id
}
Step 3. 建立 VPN Tunnel
variable "p1_vpn_peer_ip" {
default = "YOUR_VPN_IP_ON_PROJECT_02" #YOUR_PEER_IP_TO_PROJECT_02
}
resource "google_compute_vpn_tunnel" "p1_vpn_gw_tunnel" {
project = var.p1_project_id
region = var.p1_region
name = "p1-vpn-gw-tunnel"
peer_ip = var.p1_vpn_peer_ip
shared_secret = "this is secrete"
target_vpn_gateway = google_compute_vpn_gateway.p1_vpn_gw.id
local_traffic_selector = ["0.0.0.0/0"]
remote_traffic_selector = ["0.0.0.0/0"]
depends_on = [
google_compute_forwarding_rule.p1_vpn_gw_fr_esp,
google_compute_forwarding_rule.p1_vpn_gw_fr_udp500,
google_compute_forwarding_rule.p1_vpn_gw_fr_udp4500,
]
}
Step 4. 將以上 Routing Rule 更新到 GCP 上的 Routing Table 裡
定義封包可以被路由到的目的端 IP 範圍,
以下的例子, 也就是説當 VPN 通道建立之後, 封包可以被送往另一個網路下的
10.150.0.0/20 網段
variable "dest_range" {
default = "10.150.0.0/20"
}
resource "google_compute_route" "p1_vpn_gw_tunnel_route" {
project = var.p1_project_id
name = "p1-vpn-gw-tunnel-route"
network = "default"
dest_range = "10.150.0.0/20"
priority = 1000
next_hop_vpn_tunnel = google_compute_vpn_tunnel.p1_vpn_gw_tunnel.id
}
Step 5. 在 Project 2 下建立 VPN Tunnel
重複以上步驟 1~4 在 projetc 2 下建立 VPN Tunnel 相關設定
variable "p2_project_id" {
default = "project-02"
}
variable "p2_region" {
default = "us-east4"
}
resource "google_compute_vpn_gateway" "p2_vpn_gw" {
project = var.p2_project_id
region = var.p2_region
name = "p2_vpn-gw"
network = "projects/${var.p2_project_id}/global/networks/default"
}
resource "google_compute_address" "p2_vpn_ip" {
project = var.p2_project_id
region = var.p2_region
name = "p2-vpn-ip"
address_type = "EXTERNAL"
}
variable "p2_vpn_peer_ip" {
default = "YOUR_VPN_IP_ON_PROJECT_01"
}
resource "google_compute_forwarding_rule" "p2_vpn_gw_fr_esp" {
project = var.p2_project_id
region = var.p2_region
name = "p2-vpn-gw-fr-esp"
ip_protocol = "ESP"
ip_address = google_compute_address.p2_vpn_ip.address
target = google_compute_vpn_gateway.p2_vpn_gw.id
}
resource "google_compute_forwarding_rule" "p2_vpn_gw_fr_udp500" {
project = var.p2_project_id
region = var.p2_region
name = "p2-vpn-gw-fr-udp500"
ip_protocol = "UDP"
port_range = "500"
ip_address = google_compute_address.p2_vpn_ip.address
target = google_compute_vpn_gateway.p2_vpn_gw.id
}
resource "google_compute_forwarding_rule" "p2-vpn_gw_fr_udp4500" {
project = var.p2_project_id
region = var.p2_region
name = "p2-vpn-gw-fr-udp4500"
ip_protocol = "UDP"
port_range = "4500"
ip_address =google_compute_address.p2_vpn_ip.address
target = google_compute_vpn_gateway.p2_vpn_gw.id
}
resource "google_compute_vpn_tunnel" "p2_vpn_gw_tunnel" {
project = var.p2_project_id
region = var.p2_region
name = "p2-vpn-gw-tunnel"
peer_ip = var.p2_vpn_peer_ip
shared_secret = "this is secrete"
target_vpn_gateway = google_compute_vpn_gateway.p2_vpn_gw.id
local_traffic_selector = ["0.0.0.0/0"]
remote_traffic_selector = ["0.0.0.0/0"]
depends_on = [
google_compute_forwarding_rule.p2_vpn_gw_fr_esp,
google_compute_forwarding_rule.p2_vpn_gw_fr_udp500,
google_compute_forwarding_rule.p2_vpn_gw_fr_udp4500,
]
}
resource "google_compute_route" "p2_vpn_gw_tunnel_route" {
project = var.p2_project_id
name = "p2-vpn-gw-tunnel-route"
network = "default"
dest_range = "172.16.0.66/32"
priority = 1000
next_hop_vpn_tunnel = google_compute_vpn_tunnel.p2_vpn_gw_tunnel.id
}
留言
張貼留言