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.
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
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
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
Traffic Labelling and Routing of ASM (1): The Definition of Traffic Label
Traffic Labeling and Routing of ASM (3): Use Lanes to Manage Traffic
56 posts | 8 followers
FollowXi Ning Wang(王夕宁) - August 17, 2023
Xi Ning Wang(王夕宁) - August 17, 2023
Xi Ning Wang(王夕宁) - August 17, 2023
Alibaba Cloud Native - January 18, 2024
Alibaba Cloud Native - January 23, 2024
Alibaba Cloud Native - August 7, 2024
56 posts | 8 followers
FollowAlibaba Cloud Service Mesh (ASM) is a fully managed service mesh platform that is compatible with Istio.
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreMulti-source metrics are aggregated to monitor the status of your business and services in real time.
Learn MoreAlibaba Cloud PolarDB for Xscale (PolarDB-X) is a cloud-native high-performance distributed database service independently developed by Alibaba Cloud.
Learn MoreMore Posts by Xi Ning Wang(王夕宁)