用于计算引擎的 GCP API 网关

问题描述

我在 api 网关营销页面上找到了以下代码段。

enter image description here

所以我正在为我在计算引擎上运行的应用程序设置 API 网关。由于找不到关于如何在 API 引擎上配置计算引擎的任何文档,我使用内部 DNS 创建了以下配置。enter link description here

swagger: "2.0"
info:
  title: API Endpoints
  description: API Endpoints
  version: 1.0.1
schemes:
  - https
produces:
  - application/json
paths:
  /indexes:
    get:
      summary: Return Search Indexes
      operationId: searchIndexes
      x-google-backend:
        address: http://my_internal_dns_for_compute_engine.c.myproject.internal/indexes
        path_translation: APPEND_PATH_TO_ADDRESS
      responses:
        "200":
          description: A successful response
          schema:
            type: string
        "403":
          description: Failed to authenticate

当我使用 gcloud 部署配置时,出现以下错误

等待为 API 创建 API 配置 [my-api-config-v6] [my-api]...失败。
错误:(gcloud.beta.api-gateway.api-configs.create)等待服务 配置创建:后端 URL “http://my_internal_dns_for_compute_engine.c.myproject.internal/indexes”是 禁止:无法将请求路由到内部地址。

所以看起来不支持内部 DNS(显然)。

我的计算引擎实例只能通过 VPC 网络访问。如何将我的 api 网关与 VPC 网络连接以及如何通过它访问我的计算引擎?

解决方法

今天,你无法做到这一点。您的 API 网关是一项无服务器服务,您无法在其上插入 serverless VPC connector。我已经与 Google 讨论过这个问题(因为它与 Cloud Scheduler、Cloud Task 和 PubSub 推送订阅存在同样的问题),很快就会发生一些事情。敬请期待!!

无论如何,现在要解决您的问题,您有 2 个解决方案(至少,一个确定,一个要测试)

  • 第一个(肯定的)解决方案是拥有一个“代理”。另一个具有公共访问权限的计算引擎,或具有无服务器 VPC 连接器的 Cloud Function/Cloud Run/App Engine 服务。 Cloud Function/Cloud Run/App Engine 更好,因为您可以使用 IAM 保护访问(非公开访问)
  • 第二种(用于测试)解决方案是部署 Cloud Endpoint,I mean ESPv2 on Cloud Run 而不是使用 API 网关。实际上,API Gateway 目前是 Cloud Run 上 ESPv2 的托管解决方案。如果您在 Cloud Run 上插入无服务器 VPC 连接器,它应该可以工作。我写了 an article on the ESPv2 & Cloud Run 用法。 如果我有时间,我将使用无服务器 VPC 连接器对其进行测试并通知您。否则,请尝试一下。

编辑 1

我已经测试了第二个解决方案,它使用内部 IP(以及部署了 ESPv2 的 Cloud Run Endpoint 服务上的无服务器 VPC 连接器),而不是使用内部 DNS 名称。