All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use Istio resources to route traffic to different versions of a service

Last Updated:Dec 05, 2024

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

An Istio gateway defines a load balancer that works at the edge of a service mesh to receive inbound or outbound HTTP/TCP traffic. The following section describes how to create an Istio gateway and bind it to an ingress gateway.

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Gateway. On the page that appears, click Create.

  3. On the Create page, configure the following parameters and then click Create.

    For more information about the parameters, see Gateway.

    Parameter

    Description

    Basic Information

    Set Namespace to default and Name to bookinfo-gateway.

    Gateway Pod Selector

    For this example, set Key to istio and Value to ingressgateway.

    Exposed Service

    Set Name to http, Port to 80, Protocol to HTTP, and Service to *.

    网关规则

    The following YAML code shows the Istio gateway that corresponds to the preceding configurations.

    Expand to view the YAML code of the Istio gateway

    apiVersion: networking.istio.io/v1beta1
    kind: Gateway
    metadata:
      name: bookinfo-gateway
      namespace: default
    spec:
      selector:
        istio: ingressgateway
      servers:
        - port:
            number: 80
            name: http
            protocol: HTTP
          hosts:
            - '*'
    

Step 2: Create a virtual service

A virtual service defines traffic routing rules and request forwarding policies. After you create a virtual service, you can use the following paths to access the Bookinfo application: /productpage, /static, /login, /logout, and /api/v1/products.

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > VirtualService. On the page that appears, click Create.

  3. On the Create page, configure the following parameters and then click Create.

    For more information about the parameters, see Virtual Service.

    Parameter

    Description

    Basic Information

    Namespace

    In this example, default is selected.

    Name

    Specify a name for the virtual service.

    Gateways

    1. Turn on the Apply To specific Gateways switch and click Select the name of the Gateway.

    2. In the Select the name of the Gateway dialog box, select bookinfo-gateway, click the 添加 icon, and then click OK.

    3. Turn off the Apply To All Sidecars switch.

    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.

    HTTP Route

    Name

    Click the HTTP Route tab, click Add Route, and then enter a route name in the Name field.

    Request matching rules

    Click Add Request Matching Rule repeatedly to configure the following five rules:

    • Turn on Matching request URI, set Method to Exact and Content to /productpage.

    • Turn on Matching request URI, set Method to Prefix and Content to /static.

    • Turn on Matching request URI, set Method to Exact and Content to /login.

    • Turn on Matching request URI, set Method to Exact and Content to /logout.

    • Turn on Matching request URI, set Method to Prefix and Content to /api/v1/products.

    Route destination

    Click Add Route Destination. Set Host to productpage and Port to 9080.

    The following YAML code shows the virtual service that corresponds to the preceding configurations.

    Expand to view the YAML code of the virtual service

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: vs-demo
      namespace: default
    spec:
      hosts:
        - '*'
      http:
        - name: gw-to-productage
          match:
            - uri:
                exact: /productpage
            - uri:
                prefix: /static
            - uri:
                exact: /login
            - uri:
                exact: /logout
            - uri:
                prefix: /api/v1/products
          route:
            - destination:
                host: productpage
                port:
                  number: 9080
      gateways:
        - bookinfo-gateway
    

Step 3: Access the Bookinfo application

  1. Obtain the IP address of the ingress gateway.

    Method 1: by using the ASM console

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.

    3. On the Ingress Gateway page, obtain Service address.

    Method 2: by using the ACK console

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Network > Services.

    3. On the Services page, select istio-system from the Namespace drop-down list. In the External IP column, view the IP address of port 80 for the istio-ingressgateway.

  2. In the address bar of your browser, enter http://{IP address of the ingress gateway}/productpage and refresh the page 10 times to access the Bookinfo application.

    The Bookinfo application accesses the v1, v2, and v3 versions of the reviews service. You can see that the ratio of requests to the three versions of the reviews service is close to 1:1:1.

    yuque_diagram

Step 4: Create routing rules for different versions of a service

  1. 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. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > DestinationRule. On the page that appears, click Create.

    3. On the Create page, configure the following parameters and click Create.

      For more information about the parameters, see Destination Rule.

      Parameter

      Description

      Basic Information

      Set Namespace to default, specify a Name for the destination rule, and set Host to reviews.

      Service Version (subset)

      Click the Service Version (subset) tab. Then, click Add Service Version (subset) repeatedly to configure the following three subsets:

      • Subset 1: Set Name to v1. Click Add Label, and set Key to version and Value to v1.

      • Subset 2: Set Name to v2. Click Add Label, and set Key to version and Value to v2.

      • Subset 3: Set Name to v3. Click Add Label, and set Key to version and Value to v3.

      目标规则

      The following YAML code shows the destination rule that corresponds to the preceding configurations.

      Expand to view the YAML code of the destination rule

      apiVersion: networking.istio.io/v1beta1
      kind: DestinationRule
      metadata:
        name: reviews
        namespace: default
        labels: {}
      spec:
        host: reviews
        subsets:
          - name: v1
            labels:
              version: v1
          - name: v2
            labels:
              version: v2
          - name: v3
            labels:
              version: v3
      
  2. 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. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > VirtualService. On the page that appears, click Create.

    3. On the Create page, configure the following parameters and click Create.

      Parameter

      Description

      Basic Information

      Namespace

      For this example, select default.

      Name

      Specify a name for the virtual service.

      Gateways

      Turn on Apply To All Sidecars.

      Hosts

      Click Add Host. In the Add Host dialog box, set Namespace to default. In the Add Host section, select reviews, click the 添加 icon, and then click OK.

      HTTP Route

      Name

      Click the HTTP Route tab. Click Add Route, and then enter a route name in the Name field.

      Route destination

      Click Add Route Destination repeatedly to configure the following three route destinations:

      • Set Host to reviews, Subset to v1, and Weight to 10.

      • Set Host to reviews, Subset to v2, and Weight to 40.

      • Set Host to reviews, Subset to v3, and Weight to 50.

      虚拟服务2

      The following YAML code shows the virtual rule that corresponds to the preceding configurations.

      Expand to view the YAML code of the virtual service

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: reviews
        namespace: default
      spec:
        hosts:
          - reviews.default.svc.cluster.local
        http:
          - name: route
            route:
              - destination:
                  host: reviews
                  subset: v1
                weight: 10
              - destination:
                  host: reviews
                  subset: v2
                weight: 40
              - destination:
                  host: reviews
                  subset: v3
                weight: 50

Step 5: Check whether the traffic routing settings take effect

In the address bar of your browser, enter http://{IP address of the ingress gateway}/productpage. Refresh the page 10 times.

If traffic routed to the v1, v2, and v3 versions of the reviews service appears at a ratio close to 1:4:5, the traffic routing settings take effect.