简体   繁体   English

如何使用 Auth Proxy 和 Python 连接到 AlloyDB?

[英]How to connect to AlloyDB using Auth Proxy and Python?

I am trying to connect to the AlloyDB in google cloud via alloydb-auth-proxy.我正在尝试通过alloydb-auth-proxy 连接到谷歌云中的AlloyDB。 I am not able to connect to it successfully.我无法成功连接到它。 I am getting error while trying to do this.尝试执行此操作时出现错误。

I followed the instruction mentioned inhttps://cloud.google.com/alloydb/docs/auth-proxy/connect#python and https://github.com/GoogleCloudPlatform/alloydb-auth-proxy#example-invocations我按照https://cloud.google.com/alloydb/docs/auth-proxy/connect#pythonhttps://github.com/GoogleCloudPlatform/alloydb-auth-proxy#example-invocations中提到的说明进行操作

I am using FastAPI in the backend and using sqlalchemy.我在后端使用 FastAPI 并使用 sqlalchemy。


SQLALCHEMY_DATABASE_URL = "postgresql+psycopg2://<user>:<password>@\
localhost/postgres"

engine = create_engine(SQLALCHEMY_DATABASE_URL)

SesionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=True)

I start the auth proxy using the credentials我使用凭据启动身份验证代理

alloydb-auth-proxy "projects/<project-id>/locations/<region>/clusters/<database-id>/instances/<instance-id>" --credentials-file "key.json"

I am leaving the address and port as default ie address to 127.0.0.1 and port 5432 .我将地址和端口保留为默认值,即地址为127.0.0.1和端口5432

The proxy starts代理启动

[projects/<project-id>/locations/<region>/clusters/<database-id>/instances/<instance-id>] Listening on 127.0.0.1:5432
The proxy has started successfully and is ready for new connections!

But when I run the app it's getting me an error in console-但是当我运行该应用程序时,它在控制台中出现错误-

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: server closed the connection unexpectedly    
        This probably means the server terminated abnormally
        before or while processing the request.

and in proxy cmd并在代理 cmd

[projects/<project-id>/locations/<region>/clusters/<database-id>/instances/<instance-id>] failed to connect to instance: Dial error: failed to dial (instance URI = "<project-id>/<region-id>/
<database-id>/<instance-id>"): dial tcp xx.xx.xx.x:5433: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

What's happening here?这里发生了什么事?

AlloyDB currently only has the option to connect to it by Private IP. AlloyDB 目前只能选择通过 Private IP 连接到它。 This means that your local machine will be unable to reach the cluster regardless of your connectivity methods (auth proxy, Python connector, direct connection, etc).这意味着无论您使用何种连接方式(身份验证代理、Python 连接器、直接连接等),您的本地计算机都无法访问集群。

To connect you either need to be connecting from within the same network (VPC) as the AlloyDB cluster, or you'll need to set up something like a bastion instance which has a public entry point that shares the network with the AlloyDB cluster.要进行连接,您需要从与 AlloyDB 集群相同的网络 (VPC) 进行连接,或者您需要设置类似堡垒实例的东西,该实例具有与 AlloyDB 集群共享网络的公共入口点。

To test this, easiest way is to spin up the smallest GCE instance you can on the same VPC as the AlloyDB cluster.要对此进行测试,最简单的方法是在与 AlloyDB 集群相同的 VPC 上启动最小的 GCE 实例。 Then SSH into that instance, and use the psql client to confirm you can connect to the AlloyDB instance.然后 SSH 进入该实例,并使用 psql 客户端确认您可以连接到 AlloyDB 实例。 Once you confirm that, for development you either need to push your application to that GCE instance to be able to connect, or set up some connectivity between you and the GCE instance.一旦您确认,对于开发,您需要将您的应用程序推送到该 GCE 实例以便能够连接,或者在您和 GCE 实例之间建立一些连接。

There are a few ways to do that, I'd recommend locking down the GCE instance as hard as you can and reverse SSH tunnel to the instance from your local machine.有几种方法可以做到这一点,我建议尽可能锁定 GCE 实例,并从本地机器反向 SSH 隧道到实例。 Or set up a VPN (Cloud VPN is an option, but fairly expensive, so running your own VPN service on the GCE instance is an option, just more overhead and maintenance).或者设置一个 VPN(Cloud VPN 是一个选项,但相当昂贵,因此在 GCE 实例上运行您自己的 VPN 服务是一个选项,只是更多的开销和维护)。 You can also set up something like a Socks5 proxy on the bastion instance to do forwarding from there to your AlloyDB cluster and that would also work.您还可以在堡垒实例上设置诸如 Socks5 代理之类的东西,以从那里转发到您的 AlloyDB 集群,这也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM