×

使用 Istio API,您可以配置使用网关注入安装的网关代理,以引导绑定到外部服务的流量。

关于通过网关引导出站流量

您可以将使用网关注入安装的网关配置为离开服务网格的流量的出口点。在此配置中,网关充当发送到网格外部服务的请求的正向代理。

配置网关以处理出站流量有助于满足安全要求。例如,在流量限制要求所有离开网格的流量都流经一组专用节点的环境中,可以使用出站网关。类似地,当网络策略阻止应用程序节点直接访问外部服务时,可以使用网关。在这种情况下,网关代理部署在能够访问外部服务的专用出站节点上。然后,可以对这些节点进行严格的网络策略执行或额外的监控以增强安全性。

要配置使用网关注入安装的网关以引导出站流量,请结合使用 Istio 的 `ServiceEntry`、`Gateway`、`VirtualService` 和 `DestinationRule` 资源。使用 `ServiceEntry` 资源定义外部服务的属性。外部服务被添加到网格的 Istio 服务注册表中。这使您可以将 Istio 功能(例如监控和路由规则)应用于离开网格并目标为外部服务的流量。使用 `Gateway`、`VirtualService` 和 `DestinationRule` 资源设置规则,以使用网关代理将流量从网格路由到外部服务。

使用 Istio API 通过网关引导出站流量

使用 Istio API 通过使用网关注入安装的网关引导出站 HTTP 流量。

先决条件
  • 您已使用网关注入安装了网关。

步骤
  1. 通过运行以下命令创建一个名为 `curl` 的命名空间

    $ oc create namespace curl
  2. 根据您使用的更新策略,通过运行相应的命令启用命名空间中的 sidecar 注射

    1. 如果您使用的是 `InPlace` 更新策略,请运行以下命令

      $ oc label namespace curl istio-injection=enabled
    2. 如果您使用的是 `RevisionBased` 更新策略,请运行以下命令

      1. 通过运行以下命令显示修订版名称

        $ oc get istiorevisions.sailoperator.io
        示例输出
        NAME              TYPE    READY   STATUS    IN USE   VERSION   AGE
        default-v1-23-0   Local   True    Healthy   True     v1.23.0   3m33s
      2. 使用修订版名称标记命名空间以启用 sidecar 注射,方法是运行以下命令

        $ oc label namespace curl istio.io/rev=default-v1-23-0
  3. 通过运行以下命令部署 `curl` 应用程序

    $ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yaml
  4. 导出一个 `CURL_POD` 环境变量,该变量已初始化为 curl pod 的名称

    $ export CURL_POD=$(oc get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')
  5. 创建一个名为 `http-se.yaml` 的 YAML 文件,该文件将流量从网格引导到外部服务。以下示例定义了 URL 的 `ServiceEntry`。

    示例配置
    apiVersion: networking.istio.io/v1
    kind: ServiceEntry
    metadata:
      name: egress-se
      namespace: curl
    spec:
      hosts:
        - docs.redhat.com
      ports:
        - number: 80
          name: http-port
          protocol: HTTP
      location: MESH_EXTERNAL
      resolution: DNS
  6. 通过运行以下命令应用 YAML 文件

    $ oc apply -f http-se.yaml
  7. 确保 `ServiceEntry` 配置已正确应用。通过运行以下命令向您在上一步中指定的宿主发送 HTTP 请求

    $ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.com

    此命令应返回 HTTP 状态代码,例如 `301`(重定向)或 `200`(成功),表明连接有效。

  8. 创建一个名为 `http-gtw.yaml` 的 YAML 文件,该文件创建出站 `Gateway` 并将流量从网格路由到为外部服务指定的宿主。

    示例配置
    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: egress-gw
      namespace: <gateway_namespace> # Namespace where the egress gateway is deployed
    spec:
      selector:
        istio: <gateway_name> # Selects the egress-gateway instance to handle this traffic
      servers:
        - port:
            number: 80
            name: http
            protocol: HTTP
          hosts:
            - docs.redhat.com # External service host, not a full URL.
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: egress-dr
      namespace: <gateway_namespace> # Namespace where the egress gateway is deployed
    spec:
      host: <gateway_name>.<gateway_namespace>.svc.cluster.local
      subsets:
        - name: rh-docs
  9. 通过运行以下命令应用 YAML 文件

    $ oc apply -f http-gtw.yaml
  10. 创建一个名为 `http-vs.yaml` 的 YAML 文件,该文件设置 `VirtualService` 以管理通过出站网关到外部宿主的应用程序 sidecar 的流量流。

    示例配置
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: egress-vs
      namespace: curl # Namespace where the curl pod is running
    spec:
      hosts:
        - docs.redhat.com # External service host, not a full URL.
      gateways:
        - mesh
        - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step.
      http:
        - match:
            - gateways:
                - mesh
              port: 80
          route:
            - destination:
                host: <gateway_name>.<gateway_namespace>.svc.cluster.local
                subset: rh-docs
                port:
                  number: 80
              weight: 100
        - match:
            - gateways:
                - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step.
              port: 80
          route:
            - destination:
                host: docs.redhat.com
                port:
                  number: 80
              weight: 100
  11. 通过运行以下命令应用 YAML 文件

    $ oc apply -f http-vs.yaml
  12. 重新向 URL 发送 HTTP 请求

    $ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.com

    终端应显示类似于以下输出的信息

    示例输出
    ...
    HTTP/1.1 301 Moved Permanently
    ...
    location: <example_url>
    ...
    
    HTTP/2 200
    Content-Type: text/html; charset=utf-8
  13. 通过运行以下命令确保请求已通过网关路由

    $ oc logs deployment/<gateway_name> -n <gateway_namespace> | tail -1

    必须启用访问日志记录才能使此验证步骤有效。您可以通过将 Istio 资源中的 `spec.values.meshConfig.accessLogFile` 字段设置为 `/dev/stdout` 来启用对标准输出的访问日志记录。

    终端应显示类似于以下输出的信息

    示例输出
    [2024-11-07T14:35:52.428Z] "GET / HTTP/2" 301 - via_upstream - "-" 0 0 24 24 "10.128.2.30" "curl/8.11.0" "79551af2-341b-456d-b414-9220b487a03b" "docs.redhat.com" "23.55.176.201:80" outbound|80||docs.redhat.com 10.128.2.29:49766 10.128.2.29:80 10.128.2.30:38296 -