<h1>1. Problem Description</h1>

<p>I'm trying to run a <code>MongoDB</code> <code>Deployment</code> + <code>Service</code> on <code>Kubernetes</code> where other <code>Pods</code>/<code>Jobs</code> or <code>Containers</code> could access it. So far I have apparently been successful at deploying it, however whenever I try to access it from within a <code>Job</code>, <code>Pod</code> or <code>Container</code>, I get (note that I'm using <code>0.0.0.0</code> instead of <code>localhost</code> in order to access the host machine; and that my timeout is 30 seconds):</p>

<pre class="lang-none prettyprint-override"><code>pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:30001: [Errno 111] Connection refused
</code></pre>

<h1>2. Locally, it seems to work...</h1>

<p>If I try to access it through a <code>Python</code> <code>CLI</code>, it <em>does</em> look like it works though:</p>

<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; import pymongo
&gt;&gt;&gt; client = pymongo.MongoClient(host='0.0.0.0', port=30001) # 'localhost' also works
&gt;&gt;&gt; client.list_database_names()
['admin', 'config', 'local', 'test_db'] # 'test_db' is a db I had previously created
</code></pre>

<p>Should I use another host address when trying to access the <code>MongoDB</code> <code>service</code>? (If so, where does it show on <code>kubectl describe svc &lt;service_name&gt;</code>?)</p>

<h1>3. <code>Deployment</code> and <code>Service</code> Configurations</h1>

<p>My <code>MongoDB</code> <code>deployment</code> (adapted from <a href="https://rads.stackoverflow.com/amzn/click/com/B072TS9ZQZ" rel="nofollow noreferrer" rel="nofollow noreferrer">Nigel Poulton's Kubernetes Book</a>) is:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: mymongodb-dep
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-mongo
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: hello-mongo
    spec:
      containers:
      - name: mongo
        image: mongo
        imagePullPolicy: IfNotPresent
        ports:
            - containerPort: 27017
</code></pre>

<p>And its <code>service</code> is:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels:
    app: hello-mongo
spec:
  type: NodePort
  ports:
  - port: 27017
    nodePort: 30001
    protocol: TCP
  selector:
    app: hello-mongo
</code></pre>

Philippe Fanaro
<p>Your connectivity experience from within the <code>Kubernetes</code> cluster and from outside will be different.</p>

<p>From within the cluster you should reference the <code>MongoDB</code> <code>Pod</code> using <code>&lt;service-name&gt;.&lt;namespace-name&gt;.svc.cluster.local</code> rather than <code>0.0.0.0</code>. So, in your case, the <code>host</code> would end up being <code>hello-svc.default.svc.cluster.local</code>.</p>

<p>Also note that the <code>port</code> should be referenced as the one seen within the cluster and not the <code>NodePort</code>, which is used to access the cluster from outside. In your case, that would be <code>27017</code>.</p>
