尽管连接测试成功,但尝试在 Cloud Functions 中连接时使用私有 IP 连接到 Cloud SQL 时出现超时

发布于 2025-01-17 17:56:25 字数 2256 浏览 3 评论 0原文

我正在尝试通过云功能连接到具有私有IP的云SQL实例,该云功能通过VPC连接器运行所有出口流量。

我进行了一个连接测试,给出了以下结果:

”在此处输入图像描述“

然后,我部署了一个云函数遵循文档并运行以下代码:

import sqlalchemy
import os
from google.cloud import storage

# SQLAlchemy==1.4.28
# psycopg2-binary==2.9.2
# google-cloud-storage==2.2.1

def hello_world(request):
    # Remember - storing secrets in plaintext is potentially unsafe. Consider using
    # something like https://cloud.google.com/secret-manager/docs/overview to help keep
    # secrets secret.
    db_user = '<DB-USER>'
    db_pass = '<DB-PASSWORD>'
    db_name = 'postgres'
    db_host = '172.16.0.5:5432'

    # Extract port from db_host if present,
    # otherwise use DB_PORT environment variable.
    host_args = db_host.split(":")
    if len(host_args) == 1:
        db_hostname = db_host
        db_port = os.environ["DB_PORT"]
    elif len(host_args) == 2:
        db_hostname, db_port = host_args[0], int(host_args[1])

    print(f"{db_hostname}, {db_port}")
    pool = sqlalchemy.create_engine(
        # Equivalent URL:
        # mysql+pymysql://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>
        sqlalchemy.engine.url.URL.create(
            drivername="postgresql+psycopg2",
            username=db_user,  # e.g. "my-database-user"
            password=db_pass,  # e.g. "my-database-password"
            host=db_hostname,  # e.g. "127.0.0.1"
            port=db_port,  # e.g. 5432
            database=db_name,  # e.g. "my-database-name"
        )
    )
    print(f'Created engine : {pool}')
    pool.connect()
    print(f'Done engine : {pool}')

    return 'Ended well', 200

但是,它无法连接到实例,并且功能时间在pool.connect()上输出。 :

我确认源IP与连接测试的期望一致(11.0.0.11)。

原因是什么,或者我该怎么做才能找出实际原因?

I am trying to connect to a Cloud SQL instance with Private IP through a Cloud Function which runs all egress traffic through a VPC connector.

I did a connectivity test which gave the following results:

enter image description here

Then, I deployed a Cloud Function following the docs and run the following code:

import sqlalchemy
import os
from google.cloud import storage

# SQLAlchemy==1.4.28
# psycopg2-binary==2.9.2
# google-cloud-storage==2.2.1

def hello_world(request):
    # Remember - storing secrets in plaintext is potentially unsafe. Consider using
    # something like https://cloud.google.com/secret-manager/docs/overview to help keep
    # secrets secret.
    db_user = '<DB-USER>'
    db_pass = '<DB-PASSWORD>'
    db_name = 'postgres'
    db_host = '172.16.0.5:5432'

    # Extract port from db_host if present,
    # otherwise use DB_PORT environment variable.
    host_args = db_host.split(":")
    if len(host_args) == 1:
        db_hostname = db_host
        db_port = os.environ["DB_PORT"]
    elif len(host_args) == 2:
        db_hostname, db_port = host_args[0], int(host_args[1])

    print(f"{db_hostname}, {db_port}")
    pool = sqlalchemy.create_engine(
        # Equivalent URL:
        # mysql+pymysql://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>
        sqlalchemy.engine.url.URL.create(
            drivername="postgresql+psycopg2",
            username=db_user,  # e.g. "my-database-user"
            password=db_pass,  # e.g. "my-database-password"
            host=db_hostname,  # e.g. "127.0.0.1"
            port=db_port,  # e.g. 5432
            database=db_name,  # e.g. "my-database-name"
        )
    )
    print(f'Created engine : {pool}')
    pool.connect()
    print(f'Done engine : {pool}')

    return 'Ended well', 200

However, it is unable to connect to the instance, and the function times out at pool.connect():

enter image description here

I confirm that the source IP is in line with the expectation of the connectivity test (11.0.0.11).

What could be the reason, or what could I do to find out the actual cause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文