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 type | Syntax | Behavior |
|---|---|---|
| Exact | exact: "/hello" | Matches only the specified path |
| Prefix | prefix: "/api/v1" | Matches any path that starts with the specified string |
| Regex | regex: "/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.
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 -- shSend a request to the helloworld service:
curl helloworld:5000/helloThe 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: v2Create 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: v1Verify the routing rule
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 -- shSend a request to
/hello:curl helloworld:5000/helloEvery response now comes from v1 only:
Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****
What's next
Manage virtual services -- edit, update, or delete virtual service configurations.
Manage destination rules -- configure load balancing policies and connection pool settings.