Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 395 of 8026. Kubernetes Traffic and ScalingFree lesson

Configuring Traffic Routing with VirtualServices

You will create VirtualService resources to define sophisticated traffic routing rules for directing requests between different versions of services.

In this lesson, you will learn how to use Istio VirtualServices to implement granular, version-aware traffic routing beyond standard Kubernetes load balancing.

Beyond Simple Load Balancing

While a standard Kubernetes Service acts as a basic load balancer, it distributes traffic randomly across all available pods. In a professional CI/CD pipeline, you often need more control—such as sending 90% of traffic to version v1 and 10% to a new v2 for canary testing.

A VirtualService acts as a smart traffic controller that intercepts requests destined for your service and applies routing rules based on headers, weights, or URI paths.

Defining Routing Rules

To use VirtualServices, you first define DestinationRules to label your deployments as specific "subsets" (e.g., v1 and v2). Once your subsets are defined, you create the VirtualService to instruct the mesh how to distribute the traffic:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-app-route
spec:
  hosts:
  - my-app
  http:
  - route:
    - destination:
        host: my-app
        subset: v1
      weight: 90
    - destination:
        host: my-app
        subset: v2
      weight: 10

Why This Matters

By configuring these rules, you decouple the deployment of your code from the exposure of your service. You can perform "A/B testing" by routing traffic based on specific headers (like user-agent or x-beta-tester) or gradually shift traffic as you gain confidence in a new release. Because these rules are handled by the Istio sidecars (Envoy proxies) injected into your pods, this routing logic remains entirely transparent to your application code. You no longer need to build complex load-balancing logic into your services; the mesh handles the distribution at the infrastructure layer.

Key Takeaway: VirtualServices provide the traffic-shaping precision required for sophisticated deployment strategies like canary releases by allowing you to route requests based on weighted subsets and request metadata.