i deployed my application in one pod and mongodb in other pod..how to make connection between two pods?

10/17/2019

i have my spring boot application in one pod and mongodb in other pod.. I had depolyed my mongodb pod using helm chart... while deploying the spring boot application pod says connection refused to connect mongodb..

I tried mongo --port 27017 it says that

MongoDB shell version v3.6.3 connecting to: mongodb://127.0.0.1:27017/ 2019-10-17T10:52:21.711+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused 2019-10-17T10:52:21.711+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed

my logs while deploying application is

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-17 11:32:11.971 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMessageGroupStore' defined in org.onap.dcae.analytics.web.config.MessageStoreConfig: Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar!/:2.0.5.RELEASE]

I expect to connect to mongodb

my yaml files are

values.yaml:

# Default values for tcagen2-mongodb.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  tag: stable
  pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  paths: []
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

# application configuration
mongo:
  nameOverride: tcagen2-mongo
  config:
    dbName: ServiceOrderDB
  service:
    name: tcagen2-mongohost
    internalPort: 27017
  nfsprovisionerPrefix: tcagen2
  sdnctlPrefix: tcagen2
  persistence:
    mountSubPath: tcagen2/mongo/data
    enabled: true
  disableNfsProvisioner: true


resources: {}

requirements.yaml:

dependencies:
  - name: common
    version: ~5.x-0
    repository: '@local'
  - name: mongo
    version: ~5.x-0
    repository: '@local'

charts.yaml:

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: tcagen2-mongodb
version: 0.1.0

service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "tcagen2-mongodb.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "tcagen2-mongodb.name" . }}
    helm.sh/chart: {{ include "tcagen2-mongodb.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "tcagen2-mongodb.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "tcagen2-mongodb.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "tcagen2-mongodb.name" . }}
    helm.sh/chart: {{ include "tcagen2-mongodb.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "tcagen2-mongodb.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "tcagen2-mongodb.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
-- Anusha
kubernetes
mongodb

2 Answers

10/17/2019

You need a service to actually connect to one pod from another. Use the service name to connect to the mongo pod.

https://github.com/helm/charts/tree/master/stable/mongodb

Mongodb helm chart exposes service 'mongodb' https://github.com/helm/charts/blob/master/stable/mongodb/templates/svc-primary-rs.yaml

Let's say there is another mongodb client pod installed in the cluster. From this pod, run mongo --host mongodb to connect

When installing pod through helm chart, a helpful readme is printed to the console. Use commands present there to debug connection issues

-- Palash Goel
Source: StackOverflow

10/17/2019

you can refer this, try making a connection string

or else

stable/mongodb by bitnami also appears to be a great solution with the same, you can check that here, please follow the readme generated at powershell once you execute this helm chart to connect it with mongodb

-- Tushar Mahajan
Source: StackOverflow