2

我正在寻找一个关于如何将 Google Cloud API Gateway 与托管在 GKE 中的微服务/API 一起使用的工作示例/教程。例如,当我尝试创建 API 网关并将其指向 GKE 上的现有 API 时,我收到以下错误:

后端 URL“http://35.xxx.xxx.xxx/legalentities”被禁止:无法通过 IP 地址路由请求。

4

2 回答 2

2

GKE 默认为入口控制器或负载均衡器生成 IP 地址,API 网关不允许将 IP 地址作为 x-google-backend 的主机名。这是一个问题,希望它会得到解决 API Gateway 推出 beta 版。

我面临同样的情况。这就是我解决它的方法(使用 nip.io):

/products/getoptions:
    get:
      summary: get product options
      operationId: getProductOptions
      x-google-backend:
        address: https://35.xxx.xxx.xxx.nip.io/api/productservice
        path_translation: APPEND_PATH_TO_ADDRESS
      parameters:
        - name: x-access-token
          in: header
          description: Access Token
          required: true
          type: string
        - name: x-refresh-token
          in: header
          description: Refresh Token
          required: true
          type: string
      responses:
        '200':
          description: OK
          schema:
            type: object
于 2020-11-19T10:15:20.883 回答
0

在撰写本文时,API Gateway 仍处于Beta阶段,因此它可能无法完全发挥作用并且文档可能很少。目前,API Gateway 的缺点之一是您的 GKE 环境会生成面向 Internet 的服务,该服务只能通过 IP 地址访问。但是,您需要在您的内部放置一个 FQDN openapi.yaml(见下文)。这就是你的错误可能来自的地方。

缓解此问题的两种选择:

  • 在 GKE IP 地址前使用负载平衡器。这样您就可以使用负载均衡器的 FQDN。但是,我不确定身份验证在此设置中是否仍然有效,并且用户可能能够绕过 API 网关。

  • 在托管的 Cloud Run 中部署面向 Internet 的应用程序。这将始终产生一个 FQDN。您只需填写x-google-backend address. 您需要为 Cloud Run 配置无服务器 VPC 访问权限,以让应用程序与您的 GKE 集群通信。

    swagger: '2.0'
    info:
      title: API_ID optional-string
      description: Sample API on API Gateway with a Google Cloud Functions backend
      version: 1.0.0
    schemes:
      - https
    produces:
      - application/json
    paths:
      /hello:
        get:
          summary: Greet a user
          operationId: hello
          x-google-backend:
            address: [FQDN HERE]
          responses:
            '200':
              description: A successful response
              schema:
                type: string
    
于 2020-11-18T10:32:24.717 回答