使具有私有IP连接的GCP云SQL等效于云SQL代理



GCP建议通过私有IP连接使用云SQL代理https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine#before_you_begin.如果应用程序不想利用云SQL代理,而是选择私有IP连接,则没有任何建议、文档或流程可以确保私有IP连接的安全或建立必要的身份验证。

除了私有IP连接之外,应用程序还应该做些什么才能使其等效于云SQL代理?

Cloud Run不支持该解决方案。为此,您必须在容器中自己运行CloudSQL代理。

我不懂你的语言,但我在围棋中进行了一次测试。以下是如何实现

  • 创建无服务器VPC连接器
  • 将无服务器VPC连接器添加到云运行服务中

现在你可以通过私人IP访问你的数据库,你可以在这里的官方文档中找到

为了在私有模式下强制执行云sql代理,我做了这个

  • 这里是我的dockerfile(Cloud Run文档中的标准文件,我只是自定义了最新的行(
# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.13 as builder
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
#FROM google/cloud-sdk
#
FROM alpine:3
RUN apk add --no-cache ca-certificates
RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O /cloud_sql_proxy && chmod +x /cloud_sql_proxy
# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server
COPY --from=builder /app/start.sh /start.sh
#RUN chmod +x /start.sh
# Run the web service on container startup.
CMD ["/start.sh"]
  • 如您所见,我下载了Cloud-sql代理二进制文件,并调用了一个start.sh文件。内容如下
#!/bin/sh
/cloud_sql_proxy -ip_address_types=PRIVATE --dir=/cloudsql -instances=gbl-imt-homerider-basguillaueb:us-central1:vertx=unix:socket &
/bin/sleep 1
/server

在这个文件中,我在后台启动云SQL代理,等待1秒(云SQL初始化时间(,然后启动我的Go /server。我在/cloudsql/socket中创建了一个unix套接字。由于这一点,您可以获得与Cloud Run嵌入式Cloud SQL连接器完全相同的连接类型。

您也可以在tcp模式下启动云sql代理。

注意:GCP上的云SQL代理文档不是最新的。更喜欢--help以获取云sql代理配置中的更多详细信息

最新更新