GCP推荐使用云SQL Proxy over Private IP连接https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine#before_you_begin。如果应用程序不想利用Cloud SQL Proxy,但选择了专用IP连接,则没有任何建议、文档或流程来确保专用IP连接的安全或构建必要的身份验证。
除了内网IP连接之外,应用程序还应该做些什么,才能使其等同于Cloud SQL Proxy?
发布于 2020-07-24 18:23:00
Cloud Run本身并不支持该解决方案。为此,您必须在容器中自行运行Cloud SQL proxy。
我不懂你的语言,但我在Go中做了一个测试。下面是如何实现这一点的
使用Create a serverless VPC Connector
现在您可以通过内网IP访问您的数据库,您可以在官方文档here中找到这些内容
为了在私有模式下强制使用云sql代理,我这样做了
# 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"]
正如您所看到的,我下载了
start.sh
文件。此处是content#!/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
在这个文件中,我在后台启动Cloud SQL proxy,等待1秒( cloud SQL初始化时间),然后启动我的Go /server
。我在/cloudsql/socket
中创建了一个unix套接字。多亏了这一点,您拥有了与Cloud Run embedded Cloud SQL连接器完全相同的连接类型。
您也可以在tcp模式下启动云sql代理。
注意: GCP上的Cloud SQL proxy文档不是最新的。有关云sql代理配置的更多详细信息,请参阅--help
https://stackoverflow.com/questions/63047768
复制相似问题