将 Apigee Adapter for Envoy 与 Apigee Edge 搭配使用

您正在查看的是 Apigee Edge 文档。
转到 Apigee X 文档
信息

此示例演示了如何将 Apigee Adapter for Envoy 与 Apigee Edge 搭配使用。

前提条件

准备工作:

概览

此示例说明了如何将适用于 Envoy 的 Apigee Adapter 与适用于公有云的 Apigee Edge 搭配使用。API 代理调用流经作为原生应用运行的 Envoy,而 Edge 通过 Apigee Remote Service for Envoy 提供 API 管理服务。

预配 Apigee Edge

在此步骤中,您将使用 Remote Service CLI 将 Apigee Adapter for Envoy 资产预配到 Apigee Edge。预配命令会将 API 代理部署到 Apigee Edge,还会在 Apigee 上设置证书,并生成远程服务将用于从系统安全地连接到 Apigee 的凭据。

  1. 转到 $CLI_HOME 目录:
    cd $CLI_HOME
  2. 创建以下环境变量:这些变量将用作预配脚本的参数:
    export ORG=organization_name
    export ENV=environment_name
    export USER=your_apigee_username
    export PASSWORD=your_apigee_password

    其中:

    变量 说明
    organization_name 您的 Apigee 组织的名称。
    environment_name 您的组织中环境的名称。
    your_apigee_username 您的 Apigee 帐号用户名。通常,用户名是电子邮件地址。
    your_apigee_password 您的 Apigee 密码。
  3. 执行以下命令,在 Apigee Edge 上预配远程服务代理:

    如果您要升级,请使用以下命令预配 Apigee:

    $CLI_HOME/apigee-remote-service-cli provision --legacy --mfa $MFA \
      --username $USER --password $PASSWORD --organization $ORG --environment $ENV > config.yaml

    如果要升级,请使用带有 --force-proxy-install 标志的此命令来预配 Apigee:

    $CLI_HOME/apigee-remote-service-cli provision --legacy --force-proxy-install --mfa $MFA \
      --username $USER --password $PASSWORD --organization $ORG --environment $ENV > config.yaml
  4. 查看 config.yaml 文件的内容。显示的内容应如下所示:
    # Configuration for apigee-remote-service-envoy (platform: SaaS)
    # generated by apigee-remote-service-cli provision on 2020-08-26 09:43:41
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: apigee-remote-service-envoy
      namespace: apigee
    data:
      config.yaml: |
        tenant:
          internal_api: https://istioservices.apigee.net/edgemicro
          remote_service_api: https://my-username-test.apigee.net/remote-service
          org_name: my-org
          env_name: my-env
          key: f7e09c32f827cab87b8ce43842ed8467ffd2c58e6f795241e38fe7b1aec7664
          secret: 1cb5cca00dfb433cb80b32837451fce4bf694633cddbb73d704517e12b35e75
       ...

    密钥值和密钥值用于验证从远程服务代理发送到 Apigee Edge 的请求。

运行 Apigee Remote Service for Envoy 服务

您可以将远程服务作为原生二进制文件或在 Docker 上运行。

以原生方式运行服务

使用预配命令输出的配置文件执行服务二进制文件:

$REMOTE_SERVICE_HOME/apigee-remote-service-envoy -c config_file_path/config.yaml

在 Docker 上运行服务

Docker 映像与版本标记一起发布。对于此安装,请使用最新版本。有三种映像变体可供选择:

变体 映像
Google distroless google/apigee-envoy-adapter:v2.0.0
Ubuntu google/apigee-envoy-adapter:v2.0.0-ubuntu
包含 Boring Crypto 的 Ubuntu google/apigee-envoy-adapter:v2.0.0-boring

例如,如需通过卷装载将暂存映像与 /config.yaml 形式提供的本地 config.yaml 一起运行,请使用以下命令:

docker run -v ./config.yaml:/config.yaml google/apigee-envoy-adapter:v2.0.0

创建示例配置文件

使用 CLI 生成 Envoy 配置文件:

  1. 确保您位于 $ENVOY_HOME 目录中。
  2. 列出可用的配置模板:
    $CLI_HOME/apigee-remote-service-cli samples templates
  3. 执行示例命令。将 TEMPLATE 替换为受支持的 Envoy 模板之一:

    $CLI_HOME/apigee-remote-service-cli samples create --template TEMPLATE -c ./config.yaml

    该命令会创建 ./samples/envoy-config.yaml 文件。

如需了解详情,请参阅示例命令

安装并运行 Envoy 代理

请按照以下步骤安装并运行 Envoy 代理:

  1. 下载 Envoy 二进制文件或进行构建,或使用 Docker
  2. 使用您之前为 httpbin.org 服务生成的示例配置文件运行 Envoy:
    envoy -c ./samples/envoy-config.yaml

测试安装

  1. 配置 API 产品并按照如何获取 API 密钥所述获取 API 密钥。
  2. 在不使用 API 密钥的情况下调用 httpbin 服务:
    curl -i http://localhost:8080/headers -H "HOST:httpbin.org"
    

    该服务现在由 Apigee 管理,由于您未提供 API 密钥,调用将返回以下错误。

    curl -i http://localhost:8080/headers -H "HOST:httpbin.org"
    HTTP/1.1 403 Forbidden
    date: Tue, 12 May 2020 17:51:36 GMT
    server: envoy
    content-length: 0
    x-envoy-upstream-service-time: 11
  3. 使用以下密钥进行 API 调用:
    export APIKEY=YOUR_API_KEY
    curl -i http://localhost:8080/headers \
    -H "HOST:httpbin.org" -H "x-api-key: $APIKEY"

    调用成功时应显示状态为 200,并在响应中返回标头列表。例如:

    curl -i httpbin.default.svc.cluster.local/headers -H "x-api-key: kyOTalNNLMPfOSy6rnVeclmVSL6pA2zS"
    HTTP/1.1 200 OK
    server: envoy
    date: Tue, 12 May 2020 17:55:34 GMT
    content-type: application/json
    content-length: 828
    access-control-allow-origin: *
    access-control-allow-credentials: true
    x-envoy-upstream-service-time: 301
    
    {
      "headers": {
        "Accept": "*/*",
        "Content-Length": "0",
        "Host": "httpbin.default.svc.cluster.local",
        "User-Agent": "curl/7.70.0-DEV",
        "X-Api-Key": "kyOTalNNLMPfOSy6rneclmVSL6pA2zS",
        "X-Apigee-Accesstoken": "",
        "X-Apigee-Api": "httpbin.default.svc.cluster.local",
        "X-Apigee-Apiproducts": "httpbin",
        "X-Apigee-Application": "httpbin",
        "X-Apigee-Authorized": "true",
        "X-Apigee-Clientid": "kyOTalNNLMPfOSy6rVeclmVSL6pA2zS",
        "X-Apigee-Developeremail": "user@example.com",
        "X-Apigee-Environment": "test",
        "X-Apigee-Organization": "my-org",
        "X-Apigee-Scope": "",
        "X-B3-Parentspanid": "1476f9a2329bbdfa",
        "X-B3-Sampled": "0",
        "X-B3-Spanid": "1ad5c19bfb4bc96f",
        "X-B3-Traceid": "6f329a34e8ca07811476f9a2329bbdfa"
      }
    }

后续步骤

通往 httpbin 服务的 API 流量现在由 Apigee 管理。下面是您可以探索和试用的一些功能:

  • 如果您按照如何获取 API 密钥中的说明配置了 API 产品,则配额限制设置为每分钟 5 个请求。请尝试再调用几次 httpbin 服务以触发该配额。配额用尽后,将返回 HTTP 状态 403 错误。
  • 在 Edge 界面中访问 Apigee Analytics。转到分析 > API 指标 > API 代理性能
  • 生成并使用 JWT 令牌以对 API 调用进行身份验证。
  • 使用 CLI 可以进行管理、创建令牌和控制绑定。如需了解 CLI 详情,请参阅参考文档