<p>I have an API written in <code>Express</code>, that connects to a <code>Mongo</code> container. When I spin these up locally, I can connect to the mongo instance using something like <code>mongodb://${DB_HOST}:${DB_PORT}/${DB_NAME}</code> and setting <code>.env</code> variables.</p>

<p>What I am struggling to understand, is once this deployed to GKE, how will my API connect to the mongo container / pod?</p>

<p>It won't be running on localhost I assume, so perhaps I will need to use the internal IP created? </p>

<p>Should it actually be connecting via a <code>service</code>? What would that service look like?</p>

<p>I am struggling to find docs on exactly where I am stuck so I am thinking I am missing something really obvious.</p>

<p>I'm free new to <code>GKE</code> so any examples would help massively.</p>

<p>Create a mongodb deployment and a mongodb service of type ClusterIP, which basically means that your api will be able to connect to the db internally. If you want to connect your db from outside, create a service of type LoadBalancer or other service types (see <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">here</a>) </p>

<p>With a service of type ClusterIP, let's say you give it a <code>name</code> of <code>mongodbservice</code> under <code>metadata</code> key. Then your api can connect to it at <code>mongodb://mongodbservice:${DB_PORT}/${DB_NAME}</code></p>
