Mengonfigurasi Private Service Connect untuk instance tujuan

Ringkasan

Halaman ini menjelaskan cara menyiapkan Database Migration Service untuk memigrasikan Oracle ke workload Cloud SQL untuk PostgreSQL menggunakan alamat IP pribadi dari instance Cloud SQL tujuan. Koneksi pribadi memungkinkan Database Migration Service mengakses layanan tanpa melalui internet atau menggunakan alamat IP eksternal.

Database Migration Service menggunakan Private Service Connect untuk terhubung ke instance Cloud SQL Anda menggunakan alamat IP pribadi. Dengan Private Service Connect, Anda dapat mengekspos database tujuan ke koneksi aman yang masuk, dan mengontrol siapa yang dapat mengakses database.

Private Service Connect untuk instance Cloud SQL yang mengaktifkan PSC

Jika membuat instance tujuan Cloud SQL dengan Private Service Connect yang diaktifkan, Anda dapat menggunakan konektivitas IP Pribadi tanpa penyiapan jaringan tambahan. Anda hanya perlu memastikan hal berikut saat membuat profil koneksi database tujuan:

  • Gunakan nama DNS instance Cloud SQL, bukan nama host tujuan.

  • Khusus untuk akses terprogram melalui Google Cloud CLI atau API: Tentukan lampiran layanan tujuan Anda secara eksplisit dalam permintaan (tindakan ini dilakukan secara otomatis oleh Database Migration Service saat Anda membuat profil koneksi di konsol Google Cloud ).

Private Service Connect untuk instance Cloud SQL yang tidak mengaktifkan PSC

Untuk mengekspos layanan menggunakan Private Service Connect untuk instance Cloud SQL non-PSC, buat lampiran layanan di project Anda. Anda dapat menggunakan contoh skrip gcloud dan Terraform di bagian Membuat penyiapan produsen Private Service Connect untuk membuat resource yang diperlukan.

Diagram berikut menunjukkan berbagai resource komputasi dan jaringan yang digunakan Database Migration Service untuk menyiapkan konektivitas pribadi ke instance Cloud SQL Anda.

Penyiapan Private Service Connect

Pekerja Database Migration Service adalah unit pemrosesan internal yang bertanggung jawab atas migrasi Database Migration Service Anda. Fungsi ini menggunakan aturan penerusan Private Service Connect keluar untuk mengirim traffic dari pekerja ke jaringan VPC tempat virtual machine (VM) bastion berada.

Resource utama yang dibuat dengan bantuan skrip adalah:

  • Lampiran layanan: Resource yang mengekspos layanan dengan meneruskan traffic ke layanan targetnya. Dalam diagram, layanan target adalah aturan penerusan masuk.
  • Aturan penerusan masuk: Penerusan traffic yang merutekan traffic masuk dari lampiran layanan ke target khusus (misalnya, VM atau grup instance). Dalam diagram, aturan meneruskan traffic ke VM bastion.
  • VM bastion: VM dengan dua pengontrol antarmuka jaringan (NIC), salah satunya dilampirkan ke jaringan Private Service Connect khusus, dan yang lainnya ke jaringan tempat instance Cloud SQL di-peering. Bastion menjalankan server SOCKS Dante, yang digunakan untuk meneruskan koneksi dengan aman. Contoh skrip membuat jaringan dan subnet khusus di jaringan Private Service Connect untuk Anda.

Project Google Cloud yang Anda gunakan untuk memublikasikan layanan menggunakan lampiran layanan adalah produsen layanan. Konsumen layanan adalah Database Migration Service.

Untuk mengetahui informasi selengkapnya tentang Private Service Connect, penyiapan, dan arsitekturnya, lihat dokumentasi Private Service Connect.

Membuat penyiapan produsen Private Service Connect

Anda dapat menggunakan skrip berikut untuk membuat penyiapan Private Service Connect dan terhubung ke instance Cloud SQL tujuan. Jika Anda mengalami masalah selama penyiapan konektivitas, lihat halaman pemecahan masalah konektivitas.

gcloud

Skrip bash berikut menggunakan Google Cloud CLI untuk membuat penyiapan produsen Private Service Connect untuk database tujuan. Perhatikan bahwa beberapa setelan default mungkin perlu disesuaikan; misalnya, rentang CIDR subnet Private Service Connect.

#!bin/bash

# Create the VPC network for the Database Migration Service Private Service Connect.
gcloud compute networks create dms-psc-vpc \
--project=PROJECT_ID \
--subnet-mode=custom

# Create a subnet for the Database Migration Service Private Service Connect.
gcloud compute networks subnets create dms-psc-REGION \
--project=PROJECT_ID \
--range=10.0.0.0/16 --network=dms-psc-vpc \
--region=REGION

# Create a router required for the bastion to be able to install external
# packages (for example, Dante SOCKS server):
gcloud compute routers create ex-router-REGION \
--network dms-psc-vpc \
--project=PROJECT_ID \
--region=REGION

gcloud compute routers nats create ex-nat-REGION \
--router=ex-router-REGION \
--auto-allocate-nat-external-ips \
--nat-all-subnet-ip-ranges \
--enable-logging \
--project=PROJECT_ID \
--region=REGION

# Create the bastion VM.
gcloud compute instances create BASTION \
    --project=PROJECT_ID \
    --zone=ZONE \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --network-interface subnet=dms-psc-REGION,no-address \
    --network-interface subnet=DB_SUBNETWORK,no-address \
    --metadata=startup-script='#! /bin/bash

# Route the private IP address using the gateway of the database subnetwork.
# To find the gateway for the relevant subnetwork, go to the VPC network page 
# in the Google Cloud console. Click VPC networks, and select the database VPC
# to see the details.
ip route add CLOUD_SQL_INSTANCE_PRIVATE_IP via DB_SUBNETWORK_GATEWAY

# Install Dante SOCKS server.
apt-get install -y dante-server

# Create the Dante configuration file.
touch /etc/danted.conf

# Create a proxy.log file.
touch proxy.log

# Add the following configuration for Dante:
cat > /etc/danted.conf << EOF
logoutput: /proxy.log
user.privileged: proxy
user.unprivileged: nobody

internal: 0.0.0.0 port = PORT
external: ens5

clientmethod: none
socksmethod: none

client pass {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error disconnect
}
client block {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error
}
socks pass {
        from: 0.0.0.0/0
        to: CLOUD_SQL_INSTANCE_PRIVATE_IP/32
        protocol: tcp
        log: connect error disconnect
}
socks block {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error
}
EOF

# Start the Dante server.
systemctl restart danted

tail -f proxy.log'

# Create the target instance from the created bastion VM.
gcloud compute target-instances create bastion-ti-REGION \
--instance=BASTION \
--project=PROJECT_ID \
--instance-zone=ZONE \
--network=dms-psc-vpc

# Create a forwarding rule for the backend service.
gcloud compute forwarding-rules create dms-psc-forwarder-REGION \
--project=PROJECT_ID \
--region=REGION \
--load-balancing-scheme=internal \
--network=dms-psc-vpc \
--subnet=dms-psc-REGION \
--ip-protocol=TCP \
--ports=all \
--target-instance=bastion-ti-REGION \
--target-instance-zone=ZONE

# Create a TCP NAT subnet.
gcloud compute networks subnets create dms-psc-nat-REGION-tcp \
--network=dms-psc-vpc \
--project=PROJECT_ID \
--region=REGION \
--range=10.1.0.0/16 \
--purpose=private-service-connect

# Create a service attachment.
gcloud compute service-attachments create dms-psc-svc-att-REGION \
--project=PROJECT_ID \
--region=REGION \
--producer-forwarding-rule=dms-psc-forwarder-REGION \
--connection-preference=ACCEPT_MANUAL \
--nat-subnets=dms-psc-nat-REGION-tcp

# Create a firewall rule allowing the Private Service Connect NAT subnet.
# access the Private Service Connect subnet
gcloud compute \
--project=PROJECT_ID firewall-rules create dms-allow-psc-tcp \
--direction=INGRESS \
--priority=1000 \
--network=dms-psc-vpc \
--action=ALLOW \
--rules=all \
--source-ranges=10.1.0.0/16 \
--enable-logging

# Print out the created service attachment.
gcloud compute service-attachments describe dms-psc-svc-att-REGION \
--project=PROJECT_ID \
--region=REGION

Ganti kode berikut:

  • PROJECT_ID: project tempat Anda membuat penyiapan produsen Private Service Connect.
  • REGION: region tempat Anda membuat penyiapan produsen Private Service Connect.
  • ZONE: zona dalam REGION tempat Anda membuat semua resource zonal (misalnya, VM bastion).
  • BASTION: VM bastion yang akan dibuat.
  • DB_SUBNETWORK: subnetwork tempat traffic akan diteruskan. Subjaringan harus memiliki akses ke instance Cloud SQL.
  • DB_SUBNETWORK_GATEWAY: Gateway IPv4 subnetwork.
  • CLOUD_SQL_INSTANCE_CONNECTION_NAME: nama koneksi instance Cloud SQL (diformat sebagai project:region:name).
  • PORT: port yang akan digunakan bastion untuk mengekspos database yang mendasarinya.
  • CLOUD_SQL_INSTANCE_PRIVATE_IP: alamat IP pribadi instance Cloud SQL.

Terraform

File berikut dapat digunakan dalam modul Terraform untuk membuat penyiapan produsen Private Service Connect untuk database tujuan. Beberapa setelan default mungkin perlu disesuaikan, misalnya rentang CIDR subnet Private Service Connect.

variables.tf:

variable "project_id" {
  type        = string
  description = <<DESC
The Google Cloud project in which the setup is created. This should be the same project as
the one that the Cloud SQL instance belongs to.
DESC
}

variable "region" {
  type        = string
  description = "The Google Cloud region in which you create the Private Service Connect
regional resources."
}

variable "zone" {
  type        = string
  description = <<DESC
The Google Cloud zone in which you create the Private Service Connect zonal resources
(should be in the same region as the one specified in the "region" variable).
DESC
}

variable "cloud_sql_instance_name" {
  type        = string
  description = "The Cloud SQL instance name."
}

variable "port" {
  type        = string
  description = "The port that the bastion will use to expose the underlying database."
  default     = "5432"
}

variable "cloud_sql_instance_network" {
  type        = string
  description = <<DESC
The VPC to which the Cloud SQL instance is peered. This is where the bastion will
forward connections to (the destination database needs to be accessible in this VPC).
DESC
}

main.tf:

/* To execute the call:
terraform apply
-var="project_id=PROJECT_ID"
-var="region=REGION"
-var="zone=ZONE"
-var="cloud_sql_instance_name=CLOUD_SQL_INSTANCE_NAME"
-var="port=PORT"
-var="cloud_sql_instance_network=CLOUD_SQL_INSTANCE_NETWORK" */

locals {
  cloud_sql_instance_connection_name = "${var.project_id}:${var.region}:${var.cloud_sql_instance_name}"
}

# Needed for getting the private IP address of the Cloud SQL instance.
data "google_sql_database_instance" "csql_instance" {
  name    = var.cloud_sql_instance_name
  project = var.project_id
}

# Needed for getting the IPv4 gateway of the subnetwork for the database.
data "google_compute_subnetwork" "db_network_subnet" {
  name    = var.cloud_sql_instance_network
  project = var.project_id
  region  = var.region
}

resource "google_compute_network" "psc_sp_network" {
  name                    = "dms-psc-network"
  project                 = var.project_id
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "psc_sp_subnetwork" {
  name    = "dms-psc-subnet"
  region  = var.region
  project = var.project_id

  network = google_compute_network.psc_sp_network.id
  # CIDR range can be lower.
  ip_cidr_range = "10.0.0.0/16"
}

resource "google_compute_subnetwork" "psc_sp_nat" {
  provider = google-beta
  name     = "dms-psc-nat"
  region   = var.region
  project  = var.project_id

  network = google_compute_network.psc_sp_network.id
  purpose = "PRIVATE_SERVICE_CONNECT"
  # CIDR range can be lower.
  ip_cidr_range = "10.1.0.0/16"
}

resource "google_compute_service_attachment" "psc_sp_service_attachment" {
  provider = google-beta
  name     = "dms-psc-svc-att"
  region   = var.region
  project  = var.project_id

  enable_proxy_protocol = false
  connection_preference = "ACCEPT_MANUAL"
  nat_subnets           = [google_compute_subnetwork.psc_sp_nat.id]
  target_service        = google_compute_forwarding_rule.psc_sp_target_direct_rule.id
}

resource "google_compute_forwarding_rule" "psc_sp_target_direct_rule" {
  name       = "dms-psc-fr"
  region     = var.region
  project    = var.project_id
  network    = google_compute_network.psc_sp_network.id
  subnetwork = google_compute_subnetwork.psc_sp_subnetwork.id

  load_balancing_scheme = "INTERNAL"
  ip_protocol           = "TCP"
  all_ports             = true

  target = google_compute_target_instance.psc_sp_target.id

}

resource "google_compute_target_instance" "psc_sp_target" {
  provider = google-beta
  name     = "dms-psc-fr-target"
  zone     = var.zone
  instance = google_compute_instance.psc_sp_bastion.id
  network  = google_compute_network.psc_sp_network.id
}

resource "google_compute_instance" "psc_sp_bastion" {
  name           = "dms-psc-cloud-sql-bastion"
  project        = var.project_id
  machine_type   = "e2-medium"
  zone           = var.zone
  can_ip_forward = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  # The incoming NIC defines the default gateway which must be the Private Service Connect subnet.
  network_interface {
    network    = google_compute_network.psc_sp_network.id
    subnetwork = google_compute_subnetwork.psc_sp_subnetwork.id
  }

  # The outgoing NIC which is on the same network as the Cloud SQL instance.
  network_interface {
    network = data.google_compute_subnetwork.db_network_subnet.network
  }

  metadata_startup_script = <<SCRIPT

#!/bin/bash

# Route the private IP address of the database using the gateway of the database subnetwork.
# To find the gateway for the relevant subnetwork, go to the VPC network page 
# in the Google Cloud console. Click VPC networks, and select the database VPC
# to see the details.
ip route add ${data.google_sql_database_instance.csql_instance.private_ip_address} \
via ${data.google_compute_subnetwork.db_network_subnet.gateway_address}

# Install Dante SOCKS server.
apt-get install -y dante-server

# Create the Dante configuration file.
touch /etc/danted.conf

# Create a proxy.log file.
touch proxy.log

# Add the following configuration for Dante:
cat > /etc/danted.conf << EOF
logoutput: /proxy.log
user.privileged: proxy
user.unprivileged: nobody

internal: 0.0.0.0 port = ${var.port}
external: ens5

clientmethod: none
socksmethod: none

client pass {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error disconnect
}
client block {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error
}
socks pass {
        from: 0.0.0.0/0
        to: ${data.google_sql_database_instance.csql_instance.private_ip_address}/32
        protocol: tcp
        log: connect error disconnect
}
socks block {
        from: 0.0.0.0/0
        to: 0.0.0.0/0
        log: connect error
}
EOF

# Start the Dante server.
systemctl restart danted

tail -f proxy.log

SCRIPT
}

# Required firewall rules:

/* Firewall rule allowing the Private Service Connect NAT subnet to access
the Private Service Connect subnet. */
resource "google_compute_firewall" "psc_sp_in_fw" {
  name    = "dms-psc-ingress-nat-fw"
  project = var.project_id
  network = google_compute_network.psc_sp_network.id

  log_config {
    metadata = "INCLUDE_ALL_METADATA"
  }

  allow {
    protocol = "all"
  }

  priority  = 1000
  direction = "INGRESS"
  source_ranges = [google_compute_subnetwork.psc_sp_nat.ip_cidr_range]
}

/* The router that the bastion VM uses to install external packages
(for example, Dante SOCKS server). */

resource "google_compute_router" "psc_sp_ex_router" {
  name    = "dms-psc-external-router"
  project = var.project_id
  region  = var.region
  network = google_compute_network.psc_sp_network.id
}

resource "google_compute_router_nat" "psc_sp_ex_router_nat" {
  name    = "dms-psc-external-router-nat"
  project = var.project_id
  region  = var.region
  router  = google_compute_router.psc_sp_ex_router.name

  nat_ip_allocate_option             = "AUTO_ONLY"
  source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"

  log_config {
    enable = true
    filter = "ERRORS_ONLY"
  }
}

outputs.tf:

# The Private Service Connect service attachment.
output "service_attachment" {
  value = google_compute_service_attachment.psc_sp_service_attachment.id
}