<p>I'm currently working on a case when we need to dynamically create services and provide access to them via URI subpaths of the main gateway.</p>
<p>I'm planning to use virtual services for traffic routing for them.  Virtual Service for a particular service should look like:</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: subpaths-routes
spec:
  hosts:
  - mainservice.prod.svc.cluster.local
  http:
  - name: &quot;subpath-redirection&quot;
    match:
    - uri:
        prefix: &quot;/bservices/svc-2345-6789&quot;
    route:
    - destination:
        host: svc-2345-6789.prod.svc.cluster.local
</code></pre>
<p>But there may be a huge number of such services (like thousands). All follow the same pattern of routing.
I would like to know if Istio has a mechanism to specify VirtualService with variables/parameters like the following:</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: subpaths-routes
spec:
  hosts:
  - mainservice.prod.svc.cluster.local
  http:
  - name: &quot;subpath-redirection&quot;
    match:
    - uri:
        prefix: &quot;/bservices/&quot;{{ variable }}
    route:
    - destination:
        host: {{ variable }}.prod.svc.cluster.local
</code></pre>
<p>In Nginx, one can do a similar thing by specifying something like this:</p>
<pre><code>location ~ /service/(?&lt;variable&gt;[0-9a-zA-Z\_\-]+)/ {
  proxy_pass http://$variable:8080;
}
</code></pre>
<p>Is there a way in Istio to accomplish that?
And if there is not, how would thousands of VSs impact the performance of request processing? Is It expensive to keep them in terms of CPU and RAM being consumed?</p>
<p>Thank you in advance!</p>

<blockquote>
<p>How to use variables in Istio VirtualService?</p>
</blockquote>
<p>As far as I know there is no such option in istio to specify a variable in prefix and host, if it was only a prefix then you could try with <a href="https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPMatchRequest" rel="nofollow noreferrer">regex</a> instead of prefix.</p>
<hr />
<p>If you would like to automate it in some way, I mean create a variable and put in in both, prefix and host, then you could try do to it with <a href="https://helm.sh/" rel="nofollow noreferrer">helm</a>.</p>
<p>There are few examples for virtual service in helm.</p>
<ul>
<li><a href="https://github.com/streamsets/helm-charts/blob/master/incubating/control-hub/templates/istio-gateway-virtualservice.yaml" rel="nofollow noreferrer">https://github.com/streamsets/helm-charts/blob/master/incubating/control-hub/templates/istio-gateway-virtualservice.yaml</a></li>
<li><a href="https://github.com/salesforce/helm-starter-istio/blob/master/templates/virtualService.yaml" rel="nofollow noreferrer">https://github.com/salesforce/helm-starter-istio/blob/master/templates/virtualService.yaml</a></li>
</ul>
<hr />
<blockquote>
<p>how would thousands of VSs impact the performance of request processing?</p>
</blockquote>
<p>There is <a href="https://github.com/istio/istio/issues/25685" rel="nofollow noreferrer">github issue</a> about that, as @lanceliuu mentioned there</p>
<blockquote>
<p>When we create ~1k virtualservices in a single cluster, the ingress gateway is picking up new virtualservice slowly.</p>
</blockquote>
<p>So that might be one of the issues with thousands of Virtual Services.</p>
<blockquote>
<p>Is It expensive to keep them in terms of CPU and RAM being consumed?</p>
</blockquote>
<p>I would say it would require testing. I checked in above github issue and they mentioned that there is no mem/cpu pressure for istio components, but I can't say how expensive is that.</p>
<p>In theory you could create 1 big virtual service instead of thousands, but as mentioned in <a href="https://istio.io/latest/docs/ops/best-practices/traffic-management/#split-virtual-services" rel="nofollow noreferrer">documentation</a> you should rather Split large virtual services into multiple resources.</p>
<hr />
<p>Additional resources:</p>
<ul>
<li><a href="https://engineering.hellofresh.com/everything-we-learned-running-istio-in-production-part-2-ff4c26844bfb" rel="nofollow noreferrer">https://engineering.hellofresh.com/everything-we-learned-running-istio-in-production-part-2-ff4c26844bfb</a></li>
<li><a href="https://istio.io/latest/docs/ops/deployment/performance-and-scalability/" rel="nofollow noreferrer">https://istio.io/latest/docs/ops/deployment/performance-and-scalability/</a></li>
<li><a href="https://perf.dashboard.istio.io/" rel="nofollow noreferrer">https://perf.dashboard.istio.io/</a></li>
</ul>
