🤔 Introducing APISIX AI Gateway – Built for LLMs and AI workloads. Learn More

proxy-mirror

描述

proxy-mirror 插件将传入流量复制到 APISIX 并将其转发到指定的上游,而不会中断常规服务。您可以将插件配置为镜像所有流量或仅镜像一部分流量。该机制有利于一些用例,包括故障排除、安全检查、分析等。

请注意,APISIX 会忽略接收镜像流量的上游主机的任何响应。

参数

名称类型必选项默认值有效值描述
hoststring将镜像流量转发到的主机的地址。该地址应包含方案但不包含路径,例如 http://127.0.0.1:8081
pathstring将镜像流量转发到的主机的路径。如果未指定,则默认为路由的当前 URI 路径。如果插件正在镜像 gRPC 流量,则不适用。
path_concat_modestringreplace[“replace”, “prefix”]指定 path 时的连接模式。设置为 replace 时,配置的 path 将直接用作将镜像流量转发到的主机的路径。设置为 prefix 时,转发到的路径将是配置的 path,附加路由的请求 URI 路径。如果插件正在镜像 gRPC 流量,则不适用。
sample_rationumber1[0.00001, 1]将被镜像的请求的比例。默认情况下,所有流量都会被镜像。

静态配置

默认情况下,插件的超时值在默认配置中预先配置。

要自定义这些值,请将相应的配置添加到 config.yaml。例如:

plugin_attr:
  proxy-mirror:
    timeout:
      connect: 60s
      read: 60s
      send: 60s

重新加载 APISIX 以使更改生效。

示例

以下示例演示了如何为不同场景配置 proxy-mirror

NOTE

您可以这样从 config.yaml 中获取 admin_key 并存入环境变量:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

镜像部分流量

以下示例演示了如何配置 proxy-mirror 以将 50% 的流量镜像到路由并将其转发到另一个上游服务。

启动一个示例 NGINX 服务器以接收镜像流量:

docker run -p 8081:80 --name nginx nginx

您应该在终端会话中看到 NGINX 访问日志和错误日志。

打开一个新的终端会话并使用 proxy-mirror 创建路由:

admin-api

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
  -H "X-API-KEY: ${admin_key}" \
  -d '{
    "id": "traffic-mirror-route",
    "uri": "/get",
    "plugins": {
      "proxy-mirror": {
        "host": "http://127.0.0.1:8081",
        "sample_ratio": 0.5
      }
    },
    "upstream": {
      "nodes": {
        "httpbin.org": 1
      },
      "type": "roundrobin"
    }
  }'

adc

adc.yaml
services:
  - name: proxy-mirror-service
    routes:
      - name: traffic-mirror-route
        uris:
          - /get
        plugins:
          proxy-mirror:
            host: "http://127.0.0.1:8081"
            sample_ratio: 0.5
    upstream:
      type: roundrobin
      nodes:
        - host: httpbin.org
          port: 80
          weight: 1

将配置同步到网关:

adc sync -f adc.yaml

aic

gateway-api

proxy-mirror-ic.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  type: ExternalName
  externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
  namespace: aic
  name: proxy-mirror-plugin-config
spec:
  plugins:
    - name: proxy-mirror
      config:
        host: "http://nginx.aic.svc"
        sample_ratio: 0.5
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  namespace: aic
  name: traffic-mirror-route
spec:
  parentRefs:
    - name: apisix
  rules:
    - matches:
        - path:
            type: Exact
            value: /get
      filters:
        - type: ExtensionRef
          extensionRef:
            group: apisix.apache.org
            kind: PluginConfig
            name: proxy-mirror-plugin-config
      backendRefs:
        - name: httpbin-external-domain
          port: 80

apisix-crd

proxy-mirror-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
  namespace: aic
  name: httpbin-external-domain
spec:
  ingressClassName: apisix
  externalNodes:
  - type: Domain
    name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  namespace: aic
  name: traffic-mirror-route
spec:
  ingressClassName: apisix
  http:
    - name: traffic-mirror-route
      match:
        paths:
          - /get
      upstreams:
      - name: httpbin-external-domain
      plugins:
      - name: proxy-mirror
        enable: true
        config:
          host: "http://nginx.aic.svc"
          sample_ratio: 0.5

将配置应用到集群:

kubectl apply -f proxy-mirror-ic.yaml

host:配置将镜像流量转发到的方案和主机地址。

sample_ratio:将采样比率配置为 0.5,以镜像 50% 的流量。

发送生成几个请求到路由:

curl -i "http://127.0.0.1:9080/get"

您应该会收到所有请求的 HTTP/1.1 200 OK 响应。

导航回 NGINX 终端会话,您应该会看到一些访问日志条目,大约是生成的请求数量的一半:

172.17.0.1 - - [29/Jan/2024:23:11:01 +0000] "GET /get HTTP/1.1" 404 153 "-" "curl/7.64.1" "-"

这表明 APISIX 已将请求镜像到 NGINX 服务器。此处,HTTP 响应状态为 404,因为示例 NGINX 服务器未实现路由。

配置镜像超时

以下示例演示了如何更新插件的默认连接、读取和发送超时。当将流量镜像到非常慢的后端服务时,这可能很有用。

由于请求镜像是作为子请求实现的,子请求中的过度延迟可能导致原始请求被阻止。默认情况下,连接、读取和发送超时设置为 60 秒。要更新这些值,您可以在配置文件的 plugin_attr 部分中配置它们,如下所示:

conf/config.yaml
plugin_attr:
  proxy-mirror:
    timeout:
      connect: 2000ms
      read: 2000ms
      send: 2000ms

重新加载 APISIX 以使更改生效。