通过 Docker 中的身份验证代理排查 Google Cloud SQL 连接问题

问题描述 投票:0回答:2

我已设置

docker-compose.yml
来运行基于节点的 API 应用程序,并设置 Google 的 Cloud SQL 身份验证代理,以便 API 连接到我的 Cloud SQL 实例。两个应用程序都会启动,但我在 API 容器中收到以下错误:

Error: connect ECONNREFUSED 127.0.0.1:3307 at TCPConnectWrap.afterConnect 

即使我的代理容器中的终端显示:

Authorizing with the credentials file at "/secrets/cloudsql/credentials.json"
[myproject:myregion:myinstance] Listening on 127.0.0.1:3307
The proxy has started successfully and is ready for new connections!

docker-compose.yml(参考

version: '3.8'
services:
  proxy:
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.8.2
    # also tried --address 0.0.0.0
    command: myproject:myregion:myinstance --credentials-file=/secrets/cloudsql/credentials.json --address 127.0.0.1 --port=3307
    ports:
      - 127.0.0.1:3307:3307
    volumes:
      - ./cloud-sql.credentials.json:/secrets/cloudsql/credentials.json
    restart: always

  web-api:
    build:
      context: .
      dockerfile: ./apps/web-api/Dockerfile
    ports:
      - 3333:3333
    depends_on:
      - proxy

身份验证

安装 - 如您所见,我正在安装

credentials.json
,这是我的服务帐户的密钥文件,具有
Cloud SQL Client
权限。我通过查看 Docker 桌面中的容器文件确认该文件已正确安装。

Reading - 我还确认代理正在读取密钥文件。为此,我删除了密钥文件中的

type
属性,并收到错误
config error: missing 'type' field in credentials

IAM 权限 - 我能够在本地运行代理并使用此密钥文件成功与桌面客户端连接,因此密钥文件具有正确的权限(Cloud SQL 客户端)。

但这很奇怪...

我从服务帐户电子邮件中删除了一个字符,以测试密钥文件身份验证是否会失败。在本地,我收到错误

Invalid grant: account not found
,但在我的 docker 容器中,代理运行时没有错误。

我不知道这是怎么回事,也不知道如何排除故障。我是否配置错误?当我知道代理正在正确读取密钥文件时,代理如何以错误的密钥文件启动?

docker proxy google-cloud-sql
2个回答
2
投票

您需要使用 links 引用您的代理容器。

尝试这个(根据需要进行调整):

version: '3.8'
services:
  proxy:
    # binding to 0.0.0.0 is important
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.8.2
    command: myproject:myregion:myinstance --credentials-file=/secrets/cloudsql/credentials.json --address 0.0.0.0 --port=3307
    volumes:
    - ./cloud-sql.credentials.json:/secrets/cloudsql/credentials.json

  psql:
    image: postgres
    # using "proxy" as the hostname
    command: psql "host=proxy port=3307 user=postgres dbname=postgres password=mycoolpassword" -c "select now()"
    depends_on:
      - proxy
    links:
    - "proxy"

0
投票

这是一个 unix 套接字连接的示例:

version: '3.8'

services:
  proxy:
    container_name: proxy
    image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.8.2
    command: myproject:myregion:myinstance --unix-socket /tmp --credentials-file /secrets/cloudsql/credentials.json
    volumes:
      - ./cloud-sql.credentials.json:/secrets/cloudsql/credentials.json
      - socket:/tmp
    restart: always

  web-api:
    container_name: web-api
    build:
      context: .
      dockerfile: ./apps/web-api/Dockerfile
    ports:
      - 3333:3333
    volumes:
      - socket:/tmp
    depends_on:
      - proxy
    env_file:
      - .env
    restart: always

volumes:
  socket:
© www.soinside.com 2019 - 2024. All rights reserved.