<p>I have a 'UI' and an 'API' microservice that I'm deploying on k8s default namespace with Istio enabled. My k8s environment is a dev box and doesn't have an External Load Balancer.</p>
<p>The UI's port configuration is 80(service port):80(container port in pod).<br />
The API's port configuration is 8000(service port):80(container port in pod)</p>
<p>I have to expose both these microservices for external traffic, since some people might use the 'UI' and some people might directly call the 'API' (via postman) for their requests.</p>
<p>When these microservices were running as simple docker containers without the k8s layer, users directly used the <code>host.example.com</code> for UI and <code>host.example.com:8000/api</code> for API calls (API calls are JSON-RPC).</p>
<p>I have a Gateway and VirtualService set up for both these microservices:</p>
<p>For UI:</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: ui-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
servers:
- port:
    number: 80
    name: http
    protocol: HTTP
  hosts:
    - host.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ui-vs
spec:
  hosts:
  - host.example.com
  gateways:
  - ui-gateway
  http:
  - route:
    - destination:
      port:
        number: 80
      host: ui --&gt; name of k8s svc
</code></pre>
<p>For API:</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: api-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - host.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: api-vs
spec:
  hosts:
  - host.example.com
  gateways:
  - api-gateway
http:
- route:
  - destination:
      host: api -&gt; name of api service
      port:
        number: 8000
</code></pre>
<p>Now going by the Istio documentation (<a href="https://istio.io/latest/docs/tasks/traffic-management/ingress/ingress-control/#accessing-ingress-services-using-a-browser" rel="nofollow noreferrer">accessing on browser</a>) to access this UI in the browser I need to access it via <code>${INGRESS_HOST}:${INGRES_PORT}</code>. In my case:</p>
<pre><code>INGRESS_HOST=host.example.com
INGRESS_PORT=31165
</code></pre>
<p>So accessing <a href="http://host.example.com:31165" rel="nofollow noreferrer">http://host.example.com:31165</a> loads the UI, how do I now access the API microservice externally on <code>host.example.com</code> via Postman etc? The 8000 API port is not accessible from outside. I guess it all has to go via 31165, but what route do I need to use to access the API directly? What changes do I need to do for this, if any, in my set-up? I have just started with Istio.</p>

user1452759
<p>Simplest solution is to combine both into one <code>VirtualService</code> and work with something like a path + a rewrite:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-vs
spec:
  hosts:
    - host.example.com
  gateways:
    - my-gateway
  http:
    - match:
      - uri:
          prefix: /api
      rewrite:
        uri: /
      route:
      - destination:
          host: api # -&gt; name of api service
          port:
            number: 8000
    - match:
      - uri:
          prefix: /ui
      rewrite:
        uri: /
      route:
      - destination:
          port:
            number: 80
          host: ui # --&gt; name of k8s svc
</code></pre>
<p>If you query <code>host.example.com:&lt;nodePort&gt;/api</code> you'll end up at the api host, and with <code>host.example.com:&lt;nodePort&gt;/ui</code> the the ui.</p>
<p>You could also use subdomains (<code>ui.example.com:&lt;nodePort&gt;</code> and <code>api.example.com:&lt;nodePort&gt;</code>) or route based on headers. Check out the <a href="https://istio.io/latest/docs/reference/config/networking/virtual-service/" rel="nofollow noreferrer">docs</a> for more examples.</p>
