3.4. 以mbient 模式使用 Kubernetes 网关 API 通过网关直接出口流量


使用 Kubernetes 网关 API 和方法点代理通过出口网关直接出站 HTTP 流量。

先决条件

  • 已安装 OpenShift Service Mesh Operator 版本 3.2 或更高版本。
  • 已使用 ambient 配置集配置了 IstioIstioCNI 资源。
  • 您已创建了 Ztunnel 资源。

流程

  1. 可选:启用 {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。

  2. 运行以下命令,创建一个名为 egress-gateway 的命名空间:

    $ oc create namespace egress-gateway
    Copy to Clipboard Toggle word wrap
  3. 运行以下命令,将 ambient 模式标签应用到命名空间:

    $ oc label namespace egress-gateway istio.io/dataplane-mode=ambient
    Copy to Clipboard Toggle word wrap
  4. 创建名为 egress-se.yaml 的 YAML 文件,该文件定义 ServiceEntry

    apiVersion: networking.istio.io/v1
    kind: ServiceEntry
    metadata:
      name: httpbin-ext
      namespace: egress-gateway
      labels:
        istio.io/use-waypoint: waypoint
    spec:
      hosts:
      - httpbin.org
      ports:
      - number: 80
        name: http
        protocol: HTTP
      resolution: DNS
    Copy to Clipboard Toggle word wrap
    1. 运行以下命令来应用 YAML 文件:

      $ oc apply -f egress-se.yaml
      Copy to Clipboard Toggle word wrap
    2. 创建名为 waypoint.yaml 的 YAML 文件,该文件在 egress-gateway 命名空间中创建 waypoint 代理,如下例所示:

      apiVersion: gateway.networking.k8s.io/v1
      kind: Gateway
      metadata:
        name: waypoint
        namespace: egress-gateway
        labels:
          istio.io/gateway-for: service
      spec:
        gatewayClassName: istio-waypoint
        listeners:
        - name: mesh
          port: 15008
          protocol: HBONE
      Copy to Clipboard Toggle word wrap
    3. 运行以下命令来应用 YAML 文件:

      $ oc apply -f waypoint.yaml
      Copy to Clipboard Toggle word wrap
注意

作为创建 waypoint.yaml YAML 文件的替代方法,您还可以运行以下命令来设置 waypoint 代理:

$ istioctl waypoint apply --enroll-namespace --name waypoint --namespace egress-gateway
Copy to Clipboard Toggle word wrap

当您使用 --enroll-namespace 选项时,egress-gateway 命名空间中的所有服务(包括 ServiceEntries)将通过 waypoint 路由其流量。

验证

  1. 运行以下命令,验证网关配置的状态:

    $ oc get gateways.gateway.networking.k8s.io waypoint -n egress-gateway
    Copy to Clipboard Toggle word wrap

    当配置成功时,PROGRAM MED 列显示 True,如下例所示:

    NAME       CLASS            ADDRESS          PROGRAMMED   AGE
    waypoint   istio-waypoint   172.30.227.148   True         9s
    Copy to Clipboard Toggle word wrap
  2. 运行以下命令,在 egress-gateway 命名空间中创建 curl pod:

    $ oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- sh
    Copy to Clipboard Toggle word wrap
  3. 通过使用 curl 客户端,运行以下命令来验证您可以通过出口网关访问 httpbin.org

    $ curl -v http://httpbin.org/get
    Copy to Clipboard Toggle word wrap

    输出显示了 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"
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat