<p>For example I want to run my node js application. I can create Deployment and expose it via Service.
In order to keep it running I need to deploy MongoDB. It will create its own service.
I can give it static service name like mongo.svc.cluster.local and hardcode it for my Node.js app. Kubernetes will take care of DNS etc.</p>

<p>I don't know if it is a good practice or not. But it doesn't feel like that.</p>

<p>I am trying to wrap my mind around Labels. I understand I can label my MongoDB service with "component: mongo" but I can't get the concept how can I use it for discovery MongoDB hostname and passing it as env var into my application.</p>

<p>Can someone explain that to me? </p>

zoonman
<p>You should <em>always</em> make these hostnames configurable; via an environment variable is easiest to set in a Docker/Kubernetes world.  Consider the scenario you're describing:</p>

<ul>
<li>When you're doing purely local development, you want to connect to the MongoDB on your desktop, which will likely be <code>localhost:27017</code>.</li>
<li>You can do basic integration testing of your Docker container with Docker Compose, and the host name will be an unqualified <code>mongodb:27017</code>.</li>
<li>In a Kubernetes environment you can want to use the FQDN <code>mongodb.default.svc.cluster.local:27017</code>, especially if you think the database might live in a different namespace.</li>
<li>There are good arguments to run a database outside Docker or Kubernetes, in which case you'd need to provide some other hostname.</li>
</ul>

<p>Almost every language gives some way to access the environment variables (Node's <code>process.env</code>, Python's <code>os.environ</code>, Ruby's <code>ENV</code>, ...) and so you can look this up with some default that makes sense.</p>

<pre class="lang-js prettyprint-override"><code>const mongoHost = process.env.MONGO_HOST || 'localhost:27017';
</code></pre>

<p>In your Kubernetes Deployment spec you can explicitly set this as an environment variable.</p>

<pre><code>- name: MONGO_HOST
  value: 'mongodb.default.svc.cluster.local:27017'
</code></pre>

<p>I would not worry about letting the application dynamically try to find the database; just pass in its location as configuration.  (You don't need to think about labels except to the extent that the tell the service what to serve.)</p>
