基于Istio的高级流量管理三

在这里插入图片描述

VirtualService 规则

VirtualService 超时和重试

10秒后断开连接

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    timeout: 10s

超时重试处理,默认会在返回code码为5开头时触发

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    retries:                        // 重试,默认会在返回code码为5开头时触发
      attempts: 3                  // 给定请求允许的重试次数
      perTryTimeout: 2s            // 给定请求的每次超时之间的间隔

VirtualServiceVirtualService 错误注入(混沌测试)

注入错误延迟

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    fault:
      delay:                      // 延迟
        percent: 10              // 将注入延迟的请求百分比(0-100)
        fixedDelay: 5s          // 在转发请求之前添加固定延迟

注入错误状态码

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    fault:
      abort:
        percent: 10
        httpStatus: 400          // 状态码 400

VirtualServiceVirtualService 条件规则

  1. uri 匹配
  2. headers 匹配
  3. sourceLabels 真实标签匹配,用于集群内部(流量甬道部署策略)
1. uri 匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    match:
    - uri:
        prefix: /api/v1



2. headers 匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    match:
    - headers:
       end-user:
         exact: jason

3.  sourceLabels 真实标签匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
    match:
      sourceLabels:
        app: reviews

4. 以上都支持精准匹配和正则匹配
FIELDS:
   exact	<string>

   prefix	<string>

   regex	<string>
     RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax).

VirtualService 流量镜像(流量复制)

mirror 规则可以是 Envoy 截取所有 request,并在转发请求的同时将 request 转发至 mirror 版本,同时在 Header 的 Host/Authority 加上 -shadow。这些 mirror 请求会工作在 fire and forget 模式,所有的 response 都会被废弃。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
      weight: 100               // 镜像要添加权重属性
    mirror:                     // 镜像
      host: productpage
      subset: v2

VirtualService 规则委托(整理分发规则)

路由规则对应着一或多个用 VirtualService 配置指定的请求目的主机。这些主机可以是也可以不是实际的目标负载,甚至可以不是同一网格内可路由的服务。例如要给到 reviews 服务的请求定义路由规则,可以使用内部的名称 reviews,也可以用域名 ,VirtualService 可以定义这样的 host字段:

  • host 字段用显示或者隐式的方式定义了一或多个完全限定名(FQDN)。上面的 reviews,会隐式的扩展成为特定的 FQDN,例如在
    Kubernetes 环境中,全名会从 VirtualService 所在的集群和命名空间中继承而来(比如说
    reviews。default。svc。cluster。local)
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
 - productpage
  gateways:
 - mygateway
  http:
 - match:
    - uri:
        prefix: /productpage
    delegate:
      name: productpage
      namespace: nsA
 - match:
    - uri:
        prefix: /reviews
    delegate:
      name: reviews
      namespace: nsB
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
  namespace: nsA
spec:
  http:
 - match:
    - uri:
        prefix: /productpage/v1/
    route:
    - destination:
      host: productpage-v1.nsA.svc.cluster.local
 - route:
    - destination:
      host: productpage.nsA.svc.cluster.local
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
  namespace: nsB
spec:
  http:
 - match:
    - uri:
        prefix: /reviews/v1/
    route:
    - destination:
      host: reviews-v1.nsB.svc.cluster.local
 - route:
    - destination:
      host: reviews.nsB.svc.cluster.local

VirtualService 优先级

  • 当对同一目标有多个规则时,会按照在 VirtualService 中的顺序进行应用,换句话说,列表中的第一条规则具有最高优先级。
  • 当对某个服务的路由是完全基于权重的时候,就可以在单一规则中完成。另一方面,如果有多重条件(例如来自特定用户的请求)用来进行路由,就会需要不止一条规则。这样就出现了优先级问题,需要通过优先级来保证根据正确的顺序来执行规则。
  • 常见的路由模式是提供一或多个高优先级规则,这些优先规则使用源服务以及 Header
    来进行路由判断,然后才提供一条单独的基于权重的规则,这些低优先级规则不设置匹配规则,仅根据权重对所有剩余流量进行分流。
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    - uri:
        prefix: /              // 越大的规则放后面精细的规则前置

DestinationRule 目标规则

  • 在请求被 VirtualService 路由之后,DestinationRule 配置的一系列策略就生效了(离开 Envoy 往外转请求的时候用到)。
  • 这些策略由服务属主编写,包含断路器、负载均衡以及 TLS等的配置内容。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      simple: RANDOM           // 随机
  subsets:
 - name: v1
    labels:
      version: v1
 - name: v2
    labels:
      version: v2
 - name: v3
    labels:
      version: v3

DestinationRule 断路器

  • 可以用一系列的标准,例如连接数和请求数限制来定义简单的断路器。
  • 可以通过定义 outlierDetection 自定义健康检查模式。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  trafficPolicy:
    connectionPool:                       // 连接池规则
      tcp:                                // HTTP和TCP上游连接通用的设置
        maxConnections: 1                 // 最大连接数
      http:                               // tcp连接池设置
        http1MaxPendingRequests: 1        // 对目标的挂起HTTP请求的最大数量
        maxRequestsPerConnection: 1       // 同时处理的最大连接数
   outlierDetection:                      // 断路器(异常值检测)
     consecutiveErrors: 1                 // 记录错误,累计次数判定为错误
     interval: 1s                         // 探测间隔
     baseEjectionTime: 3m                 // 有问题的 endpoint 会被踢出,踢出多maxEjectionPercent: 100              // 当判定错误后,百分比多少几率剔除后
端 endpoint

ServiceEnvoy

  • Istio 内部会维护一个服务注册表,可以用 ServiceEntry 向其中加入额外的条目。通常这个对象用来启用对 Istio
    服务网格之外的服务发出请求。
  • ServiceEntry 中使用 hosts 字段来指定目标,字段值可以是一个完全限定名,也可以是个通配符域名。
  • 其中包含的白名单, 包含一或多个允许网格中服务访问的服务。
  • 只要 ServiceEntry 涉及到了匹配 host 的服务,就可以和 VirtualService 以及 DestinationRule 配合工作。

Gateway

  • Gateway 为 HTTP/TCP 流量配置了一个负载均衡,多数情况下在网格边缘进行操作,用于启用一个服务的入口(ingress)流量。
  • 和 Kubernetes Ingress 不同,Istio Gateway 只配置四层到六层的功能(例如开放端口或者 TLS配置)。绑定一个 VirtualService 到 Gateway 上,用户就可以使用标准的 Istio 规则来控制进入的 HTTP 和 TCP 流量。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值