如何跨專案存取私有 GKE 叢集
在 GKE 的 Control Plane 沒有提供外部 IP 的情況下, 維運人員若想要操作 GKE 則必需走 VPN 的方式才能訪問到 Control Plane 的 API Server
以下示範, 如何在不同的專案底下建立跳板機跟 GKE 叢集, 並透過 VPN 的方式在跳板機上去操作另一個專案下的 GKE 叢集
在 Project A 建立 VPC #1
variable "gke_project" {
default = "prj-1"
}
variable "gke_network_name" {
default = "net-1"
}
variable "gke_subnet_name" {
default = "subnet-1"
}
resource "google_compute_network" "gke_vpc_network" {
project = var.gke_project
name = var.gke_network_name
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "gke_vpc_subnet" {
project = var.gke_project
name = var.gke_subnet_name
ip_cidr_range = "10.51.0.0/23"
region = "us-central1"
network = google_compute_network.gke_vpc_network.id
}
在 Project B 建立 VPC #2
vvariable "proxy_project" {
default = "prj-2"
}
variable "proxy_network_name" {
default = "net-2"
}
variable "proxy_subnet_name" {
default = "subnet-2"
}
resource "google_compute_network" "proxy_vpc_network" {
project = var.proxy_project
name = var.proxy_network_name
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "proxy_vpc_subnet" {
project = var.proxy_project
name = var.proxy_subnet_name
ip_cidr_range = "10.49.0.0/28"
region = "us-central1"
network = google_compute_network.proxy_vpc_network.id
private_ip_google_access = true
}
建立 GKE 叢集
variable "gke_cluster_name" {
default = "gke"
}
variable "cluster_secondary_range_name" {
default = "10.104.0.0/14"
}
variable "services_secondary_range_name" {
default = "10.108.0.0/20"
}
resource "google_container_cluster" "gke_cluster" {
project = var.gke_project
name = var.gke_cluster_name
location = "us-central1"
initial_node_count = 3
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform"
]
labels = {
env = "beta"
}
tags = ["gke-playsee-node"]
}
timeouts {
create = "30m"
update = "40m"
}
network = var.gke_network_name
subnetwork = var.gke_subnet_name
master_authorized_networks_config {
cidr_blocks {
cidr_block = "10.49.0.0/28"
display_name = "Bastion Host access"
}
}
private_cluster_config {
enable_private_endpoint = true
enable_private_nodes = true
master_ipv4_cidr_block = "172.16.0.64/28"
}
ip_allocation_policy {
cluster_ipv4_cidr_block = var.cluster_secondary_range_name
services_ipv4_cidr_block = var.services_secondary_range_name
}
addons_config {
horizontal_pod_autoscaling {
disabled = false
}
}
lifecycle {
# prevent_destroy = true
}
}
建立跳板機
resource "google_service_account" "bastion_host_vm_sc" {
project = var.proxy_project
account_id = "bastion-host-vm-sc"
display_name = "bastion_host_vm Service Account"
}
resource "google_project_iam_member" "svr_iam_member_owner_role" {
project = var.proxy_project
role = "roles/owner"
member = "serviceAccount:${google_service_account.bastion_host_vm_sc.email}"
}
resource "google_compute_instance" "bastion_host_vm" {
project = var.proxy_project
name = "bastion-host"
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.proxy_vpc_network.id
subnetwork = google_compute_subnetwork.proxy_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
}
建立 VPN
需要在兩個網路上各自建立 VPN Gateway 作為各自網路能夠互相連接的入口
resource "google_compute_ha_vpn_gateway" "gke_ha_vpn_gateway" {
project = var.gke_project
region = "us-central1"
name = "gke-ha-vpn-gw"
network = google_compute_network.gke_vpc_network.id
}
resource "google_compute_ha_vpn_gateway" "repo_ha_vpn_gateway" {
project = "repo-backend"
region = "us-central1"
name = "proxy-ha-vpn-gw"
network = google_compute_network.proxy_vpc_network.id
}
除此之外還需建立 Router, VPN Tunnel, Router Interface, Router Peer, 並在 Router Peer 中設定 advertised_ip_ranges 為 Control Plane 的內網 IP, 這樣的話跳板機才可透過 VPN 在網路中使用這個 IP 去訪問 GKE
resource "google_compute_ha_vpn_gateway" "gke_ha_vpn_gateway" {
project = var.gke_project
region = "us-central1"
name = "gke-ha-vpn-gw"
network = google_compute_network.gke_vpc_network.id
}
resource "google_compute_ha_vpn_gateway" "repo_ha_vpn_gateway" {
project = "repo-backend"
region = "us-central1"
name = "proxy-ha-vpn-gw"
network = google_compute_network.proxy_vpc_network.id
}
module "gke_ha_vpn" {
source = "./terraform/_modules/ha-vpn-tunnel"
project_id = var.gke_project
region = "us-central1"
ha_vpn_gateway_name = google_compute_ha_vpn_gateway.gke_ha_vpn_gateway.name
ha_vpn_gateway_router_bpg = 64514
vpc_network = google_compute_network.gke_vpc_network
gateway_router_inf_ip_range_ha01 = "169.254.0.1/30"
gateway_router_inf_ip_range_ha02 = "169.254.1.2/30"
router_peer_advertised_ip_ranges = "172.16.0.66"
peer_gcp_gateway_id = google_compute_ha_vpn_gateway.repo_ha_vpn_gateway.id
peer_ip_address_ha01 = "169.254.0.2"
peer_ip_address_ha02 = "169.254.1.1"
peer_asn_ha01 = "64515"
peer_asn_ha02 = "64515"
}
module "repo_ha_vpn" {
source = "./terraform/_modules/ha-vpn-tunnel"
project_id = var.proxy_project
region = "us-central1"
ha_vpn_gateway_name = google_compute_ha_vpn_gateway.repo_ha_vpn_gateway.name
ha_vpn_gateway_router_bpg = 64515
vpc_network = google_compute_network.proxy_vpc_network
gateway_router_inf_ip_range_ha01 = "169.254.0.2/30"
gateway_router_inf_ip_range_ha02 = "169.254.1.1/30"
router_peer_advertised_ip_ranges = "10.150.0.0/20"
peer_gcp_gateway_id = google_compute_ha_vpn_gateway.gke_ha_vpn_gateway.id
peer_ip_address_ha01 = "169.254.0.1"
peer_ip_address_ha02 = "169.254.1.2"
peer_asn_ha01 = "64514"
peer_asn_ha02 = "64514"
}
ref:git@gitlab.com:sysinfra/terraform-private-gke-and-vpn-tunnel.git
留言
張貼留言