×
Community Blog Traffic Labelling and Routing of ASM (2): Define a Routing Rule Based on Traffic Labels

Traffic Labelling and Routing of ASM (2): Define a Routing Rule Based on Traffic Labels

Part 2 of this 4-part series describes how to define a routing rule based on traffic labels.

By Xining Wang

You can use a TrafficLabel Custom Resource Definition (CRD) to configure traffic labels, such as asm-labels-test, whose values are test1, test2, and test3. To route traffic to the corresponding workload based on labels, you must also create the corresponding destination rule and virtual service.

Define Destination Rule and Virtual Service

1.  Create a file named dr-productpage.yaml and save the following content to the file. The following example configures multiple subsets, such as test1, test2, and test3, for productpage.

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.  Run the following command to create a destination rule:

kubectl apply -f dr-productpage.yaml

3.  Create a file named vs-productpage.yaml and save the following content to the file.

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

A combination of match.headers and route.destination.subset indicates that traffic with the label is routed to the corresponding destination workload. In the following example, requests with the asm-labels-test label name and the test1 label value are routed to the test1 subset.

The specific parameters and descriptions are as follows:

Parameter Description
match.headers.asm-labels-test The name of the traffic label.
match.headers.exact The value of the traffic label.
route.destination.subset The subset to which the destination workload of the route belongs.

Run the following command to create a virtual service:

kubectl apply -f vs-productpage.yaml

Simplify the Definition of a Virtual Service

If workloads of multiple versions exist, the configuration of the virtual service becomes complicated. You can use the following code to simplify the definition of the virtual service shift traffic to workloads of other versions of the destination workload that is unavailable.

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

Extend the Definition of a Virtual Service and Shift Traffic Between Service Versions

When the destination service is unavailable, an Alibaba Cloud Service Mesh (ASM) instance of Enterprise Edition allows you to shift traffic to a backup service. The fallback parameter under route specifies the backup service to which requests fall back when the destination service of the label-based route is unavailable.

For example, the destination service is not defined or the corresponding pod does not exist. The specific fields and descriptions are as follows:

Parameter Description
target.host The name of the destination service of the route.
target.subset The subset of the destination service of the route.

The sample is as follows:

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
0 0 0
Share on

Xi Ning Wang(王夕宁)

56 posts | 8 followers

You may also like

Comments