Service Mesh (ASM) allows you to route traffic to different versions of a service based on the specified ratio. This feature is applicable to many scenarios such as canary release and A/B testing. This topic describes how to use Istio resources to route traffic to different versions of a service.
Prerequisites
Step 1: Create an Istio gateway
Create an Istio gateway and associate it with the ingress gateway.
- Log on to the ASM console. In the left-side navigation pane, choose .
- On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose . On the page that appears, click Create.
On the Create page, configure the following parameters.
In the Basic Information section, select default from the Namespace drop-down list, and set the Name parameter to bookinfo-gateway.
In the Gateway Pod Selector section, set the Key parameter to istio and the Value parameter to ingressgateway.
In the Exposed Service section, set the Name parameter to http, the Port parameter to 80, the Protocol parameter to HTTP, and the Hosts parameter to *.
Click Preview to view the YAML file of the Istio gateway. Make sure that the configurations are correct and click Submit. Then, click Create in the lower part of the Create page.
Step 2: Create a virtual service
You can create a virtual service that allows requests to the Bookinfo application by using the /productpage, /static, /login, /logout, or /api/v1/products path.
- Log on to the ASM console. In the left-side navigation pane, choose .
- On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose . On the page that appears, click Create.
In the Basic Information section of the Create page, configure the parameters that are described in the following table.
Parameter
Description
Namespace
In this example, default is selected.
Name
The name of the virtual service.
Gateways
Turn on Apply To specific Gateways and click Select the name of the Gateway.
In the Select the name of the Gateway dialog box, select bookinfo-gateway, click the
icon, and then click OK.
Turn off Apply To All Sidecars.
Hosts
Click Select the gateway domain name which VirtualService belongs to. In the Select the gateway domain name which VirtualService belongs to dialog box, select *, click the
icon, and then click OK.
Click HTTP Route, click Add Route, and then specify a route name in the Name field. Click Add Request Matching Rule five times to create five matching rules and turn on Matching request URI for each matching rule. Set the Method parameter to Exact and the Content parameter to /productpage for the first matching rule.
Set the Method parameter to Prefix and the Content parameter to /static for the second matching rule. Set the Method parameter to Exact and the Content parameter to /login for the third matching rule. Set the Method parameter to Exact and the Content parameter to /logout for the fourth matching rule. Then, set the Method parameter to Prefix and the Content parameter to /api/v1/products for the fifth matching rule.
Click Add Route Destination. Set the Host parameter to productpage and the Port parameter to 9080.
Click Preview to view the YAML file of the virtual service. Make sure that the configurations are correct and click Submit. Then, click Create in the lower part of the Create page.
Step 3: Access the Bookinfo application
Obtain the IP address of the ingress gateway.
Log on to the ACK console and click Clusters in the left-side navigation pane.
On the Clusters page, click the name of a cluster and choose in the left-side navigation pane.
At the top of the Services page, select istio-system from the Namespace drop-down list. Find your ingress gateway service and view the IP address whose port is 80 in the External Endpoint column.
Access the Bookinfo application.
In the address bar of your browser, enter an address in the following format: http://{IP address of the ingress gateway service}/productpage. Refresh the web page ten times.
During this process, the Bookinfo application alternately accesses the v1, v2, and v3 versions of the reviews service. You can view no stars on the page three times, view black stars on the page three times, and view red stars on the page four times.
Step 4: Create routing rules for different versions of a service
Create a destination rule.
Create a destination rule and group instances of the reviews service by version. In this example, the instances of the reviews service are grouped into three subsets: v1, v2, and v3.
- Log on to the ASM console. In the left-side navigation pane, choose .
- On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose . On the page that appears, click Create.
On the Create page, select default from the Namespace drop-down list, specify a name for the destination rule in the Name field, and then set the Host parameter to reviews.
Click Service Version (subset). Then, click Add Service Version (subset). Set the Name parameter to v1. Click Add Label, and set the Key parameter to version and the Value parameter to v1.
Click Add Service Version (subset). Set the Name parameter to v2. Then, click Add Label, and set the Key parameter to version and the Value parameter to v2.
Click Add Service Version (subset). Set the Name parameter to v3. Then, click Add Label, and set the Key parameter to version and the Value parameter to v3.
Click Preview to view the YAML file of the destination rule. Make sure that the configurations are correct and click Submit. Then, click Create in the lower part of the Create page.
Create a virtual service.
Create a virtual service to route 10% of the traffic to the v1 version of the reviews service, 40% of the traffic to the v2 version of the reviews service, and 50% of the traffic to the v3 version of the reviews service.
- Log on to the ASM console. In the left-side navigation pane, choose .
- On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose . On the page that appears, click Create.
On the Create page, select default from the Namespace drop-down list, specify a name for the virtual service in the Name field, and click Add Host next to Hosts.
In the Add Host dialog box, select default from the Namespace drop-down list. In the Add Host box, select reviews, click the
icon, and then click OK.
Click HTTP Route. Click Add Route, specify a route name in the Name field, and then click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v1, and the Weight parameter to 10.
Click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v2, and the Weight parameter to 40.
Click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v3, and the Weight parameter to 50.
Click Preview to view the YAML file of the virtual service. Make sure that the configurations are correct and click Submit. Then, click Create in the lower part of the Create page.
Step 5: Verify that the traffic routing configurations take effect
In the address bar of your browser, enter an address in the following format: http://{IP address of the ingress gateway service}/productpage. Refresh the web page ten times.
You can view no stars on the page one time, view black stars on the page four times, and view red stars on the page five times. This indicates that 10% of the traffic is routed to the v1 version of the reviews service, 40% of the traffic to the v2 version of the reviews service, and 50% of the traffic to the v3 version of the reviews service. The result shows that the configurations take effect and traffic is routed as expected.