title: Can not map and connect to local mongodb database using kubernetes external services
body: <p>First of all, who here thinks kubernetes documentation is really bulky and too hard to understand for someone who is starting? well I am one of those guys, as i am having  a hard time moving forward.</p>
  <p>I am trying to deploy my web application using minikube which is composed of frontend container, backend container and a mongoDb connection with from my local machine, because i dont want to deploy my database inside kubernetes cluster.</p>
  <p>And the way i have tried connecting to my MongoDB database is by using <strong>Services</strong> with <strong>EndPoints</strong>. For this purpose only I have binded multiple IP to connect to my mongo instance on my machine.</p>
  <p>This is my backend deployment:</p>
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: backend-deployment
    labels:
      backend: deployment
  spec:
    replicas: 2
    selector:
      matchLabels:
        app: backend
    template:
      metadata:
        labels:
          app: backend
      spec:
        containers:
          - name: backend
            image: app-api:latest
            imagePullPolicy: Never
            ports:
              - containerPort: 5000
                name: rest-api-port
            env:
              - name: DB_URL
                valueFrom:
                  configMapKeyRef:
                    name: mongo-config
                    key: mongoURL
  </code></pre>
  <p>This is my service for the backend deployment:</p>
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: backend-service
  spec:
    selector:
      app: backend
    type: ClusterIP
    ports:
      - protocol: TCP
        port: 80
        targetPort: rest-api-port
  </code></pre>
  <p>This is my Service for my MongoDb connection from my local machine:</p>
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: mongodb
  spec:
    ports:
      - port: 27017
        targetPort: 27017
  
  ---
  kind: Endpoints
  apiVersion: v1
  metadata:
    name: mongodb
  subsets:
    - addresses:
        - ip: 192.168.8.117
      ports:
        - port: 27017
  </code></pre>
  <p>And finally this is my config map for storing my mongo service name:</p>
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: mongo-config
  data:
    mongoURL: mongodb
  </code></pre>
  <p>But finally when i use this connection URL from the <strong>mongodb</strong> service  in my NodeJs  using mongoose, I can not create a connection successfully, it always return with error like so:</p>
  <pre><code>#NodeJs Implementation
      const connectionString ='mongodb://mongodb/dbName';
      await mongoose.connect(connectionString);
  </code></pre>
  <p>and this is the mongo error</p>
  <pre><code>MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
      at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:306:38)
      at listOnTimeout (node:internal/timers:559:17)
      at processTimers (node:internal/timers:502:7)
  TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:372:5)
      at URL.onParseError (node:internal/url:553:9)
      at new URL (node:internal/url:629:5)
      at isAtlas (/app/node_modules/mongoose/lib/helpers/topology/isAtlas.js:17:17)
      at MongooseServerSelectionError.assimilateError (/app/node_modules/mongoose/lib/error/serverSelection.js:35:35)
      at /app/node_modules/mongoose/lib/connection.js:825:36 {
    input: '127.0.0.1:27017',
    code: 'ERR_INVALID_URL'
  }
  </code></pre>
  <p>I have tried the above connection several times, even by changing the connection string like so: <code>'mongodb://mongodb.default.svc.cluster.local:27017/dbName'</code>, But still no luck. So can any one help me what i am doing wrong please????</p>