简体   繁体   English

GCP Cloud Functions 使用私有 IP 连接到云 sql

[英]GCP Cloud Functions connecting to cloud sql with private IP

I'm following this example to make a connection from Cloud Function to Postgres Cloud SQL: https://cloud.google.com/functions/docs/sql .我正在按照此示例建立从 Cloud Function 到 Postgres Cloud SQL 的连接: https : //cloud.google.com/functions/docs/sql

When I create a test Cloud SQL instance with Public IP and I trigger the cloud function, it connects to the cloud SQL instance and returns something.当我使用公共 IP 创建测试 Cloud SQL 实例并触发云函数时,它会连接到云 SQL 实例并返回一些内容。 For security reasons I can't leave Public IP on, so when I select Private IP on the cloud SQL instance I get:出于安全原因,我不能保留公共 IP,因此当我在云 SQL 实例上选择私有 IP 时,我得到:

Error: function crashed. Details:
could not connect to server: Connection refused
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/cloud-sql-test-250613:us-central1:myinstance-2/.s.PGSQL.5432"?

I can't get from the documentation what is the contract between cloud function and the cloud sql instance.我无法从文档中获得云函数和云 sql 实例之间的契约是什么。 If we are using unix domain sockets should I care at all about IPs?如果我们使用 unix 域套接字,我应该关心 IP 吗? Does it matter if it's public or private?它是公共的还是私人的,这很重要吗? If it does matter, do I have to go through all the process of setting up Private IP infrastructure?如果确实重要,我是否必须完成设置私有 IP 基础设施的所有过程? Do I need serverless VPC?我需要无服务器 VPC 吗?

I've managed to achieve a connectivity between a Cloud Function and Cloud SQL private instance by doing this.通过这样做,我设法实现了 Cloud Function 和 Cloud SQL 私有实例之间的连接。

It seems that it does matter if you disable public IPs, whenever I disabled public IP's I kept getting ERR CONN REFUSED, which seems to be your case,to have your Cloud SQL instance only with private IP, I think you do have to use Serverless VPC.如果您禁用公共 IP,这似乎很重要,每当我禁用公共 IP 时,我一直收到 ERR CONN REFUSED,这似乎是您的情况,让您的 Cloud SQL 实例仅使用私有 IP,我认为您必须使用无服务器专有网络。

This is what I would recommend you to try:这是我建议您尝试的方法:

Make sure that all your infrastructure is in the same region (Cloud SQL, Cloud Function,VPC Connector)确保您的所有基础架构都在同一区域(Cloud SQL、Cloud Function、VPC 连接器)

Take these steps,please:请采取以下步骤:

  1. Set Cloud SQL instance to private only connectivity.将 Cloud SQL 实例设置为仅限专用连接。 (Cloud SQL Instance > Connections) (云 SQL 实例 > 连接)

  2. Make sure that your private CloudSQL instance is on the desired “Associated Networking” (VPC).确保您的私有 CloudSQL 实例位于所需的“关联网络”(VPC) 上。

  3. Create a VPC connector on the VPC network that your Cloud SQL instance is located.在您的 Cloud SQL 实例所在的 VPC 网络上创建一个 VPC 连接器。 (the one associated with the MySql instance) (与 MySql 实例关联的那个)

To create a connector go to: VPC Network > VPC Serverless Access > Create Connector要创建连接器,请转到:VPC 网络 > VPC 无服务器访问 > 创建连接器

In VPC Network > [Your VPC] > VPC Network Peering you can check if the connection is correct to your Cloud SQL instance.在 VPC 网络 > [您的 VPC] > VPC 网络对等互连中,您可以检查与 Cloud SQL 实例的连接是否正确。

  1. Create a Cloud Function using the code in your desired language.使用所需语言的代码创建云函数。 (You can test with the examples in the documentation.) (您可以使用文档中的示例进行测试。)

When you create your Cloud Function make sure to set it in the same region, while also adding the VPC Connector you created to the "Egress Settings" option in your Cloud Function.创建 Cloud Function 时,请确保将其设置在同一区域中,同时将您创建的 VPC 连接器添加到 Cloud Function 中的“出口设置”选项。

If you try to create a VPC Connector through the GCP Console, you will only get 1 zone to pick from.如果您尝试通过 GCP Console 创建 VPC 连接器,您将只能选择 1 个区域。 But if you use the cloud shell you can define another areas.但是,如果您使用云外壳,则可以定义其他区域。 You can try that with this command and in these areas.您可以使用此命令并在这些区域中进行尝试。

gcloud beta compute networks vpc-access connectors create [CONNECTOR_NAME] \ 
--network [VPC_NETWORK] \ 
--region [REGION] \ 
--range [IP_RANGE]

Areas:领域:

us-central1, us-east1, europe-west1 us-central1, us-east1, europe-west1

Please let me know if this worked for you.如果这对你有用,请告诉我。

UPDATE:更新:

Hello again alobodzk,再次你好 alobodzk,

Try making your Cloud Function in Python (once making sure that all of the previous steps are OK).尝试在 Python 中创建您的 Cloud Function(一旦确保前面的所有步骤都正常)。

Try this code:试试这个代码:

Cloud Function index.js (Replace all the connector data with your own credentials) Cloud Function index.js(用您自己的凭据替换所有连接器数据)

import mysql.connector
from mysql.connector import Error


def mysql_demo(request):
    import mysql.connector
    from mysql.connector import Error
    try:
        connection = mysql.connector.connect(host='CloudSQL Instance Private IP', database='Database Name, user='UserName', password='Password')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL database... MySQL Server version on ",db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print ("Your connected to - ", record)
    except Error as e :
        print ("Error while connecting to MySQL", e)
    finally:
        #closing database connection.
        if(connection.is_connected()):
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
# [END functions_sql_mysql]

Cloud Function requirements.txt云功能要求.txt

psycopg2==2.7.7
PyMySQL==0.9.3
mysql-connector-python

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

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