美文网首页微服务 DevOps
Service Mesh - Istio 入口流量路由

Service Mesh - Istio 入口流量路由

作者: CatchZeng | 来源:发表于2021-04-30 09:03 被阅读0次

    原文:https://makeoptim.com/service-mesh/istio-ingress-routing

    上一篇 介绍了 Kubernetes & Istio 开发环境搭建方法,并解释了如何部署应用程序(Bookinfo)和访问服务。但是,究竟流量是怎么从网格外部路由到具体服务的呢?从本文开始,我们借助开发环境,深入了解下 Istio 的入口流量路由

    网格可视化

    在介绍入口流量路由之前,我们先来看下网格可视化 Kiali

    1. 执行以下命令打开 Kiali UI

      $ istioctl dashboard kiali
      http://localhost:20001/kiali
      
      
    2. 多次访问 http://localhost/productpage

    3. 到 Kiali UI 访问 Graph 可观察到如下流量图

      Istio

    由上图可以看出,流量起源于外部,并通过 istio-ingressgateway 流向服务网格内部的服务。

    我们知道在 Kubernetes 中有一个入口控制器 Ingress 来控制所有入口流量。而在 Istio 中,Ingress 被替换为 Gateway & VirtualService 两个 CRDs。而 istio-ingressgateway 正是 Istio 预先配置好的入口 Gateway。

    Gateway

    Istio Gateway 可以管理网格入站出站流量(主要用于管理入站的流量)。与服务工作负载的 sidecar 代理不同,Gateway 配置被用于运行在网格边界的独立 Envoy 代理。

    与 Kubernetes Ingress API 这种控制进入系统流量的机制不同,Gateway 的流量路由更加强大和灵活。因为是 Gateway 可以配置 4-6 层的负载均衡属性,如对外暴露的端口、TLS 设置等,而应用层流量路由(L7)则交给 VirtualService,它可以绑定到一个 Gateway 上。这让你可以像管理网格中其他数据平面的流量一样去管理 Gateway 流量

    Istio 提供了一些预先配置好的 Gateway(istio-ingressgateway 和 istio-egressgateway)供我们使用。

    注:上一篇中使用了 demo profile 所以 istio-ingressgateway 和 istio-egressgateway 都已经部署好了,详见 profiles

    Istio

    下面是 Bookinfo 例子中的 Gateway

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: bookinfo-gateway
    spec:
      selector:
        istio: ingressgateway # use istio default controller
      servers:
        - port:
            number: 80
            name: http
            protocol: HTTP
          hosts:
            - "*"
    
    

    例子中 Gateway 使用了默认的 ingressgateway,并让 HTTP 流量从 hosts 通过 80 端口进入网格。因此,访问 http://localhost/productpageHTTP 流量便由 istio-ingressgateway 通过 bookinfo-gateway 配置经 80 端口流入网格内

    注:这里 hosts 设置为 ,表示允许所有 HTTP 流量流向 80 端口。如果你想指定域名*可以将 hosts 设置为需要的域名,这样便只有设置的域名才能通过 bookinfo-gateway 将外部 HTTP 流量通过 80 端口流入网格内。

    那到了 bookinfo-gateway 的流量又是如何进入到 productpage 服务的呢?这就需要一个 VirtualService 来路由此流量。

    VirtualService

    Istio VirtualService 配置如何在服务网格内将请求路由到服务,这基于 Istio 和平台提供的基本的连通性和服务发现能力。每个 VirtualService 包含一组路由规则,Istio 按顺序评估它们,Istio 将每个给定的请求匹配到 VirtualService 指定的实际目标地址。

    下面是 Bookinfo 例子中的 VirtualService

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: bookinfo
    spec:
      hosts:
      - "*"
      gateways:
      - bookinfo-gateway
      http:
      - match:
        - uri:
            exact: /productpage
        - uri:
            prefix: /static
        - uri:
            exact: /login
        - uri:
            exact: /logout
        - uri:
            prefix: /api/v1/products
        route:
        - destination:
            host: productpage
            port:
              number: 9080
    
    

    bookinfo VirtualService 绑定了 bookinfo-gateway,并声明了路由规则到 productpage 服务。因此,访问 http://localhost/productpageHTTP 流量便由 istio-ingressgateway 通过 bookinfo-gateway 配置经 80 端口流入网格内,经过 bookinfo VirtualService 的路由规则,匹配到了 exact: /productpage。最后,将流量流入了 productpage 服务的 9080 端口。

    apiVersion: v1
    kind: Service
    metadata:
      name: productpage
      labels:
        app: productpage
        service: productpage
    spec:
      ports:
      - port: 9080
        name: http
      selector:
        app: productpage
    
    

    小结

    Istio 通过 Gateway 管理网格入站流量,再通过 VirtualService 配置路由规则,将请求路由到服务。Gateway 好比“园区(网格)门口管理着进来的“(流量),而 VirtualService 则是园区里面每一条通往“(服务)小径

    Istio

    本文旨让大家理解入口路由的基本情况,下一篇我们再详细讲解 Istio 的流量管理,咱们下一篇见。

    参考

    相关文章

      网友评论

        本文标题:Service Mesh - Istio 入口流量路由

        本文链接:https://www.haomeiwen.com/subject/dbwirltx.html