3.4. 以mbient 模式使用 Kubernetes 网关 API 通过网关直接出口流量
使用 Kubernetes 网关 API 和方法点代理通过出口网关直接出站 HTTP 流量。
先决条件
- 已安装 OpenShift Service Mesh Operator 版本 3.2 或更高版本。
-
已使用 ambient 配置集配置了
Istio和IstioCNI资源。 -
您已创建了
Ztunnel资源。
流程
可选:启用 {k8} Gateway API 自定义资源定义(CRD)。
注意从 Kubernetes 1.28 和 OpenShift Container Platform 4.18 或更早版本 Red Hat OpenShift Service Mesh 开始,Kubernetes 网关 API CRD 默认不可用,且您必须在使用 CRD 前安装 CRD。OpenShift Container Platform 4.19 及更新的版本会默认安装 CRD。
运行以下命令,创建一个名为
egress-gateway的命名空间:oc create namespace egress-gateway
$ oc create namespace egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,将 ambient 模式标签应用到命名空间:
oc label namespace egress-gateway istio.io/dataplane-mode=ambient
$ oc label namespace egress-gateway istio.io/dataplane-mode=ambientCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建名为
egress-se.yaml的 YAML 文件,该文件定义ServiceEntry。Copy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令来应用 YAML 文件:
oc apply -f egress-se.yaml
$ oc apply -f egress-se.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建名为
waypoint.yaml的 YAML 文件,该文件在egress-gateway命名空间中创建 waypoint 代理,如下例所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令来应用 YAML 文件:
oc apply -f waypoint.yaml
$ oc apply -f waypoint.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
作为创建 waypoint.yaml YAML 文件的替代方法,您还可以运行以下命令来设置 waypoint 代理:
istioctl waypoint apply --enroll-namespace --name waypoint --namespace egress-gateway
$ istioctl waypoint apply --enroll-namespace --name waypoint --namespace egress-gateway
当您使用 --enroll-namespace 选项时,egress-gateway 命名空间中的所有服务(包括 ServiceEntries)将通过 waypoint 路由其流量。
验证
运行以下命令,验证网关配置的状态:
oc get gateways.gateway.networking.k8s.io waypoint -n egress-gateway
$ oc get gateways.gateway.networking.k8s.io waypoint -n egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow 当配置成功时,PROGRAM
MED列显示True,如下例所示:NAME CLASS ADDRESS PROGRAMMED AGE waypoint istio-waypoint 172.30.227.148 True 9s
NAME CLASS ADDRESS PROGRAMMED AGE waypoint istio-waypoint 172.30.227.148 True 9sCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,在
egress-gateway命名空间中创建curlpod:oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- sh
$ oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- shCopy to Clipboard Copied! Toggle word wrap Toggle overflow 通过使用
curl客户端,运行以下命令来验证您可以通过出口网关访问httpbin.org:curl -v http://httpbin.org/get
$ curl -v http://httpbin.org/getCopy to Clipboard Copied! Toggle word wrap Toggle overflow 输出显示了
httpbin.org服务的响应,该服务表示通过配置的网关路由出口流量。ztunnel 日志应该显示通过 waypoint 路由的流量。终端应该显示类似以下输出的信息:2025-10-24T08:08:35.242159Z info access connection complete src.addr=[fd01:0:0:5::b0]:56288 src.workload="test-pod" src.namespace="egress-gateway" src.identity="spiffe://cluster.local/ns/egress-gateway/sa/default" dst.addr=[fd01:0:0:5::af]:15008 dst.hbone_addr=[2001:2::2]:80 dst.service="httpbin.org" dst.workload="waypoint-5b668759d5-vrnx8" dst.namespace="egress-gateway" dst.identity="spiffe://cluster.local/ns/egress-gateway/sa/waypoint" direction="outbound" bytes_sent=78 bytes_recv=540 duration="957ms"
2025-10-24T08:08:35.242159Z info access connection complete src.addr=[fd01:0:0:5::b0]:56288 src.workload="test-pod" src.namespace="egress-gateway" src.identity="spiffe://cluster.local/ns/egress-gateway/sa/default" dst.addr=[fd01:0:0:5::af]:15008 dst.hbone_addr=[2001:2::2]:80 dst.service="httpbin.org" dst.workload="waypoint-5b668759d5-vrnx8" dst.namespace="egress-gateway" dst.identity="spiffe://cluster.local/ns/egress-gateway/sa/waypoint" direction="outbound" bytes_sent=78 bytes_recv=540 duration="957ms"Copy to Clipboard Copied! Toggle word wrap Toggle overflow