<p>I have to get the existing microservices run. They are given as docker images.
They talk to each other by configured hostnames and ports.
I started to use Istio to view and configure the outgoing calls of each microservice.
Now I am at the point that I need to rewrite / redirect the host and the port of a request that goes out of one container.
How can I do that with Istio?</p>
<p>I will try to give a minimum example.
There are two services, service-a and service-b.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-b
spec:
  selector:
    matchLabels:
      run: service-b
  replicas: 1
  template:
    metadata:
      labels:
        run: service-b
    spec:
      containers:
        - name: service-b
          image: nginx
          ports:
            - containerPort: 80
              name: web
---
apiVersion: v1
kind: Service
metadata:
  name: service-b
  labels:
    run: service-b
spec:
  ports:
    - port: 8080
      protocol: TCP
      targetPort: 80
      name: service-b
  selector:
    run: service-b

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  selector:
    matchLabels:
      run: service-a
  replicas: 1
  template:
    metadata:
      labels:
        run: service-a
    spec:
      containers:
        - name: service-a
          image: nginx
          ports:
            - containerPort: 80
              name: web
---
apiVersion: v1
kind: Service
metadata:
  name: service-a
  labels:
    run: service-a
spec:
  ports:
    - port: 8081
      protocol: TCP
      targetPort: 80
      name: service-a
  selector:
    run: service-a

</code></pre>
<p>I can docker exec into service-a and successfully execute:</p>
<pre><code>root@service-a-d44f55d8c-8cp8m:/# curl -v service-b:8080

&lt; HTTP/1.1 200 OK
&lt; server: envoy

</code></pre>
<p>Now, to simulate my problem, I want to reach service-b by using another hostname and port. I want to configure Istio the way that this call will also work:</p>
<pre><code>root@service-a-d44f55d8c-8cp8m:/# curl -v service-x:7777
</code></pre>
<p>Best regards,
Christian</p>

<p>There are two solutions which can be used depending on necessity of using <code>istio</code> features.</p>
<p>If no <code>istio</code> features are planned to use, it can be solved using native kubernetes. In turn, if some <code>istio</code> feature are intended to use, it can be solved using <code>istio virtual service</code>. Below are two options:</p>
<hr />
<p><strong>1. Native kubernetes</strong></p>
<p><code>Service-x</code> should be pointed to the backend of <code>service-b</code> deployment. Below is <code>selector</code> which points to <code>deployment: service-b</code>:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-x
  labels:
    run: service-x
spec:
  ports:
    - port: 7777
      protocol: TCP
      targetPort: 80
      name: service-x
  selector:
    run: service-b
</code></pre>
<p>This way request will go through <code>istio</code> anyway because sidecar containers are injected.</p>
<pre><code># curl -vI service-b:8080

*   Trying xx.xx.xx.xx:8080...
* Connected to service-b (xx.xx.xx.xx) port 8080 (#0)
&gt; Host: service-b:8080
&lt; HTTP/1.1 200 OK
&lt; server: envoy
</code></pre>
<p>and</p>
<pre><code># curl -vI service-x:7777

*   Trying yy.yy.yy.yy:7777...
* Connected to service-x (yy.yy.yy.yy) port 7777 (#0)
&gt; Host: service-x:7777
&lt; HTTP/1.1 200 OK
&lt; server: envoy
</code></pre>
<hr />
<p><strong>2. Istio virtual service</strong></p>
<p>In this example <a href="https://istio.io/latest/docs/reference/config/networking/virtual-service" rel="nofollow noreferrer">virtual service</a> is used. Service <code>service-x</code> still needs to be created, but now we don't specify any selectors:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-x
  labels:
    run: service-x
spec:
  ports:
    - port: 7777
      protocol: TCP
      targetPort: 80
      name: service-x
</code></pre>
<p>Test it from another pod:</p>
<pre><code># curl -vI service-x:7777

*   Trying yy.yy.yy.yy:7777...
* Connected to service-x (yy.yy.yy.yy) port 7777 (#0)
&gt; Host: service-x:7777
&lt; HTTP/1.1 503 Service Unavailable
&lt; server: envoy
</code></pre>
<p><code>503</code> error which is expected. Now creating <code>virtual service</code> which will route requests to <code>service-b</code> on <code>port: 8080</code>:</p>
<pre><code>apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: service-x-to-b
spec:
  hosts:
  - service-x
  http:
  - route:
    - destination:
        host: service-b
        port:
          number: 8080
</code></pre>
<p>Testing from the pod:</p>
<pre><code># curl -vI service-x:7777

*   Trying yy.yy.yy.yy:7777...
* Connected to service-x (yy.yy.yy.yy) port 7777 (#0)
&gt; Host: service-x:7777
&lt; HTTP/1.1 200 OK
&lt; server: envoy
</code></pre>
<p>See it works as expected.</p>
<hr />
<p>Useful links:</p>
<ul>
<li><a href="https://istio.io/latest/docs/reference/config/networking/virtual-service" rel="nofollow noreferrer">Istio virtual service</a></li>
<li><a href="https://istio.io/latest/docs/reference/config/networking/virtual-service/#Destination" rel="nofollow noreferrer">virtual service - destination</a></li>
</ul>
