使用来自 GCE 或 GKE 的私有 IP 连接到云 SQL

Connecting to Cloud SQL with Private IP from GCE or GKE

我正在尝试使用私有 IP 从部署在 GoogleCompute 集群中的 pod 连接到 Postgres 数据库 (CloudSQL),但我只收到连接超时错误。

我使用以下设置 GCP 集群:

gcloud beta container clusters create "gcp-cluster" --zone "europe-west1-b" --no-enable-basic-auth --cluster-version "1.13.6-gke.13" --machine-type "n1-standard-1" --image-type "COS" --disk-type "pd-ssd" --disk-size "20" --metadata disable-legacy-endpoints=true --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append","https://www.googleapis.com/auth/sqlservice.admin","https://www.googleapis.com/auth/sqlservice" --num-nodes "2" --enable-stackdriver-kubernetes --enable-ip-alias --network "projects/XXX/global/networks/default" --subnetwork "projects/XXX/regions/europe-west1/subnetworks/default" --default-max-pods-per-node "110" --enable-autoscaling --min-nodes "2" --max-nodes "20" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair --maintenance-window "19:00"

然后我部署了一个指向 postgres 数据库 IP 地址的 wildfly(在同一 zone/region 中创建的数据库),但我只得到连接超时。启用源为 0.0.0.0/0 的 public IP 地址后,我可以获得连接。

知道使用私有 IP 地址吗?

私有 IP 意味着通过私有 IP 文档的 Virtual Private Cloud (VPC). You have to use a resource (in this case, GCE instance) that is also on that VPC to be able to reach it. See the environment requirements 页面访问云 SQL。

未来读者请注意:在 Public IP 地址上将 0.0.0.0/0 列入白名单是一个非常糟糕的主意。这实质上允许整个互联网尝试连接到您的实例,并且不应在任何延长的时间内保持启用状态。

您的 gke 集群位于 europe-west1-b。假设您使用默认网络,您必须为 europe-west1 子网启用私有 Google 访问。单击子网以查看详细信息并根据需要进行编辑,将 Private Google Access 设置为“On”。

几个小时后,我通过为 VM 实例启用 CloudSql 访问范围建立了连接。