Configure URI path matching rules

更新时间:
复制 MD 格式

When multiple versions of a service run in parallel, the mesh proxy needs rules to decide which version handles each request. URI path matching rules in a VirtualService let you route HTTP requests to specific service versions based on the request path.

VirtualService resources support three URI match types:

Match typeSyntaxBehavior
Exactexact: "/hello"Matches only the specified path
Prefixprefix: "/api/v1"Matches any path that starts with the specified string
Regexregex: "/users/[0-9]+"Matches paths against a regular expression

Prerequisites

Before you begin, make sure that you have:

  • Completed the preparations and deployed the helloworld and sleep services. For more information, see Preparations

Verify that the sample services are running

Confirm that requests are load-balanced across all service versions before you add routing rules.

  1. Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and then open a shell in the sleep pod:

    kubectl exec -it deploy/sleep -- sh
  2. Send a request to the helloworld service:

    curl helloworld:5000/hello

    The response comes from either v1 or v2 at random:

    Hello version: v2, instance: helloworld-v2-6b96c5684-4****
    Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****

Create a destination rule

Define subsets for each version of the helloworld service so that routing rules can reference them. For more information, see Manage destination rules.

Apply the following DestinationRule:

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: helloworld
  namespace: default
spec:
  host: helloworld
  subsets:
    - labels:
        version: v1
      name: v1
    - labels:
        version: v2
      name: v2

Create a VirtualService with a URI match rule

Apply the following VirtualService to route requests that match /hello exactly to helloworld v1. For more information, see Manage virtual services.

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: helloworld-vs
  namespace: default
spec:
  hosts:
    - helloworld
  http:
    - match:
        - uri:
            exact: /hello
      route:
        - destination:
            host: helloworld
            subset: v1

Verify the routing rule

  1. Use kubectl to connect to the ACK cluster based on the information in the kubeconfig file, and then open a shell in the sleep pod:

    kubectl exec -it deploy/sleep -- sh
  2. Send a request to /hello:

    curl helloworld:5000/hello

    Every response now comes from v1 only:

    Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****

What's next