You can use a TrafficLabel CustomResourceDefinition (CRD) to configure traffic labels. To route traffic to the corresponding workload based on labels, you must also create the corresponding destination rule and virtual service. This topic describes how to define a routing rule based on traffic labels.
Prerequisites
Traffic labels are created. For more information, see Label traffic.
Procedure
Create a file named dr-productpage.yaml and copy the following content to the file.
The following example configures multiple subsets, such as
test1
,test2
, andtest3
, forproductpage
: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
Run the following command to create a destination rule:
kubectl apply -f dr-productpage.yaml
Create a file named vs-productpage.yaml and copy the following content to the file.
A combination of
match.headers
androute.destination.subset
indicates that traffic with the label is routed to the corresponding destination workload. In the following example, requests with theasm-labels-test
label name and thetest1
label value are routed to thetest1
subset, those with the test2 label value are routed to the test2 subset, and those with the test3 label value are routed to the test3 subset: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
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
Related operations
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 and 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, a 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. Sample YAML code:
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
Parameter | Description |
target.host | The name of the destination service of the route. |
target.subset | The subset of the destination service of the route. |