> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/bu-shu-yun-wei-xu-ni-hua/kubernetes/12.5istio-he-xin-gong-neng-shi-jian-zhi-neng-lu-you.md).

# 12.5Istio核心功能实践-智能路由

请求路由任务首先会把Bookinfo应用的流量导向reviews服务的v1版本。接下来会把特定用户的请求发送给v2版本。其他用户不受影响

应用默认目标规则

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  subsets:
  - name: v1
    labels:
      version: v1
```

> 关注点：
>
> subsets
>
> labels：对应pod的labels

应用VirtualService

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  host: 
  - productpage
  http:
  - route:
      - destination:
          host:productPage
          subset:v1
```

> 关注点：
>
> subset为在DestinationRule中定义的subset

把特定用户的请求发送给v2版本

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
  ...
spec:
  hosts:
  - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1
```

> 关注点：当headers中end-user为jason的流量将路由到v2版本

故障注入

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    fault:
      delay:
        percentage:
          value: 100.0
        fixedDelay: 7s
    route:
    - destination:
        host: ratings
        subset: v1
  - route:
    - destination:
        host: ratings
        subset: v1
```

> 注意点：
>
> fault：
>
> ​ delay：
>
> ​ fixedDelay：设置延迟7s

流量迁移

迁移50%的流量从v1到v3

```yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 50
    - destination:
        host: reviews
        subset: v3
      weight: 50
```

> 关注点：
>
> weight：权重
