<p>I have Istio1.6 running in my k8 cluster. In the cluster I have also deployed sharded mongodb cluster <strong>with istio-injection disabled</strong>.</p>

<p>And I have a different namespace for my app <strong>with istio-injection enabled</strong>. And from the pod if I try to connect to the mongo I get this <strong>connection reset by peer error</strong>:</p>

<pre class="lang-sh prettyprint-override"><code>root@mongo:/# mongo "mongodb://mongo-sharded-cluster-mongos-0.mongo-service.mongodb.svc.cluster.local:27017,mongo-sharded-cluster-mongos-1.mongo-service.mongodb.svc.cluster.local:27017/?ssl=false"
MongoDB shell version v4.2.8
connecting to: mongodb://mongo-sharded-cluster-mongos-0.mongo-service.mongodb.svc.cluster.local:27017,mongo-sharded-cluster-mongos-1.mongo-service.mongodb.svc.cluster.local:27017/?compressors=disabled&amp;gssapiServiceName=mongodb&amp;ssl=false
2020-06-18T19:59:14.342+0000 I  NETWORK  [js] DBClientConnection failed to receive message from mongo-sharded-cluster-mongos-0.mongo-service.mongodb.svc.cluster.local:27017 - HostUnreachable: Connection reset by peer
2020-06-18T19:59:14.358+0000 I  NETWORK  [js] DBClientConnection failed to receive message from mongo-sharded-cluster-mongos-1.mongo-service.mongodb.svc.cluster.local:27017 - HostUnreachable: Connection reset by peer
2020-06-18T19:59:14.358+0000 E  QUERY    [js] Error: network error while attempting to run command 'isMaster' on host 'mongo-sharded-cluster-mongos-1.mongo-service.mongodb.svc.cluster.local:27017'  :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-06-18T19:59:14.362+0000 F  -        [main] exception: connect failed
2020-06-18T19:59:14.362+0000 E  -        [main] exiting with code 1
</code></pre>

<p>But if I <strong>disable the istio-injection</strong> to my app(pod) then I can successfully connect and use mongo as expected. </p>

<p><strong>Is there a work around for this, I would like to have istio-proxy injected to my app/pod and use mongodb?</strong></p>

Prata
<p>Injecting Databases with istio is complicated.</p>

<hr>

<p>I would start with checking your mtls, if it´s STRICT, I would change it to permissive and check if it works. It´s well described <a href="https://istio.io/latest/docs/tasks/security/authentication/authn-policy/#globally-enabling-istio-mutual-tls-in-strict-mode" rel="nofollow noreferrer">here</a>. </p>

<blockquote>
  <p>You see requests still succeed, except for <strong>those from the client that doesn’t have proxy, sleep.legacy, to the server with a proxy, httpbin.foo or httpbin.bar. This is expected because mutual TLS is now strictly required</strong>, but the workload without sidecar cannot comply.</p>
</blockquote>

<hr>

<blockquote>
  <p>Is there a work around for this, I would like to have istio-proxy injected to my app/pod and use mongodb?</p>
</blockquote>

<p>If changing mtls won´t work, then in istio You can set up database without injecting and then add it to istio registry using ServiceEntry object so it would be able to communicate with the rest of istio services.</p>

<p>To add your mongodb database to istio you can use <a href="https://istio.io/latest/docs/reference/config/networking/service-entry/" rel="nofollow noreferrer">ServiceEntry</a>.</p>

<blockquote>
  <p>ServiceEntry enables adding additional entries into Istio’s internal service registry, so that auto-discovered services in the mesh can access/route to these manually specified services. A service entry describes the properties of a service (DNS name, VIPs, ports, protocols, endpoints). These services could be external to the mesh (e.g., web APIs) or mesh-internal services that are not part of the platform’s service registry (e.g., a set of VMs talking to services in Kubernetes). In addition, the endpoints of a service entry can also be dynamically selected by using the workloadSelector field. These endpoints can be VM workloads declared using the WorkloadEntry object or Kubernetes pods. The ability to select both pods and VMs under a single service allows for migration of services from VMs to Kubernetes without having to change the existing DNS names associated with the services.</p>
</blockquote>

<p>Example of ServiceEntry</p>

<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: external-svc-mongocluster
spec:
  hosts:
  - mymongodb.somedomain # not used
  addresses:
  - 192.192.192.192/24 # VIPs
  ports:
  - number: 27018
    name: mongodb
    protocol: MONGO
  location: MESH_INTERNAL
  resolution: STATIC
  endpoints:
  - address: 2.2.2.2
  - address: 3.3.3.3
</code></pre>

<p>If You have mtls enabled You will also need DestinationRule that will define how to communicate with the external service.</p>

<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: mtls-mongocluster
spec:
  host: mymongodb.somedomain
  trafficPolicy:
    tls:
      mode: MUTUAL
      clientCertificate: /etc/certs/myclientcert.pem
      privateKey: /etc/certs/client_private_key.pem
      caCertificates: /etc/certs/rootcacerts.pem
</code></pre>

<hr>

<p>Additionally take a look at this documentation</p>

<ul>
<li><a href="https://istiobyexample.dev/databases/" rel="nofollow noreferrer">https://istiobyexample.dev/databases/</a></li>
<li><a href="https://istio.io/latest/blog/2018/egress-mongo/" rel="nofollow noreferrer">https://istio.io/latest/blog/2018/egress-mongo/</a></li>
</ul>
