同时创建多个具有私有 IP 的 Google Cloud SQL 实例时如何解决“发生未知错误”?
Posted
技术标签:
【中文标题】同时创建多个具有私有 IP 的 Google Cloud SQL 实例时如何解决“发生未知错误”?【英文标题】:How to fix "An Unknown Error Occurred" when creating multiple Google Cloud SQL instances with private IP simultaneously? 【发布时间】:2019-09-23 05:37:24 【问题描述】:我们的云后端设置包含 5 个用于 Postgres 实例的 Cloud SQL。我们使用 Terraform 管理我们的基础设施。我们使用公共 IP 和 Cloud SQL container 从 GKE 连接它们。
为了简化我们的设置,我们希望通过迁移到私有 IP 来摆脱代理容器。我尝试关注Terraform guide。虽然创建单个实例可以正常工作,但尝试同时创建 5 个实例会导致 4 个失败和一个成功:
在失败实例上出现在 Google Clod 控制台中的错误是“发生未知错误”:
以下是重现它的代码。注意count = 5
这一行:
resource "google_compute_network" "private_network"
provider = "google-beta"
name = "private-network"
resource "google_compute_global_address" "private_ip_address"
provider = "google-beta"
name = "private-ip-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "$google_compute_network.private_network.self_link"
resource "google_service_networking_connection" "private_vpc_connection"
provider = "google-beta"
network = "$google_compute_network.private_network.self_link"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["$google_compute_global_address.private_ip_address.name"]
resource "google_sql_database_instance" "instance"
provider = "google-beta"
count = 5
name = "private-instance-$count.index"
database_version = "POSTGRES_9_6"
depends_on = [
"google_service_networking_connection.private_vpc_connection"
]
settings
tier = "db-custom-1-3840"
availability_type = "REGIONAL"
ip_configuration
ipv4_enabled = "false"
private_network = "$google_compute_network.private_network.self_link"
provider "google-beta"
version = "~> 2.5"
credentials = "credentials.json"
project = "PROJECT_ID"
region = "us-central1"
zone = "us-central1-a"
我尝试了几种选择:
在创建google_service_networking_connection
后等待一分钟,然后同时创建所有实例,但我遇到了同样的错误。
为每个实例创建一个地址范围和一个 google_service_networking_connection
,但我收到一个错误,即无法同时创建 google_service_networking_connection
。
为每个实例创建一个地址范围和一个链接到所有实例的 google_service_networking_connection
,但我遇到了同样的错误。
【问题讨论】:
【参考方案1】:找到了一个丑陋但有效的解决方案。 is a bug in GCP 虽然无法完成,但不会阻止同时创建实例。既没有关于它的文档,也没有有意义的错误消息。它也出现在Terraform Google provider issue tracker 中。
另一种方法是在实例之间添加依赖关系。这允许他们的创建成功完成。但是,每个实例都需要几分钟才能创建。这累积到许多花费的分钟。如果我们在实例创建之间添加 60 秒的人为延迟,我们可以避免失败。备注:
延迟所需的秒数取决于实例层。例如,对于db-f1-micro
,30 秒就足够了。对于db-custom-1-3840
,它们还不够。
我不确定db-custom-1-3840
所需的确切秒数是多少。 30 秒还不够,60 秒还不够。
以下是解决问题的代码示例。它仅显示 2 个实例,因为由于 depends_on
的限制,我无法使用计数功能,并且显示 5 个实例的完整代码会很长。它适用于 5 个实例:
resource "google_compute_network" "private_network"
provider = "google-beta"
name = "private-network"
resource "google_compute_global_address" "private_ip_address"
provider = "google-beta"
name = "private-ip-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "$google_compute_network.private_network.self_link"
resource "google_service_networking_connection" "private_vpc_connection"
provider = "google-beta"
network = "$google_compute_network.private_network.self_link"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["$google_compute_global_address.private_ip_address.name"]
locals
db_instance_creation_delay_factor_seconds = 60
resource "null_resource" "delayer_1"
depends_on = ["google_service_networking_connection.private_vpc_connection"]
provisioner "local-exec"
command = "echo Gradual DB instance creation && sleep $local.db_instance_creation_delay_factor_seconds * 0"
resource "google_sql_database_instance" "instance_1"
provider = "google-beta"
name = "private-instance-delayed-1"
database_version = "POSTGRES_9_6"
depends_on = [
"google_service_networking_connection.private_vpc_connection",
"null_resource.delayer_1"
]
settings
tier = "db-custom-1-3840"
availability_type = "REGIONAL"
ip_configuration
ipv4_enabled = "false"
private_network = "$google_compute_network.private_network.self_link"
resource "null_resource" "delayer_2"
depends_on = ["google_service_networking_connection.private_vpc_connection"]
provisioner "local-exec"
command = "echo Gradual DB instance creation && sleep $local.db_instance_creation_delay_factor_seconds * 1"
resource "google_sql_database_instance" "instance_2"
provider = "google-beta"
name = "private-instance-delayed-2"
database_version = "POSTGRES_9_6"
depends_on = [
"google_service_networking_connection.private_vpc_connection",
"null_resource.delayer_2"
]
settings
tier = "db-custom-1-3840"
availability_type = "REGIONAL"
ip_configuration
ipv4_enabled = "false"
private_network = "$google_compute_network.private_network.self_link"
provider "google-beta"
version = "~> 2.5"
credentials = "credentials.json"
project = "PROJECT_ID"
region = "us-central1"
zone = "us-central1-a"
provider "null"
version = "~> 1.0"
【讨论】:
【参考方案2】:如果有人以稍微不同的情况登陆这里(在专用网络中创建 google_sql_database_instance 会导致“未知错误”):
-
手动启动一个 Cloud SQL 实例(这将启用 servicenetworking.googleapis.com 以及该项目的一些其他 API)
运行您的清单
终止在步骤 1 中创建的实例。
在那之后为我工作
¯_(ツ)_/¯
【讨论】:
当您说“手动启动一个 Cloud SQL 实例”时,您是什么意思?非私人的? 使用浏览器打开 GCP 控制台并通过“向导”:D【参考方案3】:我在这里的情况略有不同,与@Grigorash Vasilij 相同 (在专用网络中创建 google_sql_database_instance 会导致“未知错误”)。
我正在使用 UI 在私有 VPC
上部署 SQL 实例,但出于某种原因,这也给我带来了“未知错误”。我终于改用gcloud
命令解决了(为什么它有效而没有 UI?IDK,也许 UI 与命令不一样)
gcloud --project=[PROJECT_ID] beta sql instances create [INSTANCE_ID]
--network=[VPC_NETWORK_NAME]
--no-assign-ip
follow this for more details
【讨论】:
以上是关于同时创建多个具有私有 IP 的 Google Cloud SQL 实例时如何解决“发生未知错误”?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用私有IP在两个项目之间共享Google Cloud SQL实例?
仅具有私有 IP 的 Azure VM 的 Ansible 动态清单
如何在谷歌云平台中使用私有 IP 从我的电脑连接到 sql 实例