在 ambient 模式下通过 Kubernetes Gateway API 暴露服务
在 Istio ambient 模式下,Kubernetes Gateway API 是配置入口流量路由的推荐方法。您可以创建 Gateway 和 HTTPRoute 资源来部署 gateway,使外部流量能够访问 mesh 内部服务。
用于 Layer 7 路由的 waypoint proxy
要强制执行 Layer 7 (L7) 路由策略(包括基于路径的路由和标头匹配),请在包含目标服务的 namespace 中部署 waypoint proxy。waypoint proxy 负责处理 L7 流量,并应用通过 HTTPRoute 和 GRPCRoute 资源定义的路由规则。
在 ambient 模式下,VirtualService 资源的兼容性有限,不应与 Gateway API 配置结合使用。在 ambient 模式下,请使用 Kubernetes Gateway API 资源作为流量路由的标准方法。
前提条件
- 已安装 Alauda Service Mesh v2 Operator。
- 已使用 ambient profile 配置
Istio和IstioCNI资源。 - 已创建
Ztunnel资源。 - 已安装 Gateway API CRD。
- 确认 Linux kernel 兼容性。
- 您的 Kubernetes 集群支持外部负载均衡器(即类型为
LoadBalancer的 Service)。
操作步骤
-
创建名为
httpbin的 namespace: -
向
httpbinnamespace 添加istio-discovery=enabled标签: -
通过应用 dataplane mode 标签为该 namespace 启用 ambient 模式:
-
部署
httpbin示例服务: -
将
istio和istio-waypointgateway class overlay 添加到Istio资源,以便将 gateway 和 waypoint pod 准入Restrictedhttpbinnamespace:有关详细信息,请参阅 Pod Security Admission。
-
创建名为
httpbin-waypoint.yaml的文件以定义 waypoint proxy。此Gateway资源使用istio-waypointgateway class 处理 namespace 中服务的 L7 流量。istio.io/waypoint-for: service标签表示此 waypoint 处理服务流量。标签值决定所处理流量的类型。有关详细信息,请参阅 Waypoint traffic types(Istio 文档)。- 指定
istio-waypointgateway class,该 class 部署 waypoint proxy,而不是标准 ingress gateway。
-
应用 waypoint proxy 配置:
-
为
httpbinservice 添加标签,使入口流量通过 waypoint proxy:NOTEistio.io/ingress-use-waypoint=true标签确保从 ingress gateway 到达的流量经过 waypoint proxy,从而在流量到达httpbinservice 之前,强制执行在 waypoint 上配置的 L7 策略。 -
通过为 namespace 添加标签,将 namespace 中的所有服务与 waypoint proxy 关联:
-
创建名为
httpbin-gw.yaml的文件,该文件定义 KubernetesGateway资源。此配置使 gateway proxy 接受端口 80 上、面向主机httpbin.example.com的 HTTP 流量。- 指定 gateway 的
Service类型;默认为LoadBalancer。 - 指定客户端通过此端口访问 mesh 服务时使用的虚拟主机名。
- 指定 gateway 的
-
应用 gateway 配置:
-
创建名为
httpbin-ingress-hr.yaml的文件,该文件为 ingress gateway 定义HTTPRoute资源。此资源指定如何将流量从 gateway proxy 路由到httpbinservice。- 将此
HTTPRoute绑定到上一步创建的 KubernetesGateway。 - 将匹配的流量路由到端口 8000 上的
httpbinservice。
- 将此
-
应用 ingress HTTPRoute:
-
创建名为
httpbin-waypoint-hr.yaml的文件,该文件为 waypoint proxy 定义HTTPRoute资源。此资源配置由 waypoint 强制执行的基于路径的路由规则。- 将此
HTTPRoute绑定到httpbinservice。结合istio.io/ingress-use-waypoint=true标签,这会配置 waypoint proxy 为发往该服务的流量强制执行的 L7 路由规则。 - 将匹配的流量转发到端口 8000 上的
httpbinservice。
- 将此
-
应用 waypoint HTTPRoute:
NOTE在此配置中,由于 service 上的
istio.io/ingress-use-waypoint=true标签,来自 ingress gateway 的流量会经过 waypoint proxy。随后,waypointHTTPRoute会在流量到达httpbinservice 之前应用基于路径的路由策略。 -
等待 waypoint proxy 就绪:
-
可选:将 gateway 部署到 Infra Nodes:
单击以展开
前提条件Alauda Container Platform 4.2.0 或更高版本,或者将 Gateway API CRD 升级到最新版本。
a. 在计划部署 Gateway 的同一 namespace 中创建名为
asm-kube-gateway-options的 ConfigMap:- 指定 configmap 的名称。
- 指定与 gateway 相同的 configmap namespace。
- 设置节点选择器和容忍度,以便将 gateway pod 调度到 Infra Nodes。
b. 通过添加
infrastructure.parametersRef字段,在 Gateway 资源中引用 ConfigMap:- 指定 gateway 的名称。
- 指定 gateway 的 namespace。
验证
-
为
curlclient 创建 namespace: -
部署
curlclient: -
向
curlnamespace 添加istio-discovery=enabled标签: -
为
curlnamespace 启用 ambient 模式: -
将
curlpod 的名称存储在变量中: -
从
curlclient 向httpbinapplication 的/headersendpoint 发送请求,并通过 ingress gatewayService。将Hostheader 设置为httpbin.example.com,以匹配 KubernetesGateway和HTTPRoute资源中指定的主机:响应应返回
HTTP/1.1 200 OK状态,表示请求已成功处理。示例输出
-
向 waypoint
HTTPRoute中 URI 前缀不匹配的 endpoint 发送请求:响应返回
HTTP/1.1 404 Not Found,这是预期结果,因为/get路径没有在 waypointHTTPRoute中定义对应的前缀匹配。示例输出
-
将
Service类型更改为默认的LoadBalancer,使 gateway proxy 暴露给集群外部的流量: -
使用 gateway
Service的外部主机名或 IP 地址,确认可从集群外部访问httpbinservice。根据您的集群环境设置INGRESS_HOST变量。a. 设置
INGRESS_HOST变量:b. 设置
INGRESS_PORT变量:c. 使用 gateway host 向
httpbinservice 发送curl请求:INFO如果
$INGRESS_HOST是 IPv6 地址,构造 URL 时请将其括在方括号中。例如: -
确认响应包含
HTTP/1.1 200 OK状态,表示请求成功。示例输出
清理
移除在此操作步骤中创建的资源: