Connecting Mongodb in a pod to another nestjs api pod in kubernetes

Viewed 126

I'm using Hyper-V with minikube and kubectl in kubernetes, I'm unable to connect my Nestjs api to Mongo Pod through mongo service, 2 different deployments.

My Mongo.yml file with service:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: mongodb-deployment
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        - containerPort: 27017

---

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

My Api acp-api.yml file:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: acp-api-deployment
  labels:
    app: acp-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: acp-api
  template:
    metadata:
      labels:
        app: acp-api
    spec:
      containers:
      - name: acp-api
        image: acp/acp-rest-api
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000

Api Configuration where i have to give mongodb uri for connection :

import { Injectable } from '@nestjs/common'
import { MongooseModuleOptions, MongooseOptionsFactory } from '@nestjs/mongoose'

@Injectable()
export class MongooseConfigService implements MongooseOptionsFactory {
  private username: string = ''
  private password: string = ''

  constructor() {}

  createMongooseOptions():
    | Promise<MongooseModuleOptions>
    | MongooseModuleOptions {
    return {
      useCreateIndex: true,
      useNewUrlParser: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
      uri: 'mongodb://mongodb-service:27017/acp--db'
    }
  }
}

I have tried using mongo db service name with and without port, pod ip But unable to connect

0 Answers
Related