文档

基于流量标签定义路由规则

流量标签(TrafficLabel )CRD用于配置自定义的流量标签。为支持基于标签的路由能力,您还需要创建相应的目标规则(DestinationRule)和虚拟服务(VirtualService),才能将流量根据标签路由到对应的工作负载。本文介绍如何基于流量标签定义路由规则。

前提条件

已创建TrafficLabel。具体操作,请参见流量标签TrafficLabel说明

操作步骤

  1. 使用以下内容,创建dr-productpage.yaml文件。

    以下文件表示将productpage分为test1test2test3等多个子集。

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: dr-productpage
    spec:
      host: productpage
      subsets:
      - name: test1
        labels:
          version: test1
      - name: test2
        labels:
          version: test2
      - name: test3
        labels:
          version: test3
      ...
      - name: testn
        labels:
          version: testn
      - name: base
        labels:
          version: base
  2. 执行以下命令,创建目标规则DestinationRule。

    kubectl apply -f dr-productpage.yaml
  3. 使用以下内容,创建vs-productpage.yaml文件。

    match.headersroute.destination.subset表示将流量根据标签值路由到目标工作负载。例如,将带有名称为asm-labels-test且值为test1的标签的请求路由到test1子集下的工作负载。

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: vs-productpage
    spec:
      hosts:
        - productpage
      http:
      - match:
        - headers:
            asm-labels-test:
             exact: test1
        route:
        - destination:
            host: productpage
            subset: test1
      - match:
        - headers:
            asm-labels-test:
             exact: test2
        route:
        - destination:
            host: productpage
            subset: test2
      - match:
        - headers:
            asm-labels-test:
             exact: test3
        route:
        - destination:
            host: productpage
            subset: test3
      - route:
        - destination:
            host: productpage
            subset: base

    参数

    说明

    match.headers.asm-labels-test

    流量标签名称。

    match.headers.exact

    流量标签值。

    route.destination.subset

    路由目标对应的目标工作负载所在的子集(Subset)。

  4. 执行以下命令,创建虚拟服务VirtualService。

    kubectl apply -f vs-productpage.yaml

相关操作

简化虚拟服务的定义

若工作负载存在多个版本时,虚拟服务VirtualService的配置将变得较为复杂。您可以使用以下内容简化虚拟服务的定义,并对流量进行降级。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-productpage
spec:
  hosts:
    - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: $asm-labels-test
                

扩展虚拟服务的定义,支持流量降级

当目标服务不可用时,ASM企业版提供流量降级功能。fallback参数定义在route参数下,用于定义标签路由的目标服务不可用(包括目标服务未定义或对应的Pod不存在等情况)时回退到备用服务。YAML示例如下。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-productpage
spec:
  hosts:
    - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: $asm-labels-test
      fallback:
        target:
          host: productpage
          subset: base
                

参数

说明

target.host

路由的目标服务名称。

target.subset

路由的目标服务的子集。

  • 本页导读 (1)
文档反馈