2.6. 在 ambient 模式中使用 Kubernetes Gateway API 来公开服务


您可以使用 Kubernetes 网关 API 创建 网关和 HTTPRoute 资源,并在 ambient 模式中部署网关。资源配置网关,将网格中的服务公开给网格外的流量。

先决条件

  • 以具有 cluster-admin 角色的用户身份登录到 OpenShift Container Platform Web 控制台。
  • 已安装 Red Hat OpenShift Service Mesh Operator。
  • 您已部署了 Istio 资源。
  • 您可以使用 Kubernetes 原生网关 API 资源。
  • 您是使用 Istio 辅助模式,或计划迁移到 ambient 模式。
注意

当使用 ambient 模式(istio.io/dataplane-mode=ambient)时,建议使用 Kubernetes 网关 API 进行入口配置,因为 Istio 网关和 VirtualService 资源与 ambient 模式不完全兼容。

流程

  1. 运行以下命令,创建一个名为 httpbin 的命名空间:

    $ oc create namespace httpbin
    Copy to Clipboard Toggle word wrap
  2. 运行以下命令,为 ambient 模式应用标签:

    $ oc label namespace httpbin istio.io/dataplane-mode=ambient
    Copy to Clipboard Toggle word wrap
  3. 运行以下命令,部署名为 httpbin 的示例服务:

    $ oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yaml
    Copy to Clipboard Toggle word wrap
  4. 通过创建名为 httpbin-waypoint.yaml 的 YAML 文件来部署 waypoint 代理,如下例所示:

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

    $ oc apply -f httpbin-waypoint.yaml
    Copy to Clipboard Toggle word wrap
  6. 运行以下命令,在 httpbin 服务中启用 ingress waypoint 路由:

    $ oc label service httpbin -n httpbin istio.io/ingress-use-waypoint=true
    Copy to Clipboard Toggle word wrap

    该标签确保 ingress 网关路由的流量通过 waypoint 代理和第 7 层(L7)策略在 waypoint 代理上配置到入口流量,然后再到达 httpbin 服务。

  7. 运行以下命令,将 waypoint 标签应用到命名空间,以便命名空间路由中的所有服务都通过 waypoint 进行路由:

    $ oc label ns httpbin istio.io/use-waypoint=httpbin-waypoint
    Copy to Clipboard Toggle word wrap
  8. 创建名为 httpbin-gw.yaml 的 YAML 文件,用于定义 Kubernetes 网关资源。此资源将网关代理配置为为主机公开端口 80 (HTTP),即 httpbin.example.com

    apiVersion: gateway.networking.k8s.io/v1
    kind: Gateway
    metadata:
      name: httpbin-gateway
      namespace: httpbin
    spec:
      gatewayClassName: istio
      listeners:
      - name: default
        hostname: "httpbin.example.com"
        port: 80
        protocol: HTTP
        allowedRoutes:
          namespaces:
            from: All
    Copy to Clipboard Toggle word wrap
    "httpbin.example.com"
    指定客户端在尝试访问关联端口上网格服务时使用的虚拟主机名。
  9. 运行以下命令来应用 YAML 文件:

    $ oc apply -f httpbin-gw.yaml
    Copy to Clipboard Toggle word wrap
  10. 创建名为 httpbin-ingress-hr.yaml 的 YAML 文件,该文件为 ingress 网关定义 HTTPRoute 资源,如下例所示:

    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-ingress
      namespace: httpbin
    spec:
      parentRefs:
      - name: httpbin-gateway
        namespace: httpbin
      hostnames:
      - "httpbin.example.com"
      rules:
      - backendRefs:
        - name: httpbin
          port: 8000
    Copy to Clipboard Toggle word wrap
    • spec.parentRefsHTTPROUTE 资源绑定到上一步中创建的 Kubernetes 网关资源。
    • spec.rules. backendRefs 通过定义包括 httpbin 服务的 name 和 port 来将匹配的流量路由到 httpbin 服务。

    HTTPRoute 资源指定将网关代理的流量路由到 httpbin 服务的规则。

  11. 运行以下命令来应用 YAML 文件:

    $ oc apply -f httpbin-ingress-hr.yaml
    Copy to Clipboard Toggle word wrap
  12. 创建名为 httpbin-waypoint-hr.yaml 的 YAML 文件,该文件为 waypoint 代理定义 HTTPRoute 资源。

    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-waypoint-route
      namespace: httpbin
    spec:
      parentRefs:
      - group: ""
        kind: service
        name: httpbin
        namespace: httpbin
      rules:
      - matches:
        - path:
            type: PathPrefix
            value: /status
        - path:
            type: PathPrefix
            value: /headers
        backendRefs:
        - name: httpbin
          port: 8000
    Copy to Clipboard Toggle word wrap
    • spec.parentRefsHTTPRoute 资源绑定到 httpbin 服务。当与服务上的 istio.io/ingress-use-waypoint=true 标签结合使用时,HTTPRoute 配置 L7 路由规则,其中point 代理针对目标到该服务的流量强制执行。
    • spec.rules. backendRefs 通过定义包括 httpbin 服务的 name 和 port 来将匹配的流量路由到 httpbin 服务。
  13. 运行以下命令来应用 YAML 文件:

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

    在本例中,因为 istio.io/ingress-use-waypoint=true 标签,来自 ingress 网关的流量通过 waypoint 代理流。HTTPRoute 资源随后在流量到达 httpbin 服务前应用基于路径的路由策略。

  14. 运行以下命令,确保 waypoint 代理已就绪:

    $ oc wait --for=condition=programmed gtw httpbin-waypoint -n httpbin
    Copy to Clipboard Toggle word wrap

验证

  1. 运行以下命令,为 curl 客户端创建命名空间:

    $ oc create namespace curl
    Copy to Clipboard Toggle word wrap
  2. 运行以下命令来部署 curl 客户端:

    $ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yaml
    Copy to Clipboard Toggle word wrap
  3. 运行以下命令,将 ambient 模式的标签应用到 curl 命名空间:

    $ oc label namespace curl istio.io/dataplane-mode=ambient
    Copy to Clipboard Toggle word wrap
  4. 运行以下命令,使用 curl pod 的名称设置 CURL_POD 变量:

    $ CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')
    Copy to Clipboard Toggle word wrap
  5. 使用 curl 客户端,通过入口网关服务资源向 httpbin 应用程序的 /headers 端点发送请求。将请求的 Host 标头设置为 httpbin.example.com,以匹配 Kubernetes 网关和 HTTPROUTE 资源指定的主机。运行以下命令来发送 curl 请求:

    $ oc exec $CURL_POD -n curl -- \
      curl -s -I \
        -H Host:httpbin.example.com \
        httpbin-gateway-istio.httpbin.svc.cluster.local/headers
    Copy to Clipboard Toggle word wrap

    响应应返回 200 OK HTTP 状态,这表示请求成功,如下例所示:

    HTTP/1.1 200 OK
    server: istio-envoy
    ...
    Copy to Clipboard Toggle word wrap
  6. 运行以下命令,向没有相应 Uniform Resource Identifier (URI)前缀匹配 的端点 发送 curl 请求:

    $ oc exec $CURL_POD -n curl -- \
      curl -s -I \
        -H Host:httpbin.example.com \
        httpbin-gateway-istio.httpbin.svc.cluster.local/get
    Copy to Clipboard Toggle word wrap

    响应返回 404 Not Found 状态,因为 /get 端点在 httpbin HTTPROUTE 资源中没有匹配的 URI 前缀,如下例所示:

    HTTP/1.1 404 Not Found
    server: istio-envoy
    ...
    Copy to Clipboard Toggle word wrap
  7. 通过将 Service 类型设置为 LoadBalancer,将网关代理公开给集群外的流量。运行以下命令:

    $ oc patch service httpbin-gateway-istio -n httpbin -p '{"spec": {"type": "LoadBalancer"}}'
    Copy to Clipboard Toggle word wrap
    注意

    网关也可以使用 OpenShift 路由公开给集群外部的流量。如需更多信息,请参阅"使用 OpenShift 路由向集群外的流量扩展网关"。

  8. 使用网关服务资源的外部主机名或 IP 地址,验证可以从集群外部访问 httpbin 服务。确保为运行集群的环境正确设置了 INGRESS_HOST 变量。

    1. 运行以下命令设置 INGRESS_HOST 变量:

      $ export INGRESS_HOST=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.status.addresses[0].value}')
      Copy to Clipboard Toggle word wrap
    2. 运行以下命令设置 INGRESS_PORT 变量:

      $ INGRESS_PORT=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.spec.listeners[?(@.name=="http")].port}')
      Copy to Clipboard Toggle word wrap
    3. 使用网关主机,运行以下命令来向 httpbin 服务发送 curl 请求:

      $ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headers
      Copy to Clipboard Toggle word wrap
  9. 验证响应是否具有 HTTP/1.1 200 OK 状态,这表示请求是否成功。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat