Alibaba Cloud 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 an ingress gateway.

  1. Log on to the ASM console.
  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.
  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.
  4. On the details page of the ASM instance, choose Traffic Management Center > Gateway in the left-side navigation pane. On the Gateway page, click Create.
  5. On the Create page, select default from the Namespace drop-down list and set the Name parameter to bookinfo-gateway. Click Gateway Pod Selector, click Add Label for Selecting, and then set the Name parameter to istio and the Value parameter to ingressgateway.
  6. Click Exposed Service, click Add Service, and then set the Name parameter to http, the Port parameter to 80, the Protocol parameter to HTTP, and the Hosts parameter to *.
    Create page
  7. Click Preview to view the YAML file of the Istio gateway. After you confirm the information, click Create.

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.

  1. Log on to the ASM console.
  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.
  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.
  4. On the details page of the ASM instance, choose Traffic Management Center > VirtualService in the left-side navigation pane. On the VirtualService page, click Create.
  5. On the Create page, select default from the Namespace drop-down list and set the Name parameter to the name of the virtual service that you want to create. Click Apply to Gateways (Please Add), enter bookinfo-gateway as the name of the Istio gateway, and then set the Hosts parameter to *.
  6. Click HTTP Route, click Add Route, and then set the Name parameter to a custom name. Click Add Request Matching Rule five times to create five matching rules and turn on Matching URI for each matching rule. Set the Method parameter to Exact and the Content parameter to /productpage for the first matching rule.
  7. 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 parameter 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.
    HTTP Route section
  8. Click Add Route Destination. Set the Host parameter to productpage and the Port parameter to 9080.
    Add Route Destination
  9. Click Preview to view the YAML file of the virtual service. After you confirm the information, click Create.

Step 3: Access the Bookinfo application

  1. Obtain the IP address of the ingress gateway service.
    1. Log on to the ACK console.
    2. In the left-side navigation pane of the ACK console, click Clusters.
    3. On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
    4. In the left-side navigation pane of the details page, choose Network > Services
    5. At the top of the Services page, select istio-system from the Namespace drop-down list. Find the ingress gateway that is named istio-ingressgateway and view the IP address whose port is 80 in the External Endpoint column.
  2. 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 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

  1. 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 include the v1, v2, and v3 subsets.
    1. Log on to the ASM console.
    2. In the left-side navigation pane, choose Service Mesh > Mesh Management.
    3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.
    4. On the details page of the ASM instance, choose Traffic Management Center > DestinationRule in the left-side navigation pane. On the DestinationRule page, click Create.
    5. On the Create page, select default from the Namespace drop-down list, set the Name parameter to the name of the destination rule that you want to create, and then set the Host parameter to reviews.
    6. Click Subset. Then, click Add Subset. Set the Name parameter to v1. Click Add Label and set the Name parameter to version and the Value parameter to v1.
    7. Click Add Subset. Set the Name parameter to v2. Then, click Add Label and set the Name parameter to version and the Value parameter to v2.
    8. Click Add Subset. Set the Name parameter to v3. Then, click Add Label and set the Name parameter to version and the Value parameter to v3.
      Subset section
    9. Click Preview to view the YAML file of the destination rule. After you confirm the information, click Create.
  2. 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.
    1. Log on to the ASM console.
    2. In the left-side navigation pane, choose Service Mesh > Mesh Management.
    3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.
    4. On the details page of the ASM instance, choose Traffic Management Center > VirtualService in the left-side navigation pane. On the VirtualService page, click Create.
    5. On the Create page, select default from the Namespace drop-down list, set the Name parameter to the name of the virtual service that you want to create, and then set the Hosts parameter to reviews.
    6. Click HTTP Route. Click Add Route, set the Name parameter to a custom name, and then click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v1, and the Weight parameter to 10.
    7. Click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v2, and the Weight parameter to 40.
    8. Click Add Route Destination. Set the Host parameter to reviews, the Subset parameter to v3, and the Weight parameter to 50.
      HTTP Route section
    9. Click Preview to view the YAML file of the virtual service. After you confirm the information, click Create.

Step 5: Verify that the traffic routing settings 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 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. In this case, the settings for routing traffic to different versions of the reviews service take effect.