使用 agentgateway

agentgateway 是一个数据平面代理, 可用作 Envoy 的替代品。它专为 AI 代理和模型上下文协议 (MCP) 流量而构建, 同时还支持通用 L7 路由。启用 agentgateway 后,Istio 可以对其进行编程, 以代替 Envoy 在 ambient 网格中扮演两个角色:

  • 作为入口网关,处理进入网格的南北流量,以及
  • 作为一个 waypoint 代理,处理一组工作负载的东西向 L7 处理。

本指南介绍了集成的工作原理、支持的 API, 以及如何安装 Istio 并针对每种角色配置 agentgateway。

集成是如何运作的

Istiod 仅通过 Kubernetes Gateway API 资源配置 agentgateway, 并通过 xDS 将其传递给代理。代理是与 Envoy 不同的 data plane 实现: 当 Gateway 选择一个 agentgateway GatewayClass 时, Istiod 为其配置和管理 agentgateway DeploymentService, 就像管理 Istio 基于 Envoy 的那样网关。

Enabling agentgateway registers two GatewayClass resources: 启用 agentgateway 会注册两个 GatewayClass 资源:

GatewayClass控制器角色
istio-agentgatewayistio.io/agentgateway-controller入口网关
istio-agentgateway-waypointistio.io/agentgateway-waypoint-controllerwaypoint 代理

由于数据平面是通过 gatewayClassName 字段针对每个 Gateway 选择的, 因此 agentgateway 和 Istio 默认的基于 Envoy 的网关和 waypoint 可以在同一集群中共存。 您只需引用上述类之一即可为特定网关或 waypoint 选择 agentgateway。

受支持与不受支持的配置

Istio 支持 agentgateway 的以下 Gateway API 资源:

  • Gateway(使用 istio-agentgatewayistio-agentgateway-waypoint 类)
  • HTTPRouteGRPCRouteTCPRouteTLSRoute
  • InferencePool,来自 Gateway API 推理扩展,用于路由到 AI 推理工作负载

开始之前

请注意,Kubernetes Gateway API CRD 不会默认安装在大多数 Kubernetes 集群上, 因此请确保在使用 Gateway API 之前已安装好这些 CRD:

$ kubectl get crd gateways.gateway.networking.k8s.io &> /dev/null || \
  kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.6.0/standard-install.yaml

安装启用了 agentgateway 的 Istio

agentgateway 支持在 istiod 上的 PILOT_ENABLE_AGENTGATEWAY 功能标志后面进行门控, 默认情况下处于禁用状态。使用 ambient 配置文件并启用该标志来安装 Istio。 需要 ambient 配置文件,以便也注册 waypoint GatewayClass

$ istioctl install --set profile=ambient --set values.pilot.env.PILOT_ENABLE_AGENTGATEWAY=true -y

确认两个 agentgateway GatewayClass 资源均已注册:

$ kubectl get gatewayclass istio-agentgateway istio-agentgateway-waypoint
NAME                          CONTROLLER                                  ACCEPTED   AGE
istio-agentgateway            istio.io/agentgateway-controller            True       30s
istio-agentgateway-waypoint   istio.io/agentgateway-waypoint-controller   True       30s

部署示例应用程序

部署 Bookinfo 示例应用程序,本指南中的示例将使用该应用程序:

Zip
$ kubectl apply -f @samples/bookinfo/platform/kube/bookinfo.yaml@

将 agentgateway 配置为入口网关

要将 agentgateway 用作入口网关,请创建一个引用 istio-agentgateway 类的 Gateway。 Istiod 自动配置和管理相应的 agentgateway 部署。

$ kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: bookinfo-gateway
  annotations:
    networking.istio.io/service-type: ClusterIP
spec:
  gatewayClassName: istio-agentgateway
  listeners:
  - name: http
    port: 80
    protocol: HTTP
    allowedRoutes:
      namespaces:
        from: Same
EOF

gatewayClassName: istio-agentgateway 字段用于选择 agentgateway 数据平面而不是 Envoy。 默认情况下,Istio 为网关创建一个 LoadBalancer 服务; networking.istio.io/service-type: ClusterIP 注解请求 ClusterIP 服务, 以便可以使用本指南中的 kubectl port-forward 访问网关。

附加一个 HTTPRoute 以通过网关公开 productpage 服务:

$ kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: bookinfo
spec:
  parentRefs:
  - name: bookinfo-gateway
  rules:
  - matches:
    - path:
        type: Exact
        value: /productpage
    - path:
        type: PathPrefix
        value: /static
    - path:
        type: Exact
        value: /login
    - path:
        type: PathPrefix
        value: /api/v1/products
    backendRefs:
    - name: productpage
      port: 9080
EOF

确认网关已配置并编程。CLASS 列显示 agentgateway 类:

$ kubectl get gateway bookinfo-gateway
NAME               CLASS                ADDRESS                                      PROGRAMMED   AGE
bookinfo-gateway   istio-agentgateway   bookinfo-gateway.default.svc.cluster.local   True         30s

您现在可以通过 agentgateway 入口网关访问应用程序。 将本地端口转发到网关服务并在浏览器中打开 http://localhost:8080/productpage

$ kubectl port-forward svc/bookinfo-gateway 8080:80

将 agentgateway 配置为 waypoint

waypoint 代理将 L7 处理添加到 Ambient 网格中的一组工作负载。 要使用 agentgateway 担任此角色,请部署引用 istio-agentgateway-waypoint 类的 Gateway

首先,确认命名空间已在 Ambient 数据平面中注册:

$ kubectl label namespace default istio.io/dataplane-mode=ambient
namespace/default labeled

部署 waypoint。与所有 waypoint 一样,它必须使用 HBONE 协议在端口 15008 上定义一个名为 mesh 的监听器;与 Envoy waypoint 的唯一区别是 gatewayClassName

$ kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: agentgateway-waypoint
  labels:
    istio.io/waypoint-for: service
spec:
  gatewayClassName: istio-agentgateway-waypoint
  listeners:
  - name: mesh
    port: 15008
    protocol: HBONE
EOF

确认 waypoint 已配置:

$ kubectl get gateway agentgateway-waypoint
NAME                    CLASS                         ADDRESS        PROGRAMMED   AGE
agentgateway-waypoint   istio-agentgateway-waypoint   10.96.15.112   True         30s

通过添加包含 waypoint 名称的 istio.io/use-waypoint 标签来注册服务以使用 waypoint。 例如,要通过 agentgateway waypoint 发送发往 reviews 服务的流量:

$ kubectl label service reviews istio.io/use-waypoint=agentgateway-waypoint
service/reviews labeled

从 Ambient 网格中的工作负载到 reviews 服务的请求现在通过 agentgateway waypoint 进行路由以进行 L7 处理。 要了解有关注册命名空间、Service 和 Pod 以及 waypoint 如何处理不同流量类型的更多信息, 请参阅配置 waypoint 代理

若要在 waypoint 处应用 L7 路由策略,请将 Gateway API 路由关联到 Service, 并使用 kindServiceparentRef。例如, 以下 HTTPRoutereviews 服务的 90% 流量发送至 reviews-v1,其余 10% 发送至 reviews-v2

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: reviews
spec:
  parentRefs:
  - group: ""
    kind: Service
    name: reviews
    port: 9080
  rules:
  - backendRefs:
    - name: reviews-v1
      port: 9080
      weight: 90
    - name: reviews-v2
      port: 9080
      weight: 10

清理

删除入口网关及其路由:

$ kubectl delete httproute bookinfo
$ kubectl delete gateway bookinfo-gateway

删除 waypoint 并取消注册 reviews 服务:

$ kubectl label service reviews istio.io/use-waypoint-
$ kubectl delete gateway agentgateway-waypoint

删除示例应用程序和 Ambient 标签:

Zip
$ kubectl delete -f @samples/bookinfo/platform/kube/bookinfo.yaml@
$ kubectl label namespace default istio.io/dataplane-mode-

卸载 Istio:

$ istioctl uninstall --purge -y
$ kubectl delete namespace istio-system

删除 Kubernetes Gateway API CRD:

$ kubectl delete -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.6.0/standard-install.yaml
这些信息有用吗?
您是否有更多建议和改进意见?

感谢您的反馈!