本页介绍了如何设置 Database Migration Service,以使用目标 AlloyDB for PostgreSQL 集群的专用 IP 地址将 Oracle 迁移到 AlloyDB for PostgreSQL 工作负载。借助专用连接,Database Migration Service 无需通过互联网或使用外部 IP 地址即可访问服务。
Database Migration Service 使用 Private Service Connect 通过专用 IP 地址连接到 AlloyDB for PostgreSQL 集群。借助 Private Service Connect,您可以向传入的安全连接公开目标数据库,并控制哪些人可以访问该数据库。
适用于启用了 PSC 的 AlloyDB for PostgreSQL 集群的 Private Service Connect
如果您创建启用了 Private Service Connect 的 AlloyDB for PostgreSQL 目标实例,则无需进行任何其他网络设置,即可使用专用 IP 连接。在创建目标数据库连接配置文件时,您只需确保以下几点:
使用 AlloyDB for PostgreSQL 集群的 DNS 记录,而不是目标主机的主机名。
仅限通过 Google Cloud CLI 或 API 进行程序化访问:在请求中明确指定目标的服务附件(当您在 Google Cloud 控制台中创建连接配置文件时,Database Migration Service 会自动执行此操作)。
适用于未启用 PSC 的 AlloyDB for PostgreSQL 集群的 Private Service Connect
如需使用 Private Service Connect 公开 AlloyDB for PostgreSQL 集群中的服务,请在项目中创建服务连接。您可以使用创建 Private Service Connect 生产者设置部分中的示例 gcloud
和 Terraform 脚本来创建所需的资源。
下图显示了 Database Migration Service 用于设置与 AlloyDB for PostgreSQL 集群的专用连接的各种计算和网络资源。
Database Migration Service 工作器是负责 Database Migration Service 迁移的内部处理单元。它使用出站 Private Service Connect 转发规则将流量从工作器发送到堡垒虚拟机 (VM) 所在的 VPC 网络。
脚本可帮助您创建以下关键资源:
- 服务连接:一种资源,用于通过将流量转发到其目标服务来公开服务。在该图中,目标服务是传入转发规则。
- 传入转发规则:一种流量转发器,用于将传入流量从服务附件路由到专用目标(例如虚拟机或实例组)。在该图中,该规则会将流量转发到堡垒虚拟机。
- 堡垒主机:一个具有两个网络接口控制器 (NIC) 的虚拟机,其中一个 NIC 连接到专用 Private Service Connect 网络,另一个 NIC 连接到 AlloyDB for PostgreSQL 集群对等互连的网络。堡垒运行着 Dante SOCKS 服务器,用于安全地转发连接。示例脚本会在 Private Service Connect 网络中为您创建专用网络和子网。
您使用服务附件发布服务的 Google Cloud 项目是服务提供方。服务使用方是 Database Migration Service。 如需详细了解 Private Service Connect、其设置和架构,请参阅 Private Service Connect 文档。
创建 Private Service Connect 提供方设置
您可以使用以下脚本创建 Private Service Connect 设置并连接到目标 AlloyDB for PostgreSQL 集群。如果您在连接设置过程中遇到问题,请参阅连接问题排查页面。
gcloud
以下 Bash 脚本使用 Google Cloud CLI 为目标数据库创建 Private Service Connect 生产者设置。请注意,您可能需要调整某些默认值;例如,Private Service Connect 子网 CIDR 范围。
#!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 ALLOYDB_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: ALLOYDB_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
替换以下内容:
- PROJECT_ID:您创建 Private Service Connect 提供方设置的项目。
- REGION:您创建 Private Service Connect 提供方设置时所在的区域。
- ZONE:REGION 中的可用区,您可以在其中创建所有可用区资源(例如堡垒虚拟机)。
- BASTION:要创建的堡垒虚拟机。
- DB_SUBNETWORK:要将流量转发到的子网。子网需要有权访问 AlloyDB for PostgreSQL 集群。
- DB_SUBNETWORK_GATEWAY:子网的 IPv4 网关。
- PORT:堡垒主机将用来公开底层数据库的端口。
- ALLOYDB_INSTANCE_PRIVATE_IP:AlloyDB for PostgreSQL 集群的专用 IP 地址。
Terraform
您可以在 Terraform 模块中使用以下文件为目标数据库创建 Private Service Connect 生产者设置。您可能需要调整某些默认值,例如 Private Service Connect 子网 CIDR 范围。
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 AlloyDB for PostgreSQL cluster 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 "primary_instance_private_ip" {
type = string
description = "The cluster's primary instance private IP"
}
variable "port" {
type = string
description = "The port that the bastion will use to expose the underlying database."
default = "5432"
}
variable "alloydb_cluster_network" {
type = string
description = <<DESC
The VPC to which the AlloyDB for PostgreSQL cluster 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="primary_instance_private_ip=PRIMARY_INSTANCE_PRIVATE_IP"
-var="port=PORT"
-var="alloydb_cluster_network=ALLOYDB_CLUSTER_NETWORK" */
# Needed for getting the IPv4 gateway of the subnetwork for the database.
data "google_compute_subnetwork" "db_network_subnet" {
name = var.alloydb_cluster_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 AlloyDB for PostgreSQL cluster.
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 ${var.primary_instance_private_ip} \
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: ${var.primary_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
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
}