VirtualService
A VirtualService is an Istio resource that defines how requests to a service are routed. It adds matching rules, weighted traffic splits, timeouts, retries, and fault injection on top of default Kubernetes Service routing.
What it does
By default a Kubernetes Service balances requests across all matching pods. A VirtualService lets the sidecar proxies apply richer rules first: match on path or header, send a percentage to a new version, set a timeout, or retry failures.
orders-service"] --> M{"Match rules"} M -->|"header x-canary true"| V2["Subset v2"] M -->|"default"| W{"Weights"} W -->|"90 percent"| V1["Subset v1"] W -->|"10 percent"| V2b["Subset v2"] classDef start fill:#475569,color:#ffffff classDef decide fill:#FFEDD5,color:#1E293B classDef sub fill:#D1FAE5,color:#1E293B class C start class M,W decide class V1,V2,V2b sub
Structure
apiVersion: networking.istio.io/v1kind: VirtualServicemetadata: name: orders-service namespace: productionspec: hosts: - orders-service http: - match: - headers: x-canary: exact: "true" route: - destination: host: orders-service subset: v2 - route: - destination: host: orders-service subset: v1 weight: 90 - destination: host: orders-service subset: v2 weight: 10 timeout: 3s retries: attempts: 3 perTryTimeout: 1s retryOn: connect-failure,gateway-errorWhat you can match and control
| Match on | Route actions |
|---|---|
| Method, URI, headers, query params | Weighted destinations |
| Source labels | Timeout and retries |
| Port and gateways | Fault injection, redirects, rewrites |
Rules are evaluated in order and the first match wins. Put specific rules before a catch-all.
Requires a DestinationRule for subsets
If you reference subset: v2, a DestinationRule must define it. Validate before applying:
istioctl analyze -n productionRememberVirtualService controls how traffic is routed. DestinationRule defines what the destinations are and what connection policy applies.
Common MistakeApplying a VirtualService referencing a subset before the DestinationRule exists. Matching traffic gets 503 errors.
TipPlace broad catch-all routes last, since an earlier broad rule shadows later specific ones.
Frequently Asked Questions
Do I still need a Kubernetes Service if I use a VirtualService?
Yes. The Service provides the stable name and endpoint set. The VirtualService layers routing rules over it, and without one Istio just uses normal Service behaviour.
What is a common mistake with canary routing?
Defining weighted subsets without a matching DestinationRule, or placing a catch-all route ahead of a specific header match so it is never reached.
Can a VirtualService route traffic from outside the mesh?
Yes, when bound to an Istio Gateway through the gateways field, it routes external ingress traffic in the same way.