【问题标题】:Connect to Memorystore from Cloud Run从 Cloud Run 连接到 Memorystore
【发布时间】:2019-05-20 17:06:37
【问题描述】:

我想在使用 Cloud Memorystore 作为缓存的 Google Cloud Run 上运行一项服务。

我在与 Cloud Run 相同的区域创建了一个 Memorystore 实例,并使用示例代码进行连接:https://github.com/GoogleCloudPlatform/golang-samples/blob/master/memorystore/redis/main.go 这不起作用。

接下来,我创建了一个无服务器 VPC 访问连接器,但没有帮助。我在没有 GKE 集群的情况下使用 Cloud Run,因此无法更改任何配置。

有没有办法从 Cloud Run 连接到 Memorystore?

【问题讨论】:

    标签: google-cloud-platform google-cloud-run


    【解决方案1】:

    要将 Cloud Run(完全托管)连接到 Memorystore,您需要使用称为“无服务器 VPC 访问”或“VPC 连接器”的机制。

    截至 2020 年 5 月,Cloud Run(全托管)为无服务器 VPC 访问提供 Beta 版支持。请参阅Connecting to a VPC Network 了解更多信息。

    使用此 Beta 的替代方法包括:

    【讨论】:

    • 此 VPC 连接器支持功能是否有任何更新? (2019 年 10 月)
    • 还没有,有消息我会更新的。
    • 那么就没有办法在 Cloud Run 上使用 Memorystore 了吗? ://
    • @Grayside 有什么粗略的预计到达时间,我们可以期待这种支持可用吗?
    • 这只是在文档之前达到了测试版。文档出来后,我会更新答案。
    【解决方案2】:

    在等待serverless VPC connectors on Cloud Run 的同时 - Google 昨天表示将在近期发布公告 - 您可以通过 GCE 使用 SSH 隧道从 Cloud Run 连接到 Memorystore。

    基本方法如下。

    首先,在 GCE 上创建一个转发器实例

    gcloud compute instances create vpc-forwarder --machine-type=f1-micro --zone=us-central1-a
    

    不要忘记在您的防火墙策略中打开端口 22(默认情况下它是打开的)。

    然后通过您的 Dockerfile 安装 gcloud CLI

    这是一个 Rails 应用程序的示例。 Dockerfile 使用入口点的脚本。

    # Use the official lightweight Ruby image.
    # https://hub.docker.com/_/ruby
    FROM ruby:2.5.5
    
    # Install gcloud
    RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
    RUN mkdir -p /usr/local/gcloud \
      && tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz \
      && /usr/local/gcloud/google-cloud-sdk/install.sh
    ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin
    
    # Generate SSH key to be used by the SSH tunnel (see entrypoint.sh)
    RUN mkdir -p /home/.ssh && ssh-keygen -b 2048 -t rsa -f /home/.ssh/google_compute_engine -q -N ""
    
    # Install bundler
    RUN gem update --system
    RUN gem install bundler
    
    # Install production dependencies.
    WORKDIR /usr/src/app
    COPY Gemfile Gemfile.lock ./
    ENV BUNDLE_FROZEN=true
    RUN bundle install
    
    # Copy local code to the container image.
    COPY . ./
    
    # Run the web service on container startup.
    CMD ["bash", "entrypoint.sh"]
    

    最后在 entrypoint.sh 脚本中打开一个到 Redis 的 SSH 隧道

    
    # !/bin/bash
    
    # Memorystore config
    MEMORYSTORE_IP=10.0.0.5
    MEMORYSTORE_REMOTE_PORT=6379
    MEMORYSTORE_LOCAL_PORT=6379
    
    # Forwarder config
    FORWARDER_ID=vpc-forwarder
    FORWARDER_ZONE=us-central1-a
    
    # Start tunnel to Redis Memorystore in background
    gcloud compute ssh \
      --zone=${FORWARDER_ZONE} \
      --ssh-flag="-N -L ${MEMORYSTORE_LOCAL_PORT}:${MEMORYSTORE_IP}:${MEMORYSTORE_REMOTE_PORT}" \
      ${FORWARDER_ID} &
    
    # Run migrations and start Puma
    bundle exec rake db:migrate && bundle exec puma -p 8080
    

    通过上述解决方案,您的应用程序将可以在 localhost:6379 上使用 Memorystore。

    有一些注意事项

    1. 这种方法需要在您的 Cloud Run 服务上配置的服务帐号具有roles/compute.instanceAdmin 角色,这非常强大。
    2. 将 SSH 密钥备份到映像中以加快容器启动时间。这并不理想。
    3. 如果您的转发器崩溃,则不会进行故障转移。

    我在blog post 中编写了一个更长、更详细的方法,它提高了整体安全性并增加了故障转移功能。该解决方案使用纯 SSH 而不是 gcloud CLI。

    【讨论】:

      【解决方案3】:

      如果您需要 VPC 中的某些内容,也可以启动 Redis on Compute Engine

      它比 Redis Cloud 成本更高(尤其是对于集群而言)——但如果您必须将数据保存在 VPC 中,这是一种临时解决方案。

      【讨论】:

      • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。请参阅:How to anwser
      • 由于 Google Cloud Run 容器(还)不能连接到 VPC,我看不出这在这种情况下会有什么帮助。
      猜你喜欢
      • 2020-05-27
      • 2021-12-28
      • 2020-02-23
      • 1970-01-01
      • 2020-06-15
      • 2019-10-28
      • 2019-09-05
      • 2019-07-10
      • 2020-07-01
      相关资源
      最近更新 更多