<p>I have been setting up a Kubernetes cluster for my work deployment. I have my MongoDB hosted on an external droplet and want to configure external service access.</p>

<p>I am following this <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services" rel="nofollow noreferrer">tutorial</a>.
When I apply the my configuration with kubectl everything seems to be running fine.</p>

<p>My setup service &amp; Endpoint</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
 name: mongo
spec:
 ports:
 - name: mongodb
   port: xxxx
------
kind: Endpoints
apiVersion: v1
metadata:
 name: mongo
subsets:
 - addresses:
     - ip: 159.89.x.x
   ports:
     - port: xxx
</code></pre>

<p>I am developing using typescript and this is how I currently set up my DB connection</p>

<pre><code>const MONGO_URI = `mongodb://${config.mongourl}:${config.mongoport}/${config.collection}?authSource=admin`;
 const options ={
    user:`${config.mngusername}`, 
    pass:`${config.mngpassword}`,
    keepAlive: true, 
    keepAliveInitialDelay: 300000,
    useNewUrlParser: true,
    useCreateIndex: true
 }
mongoose.connect(MONGO_URI, options);
</code></pre>

<p>My question is, how do I consume the setup service/deploymentsin my code.
<br><b>thanks</b></p>

<p>There are many ways to consume an external service, such as:</p>

<ol>
<li>Hardcoding the IP:Port of the service in your app.</li>
<li>Using environment variables in your app and injecting them through <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">configmaps</a>.</li>
<li>Creating a service/endpoint, <a href="http://EXTERNAL_MYSQL_SERVICE_SERVICE_HOST" rel="nofollow noreferrer">ExternalName</a> service, or a service with <a href="https://kubernetes.io/docs/concepts/services-networking/service/#external-ips" rel="nofollow noreferrer">External IPs</a>. This allows your applications to use
Kubernetes' <a href="https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services" rel="nofollow noreferrer">Service Discovery</a> mechanisms.</li>
</ol>

<p>In your case you can just use:<br>
 <code>const MONGO_URI = 'mongodb://mongo/${config.collection}?authSource=admin';</code><br>
as the name <code>mongo</code> will be mapped to <code>159.89.x.x:xxx</code></p>
