我有应用程序容器和在同一个pod中运行的cloudsql代理容器 .

创建群集后,代理容器的日志似乎正确:

$kubectl logs users-app-HASH1-HASH2 cloudsql-proxy

2018/08/03 18:58:45 using credential file for authentication; email=it-test@tutorial-bookshelf-xxxxxx.iam.gserviceaccount.com

2018/08/03 18:58:45 Listening on 127.0.0.1:3306 for tutorial-bookshelf-xxxxxx:asia-south1:it-sample-01

2018/08/03 18:58:45 Ready for new connections

但是,来自应用程序容器的日志会导致无法连接localhost错误:

$kubectl logs users-app-HASH1-HASH2 app-container

...

19:27:38 users_app.1 | return Connection(*args, **kwargs)

19:27:38 users_app.1 | File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 327, in __init__

19:27:38 users_app.1 | self.connect()

19:27:38 users_app.1 | File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect

19:27:38 users_app.1 | raise exc

19:27:38 users_app.1 | sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)") (Background on this error at: http://sqlalche.me/e/e3q8)

SQLALCHEMY_DATABASE_URI 是 'mysql+pymysql://{user}:{password}@/{database}?unix_socket=/cloudsql/{cloudsql_connection_name}' 并填充了正确的值(我使用kubectl机密设置的凭据) .

我确定我在这里做了些傻事,所以我希望有人在GCP上有更多的经验可以看看,并提供有关解决这个问题的指示 .

更新:我刚刚访问了GCP kubernetes引擎页面,并在应用程序容器上打开了一个shell,并尝试连接到 Cloud sql实例 . 这似乎有效 .

$gcloud container cluster ......... -it /bin/sh

#python

>>> import pymysql

>>> connection = pymysql.connect(host='127.0.0.1', user='user', password='password', db='db')

>>> with connection.cursor() as cursor:

... cursor.execute("show databases;")

... tables = cursor.fetchall()

...

5

但以下(当我尝试通过sqlalchemy连接时)失败:

>>> connection = pymysql.connect(host='127.0.0.1', user='u', password='p', db='d', unix_socket='/cloudsql/CONNECTION_NAME')

...

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 2] No such file or directory)")

>>> from sqlalchemy import create_engine

>>> engine = create_engine('mysql://user:password@localhost/db')

>>> engine.connect()

Traceback (most recent call last):

...

sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, 'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")') (Background on this error at: http://sqlalche.me/e/e3q8)

>>> engine = create_engine('mysql+pymysql://user:password@/db?unix_socket=/cloudsql/tutorial-bookshelf-xxxx:asia-south1:test-01')

>>> engine.connect()

Traceback (most recent call last):

...

raise exc

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)") (Background on this error at: http://sqlalche.me/e/e3q8)

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐