我将应用程序部署在一个Pod中,将mongodb部署在另一个Pod中。如何在两个Pod之间建立连接?

时间:2019-10-17 11:38:19

标签: mongodb kubernetes

我在一个Pod中安装了我的Spring Boot应用程序,在另一个Pod中安装了mongodb。。我已经使用头盔图表将mongodb Pod解除了身份...在部署Spring Boot应用程序Pod时,连接拒绝连接mongodb。

我尝试了mongo --port 27017,它说

MongoDB Shell版本v3.6.3 连接到:mongodb://127.0.0.1:27017 / 2019-10-17T10:52:21.711 + 0000 W网络[thread1]无法连接到127.0.0.1:27017,输入(轮询后检查套接字是否错误),原因:连接被拒绝 2019-10-17T10:52:21.711 + 0000 E查询[thread1]错误:无法连接到服务器127.0.0.1:27017,连接尝试失败: connect@src/mongo/shell/mongo.js:251:13 @(连接):1:6 例外:连接失败

我在部署应用程序时的日志是

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]

我希望连接到mongodb

我的Yaml文件是

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 }}


2 个答案:

答案 0 :(得分:1)

您可以参考this,尝试创建连接字符串

否则

bitnami的

stable / mongodb似乎也是一个很好的解决方案,您可以检查here,一旦执行此头盔图以将其与mongodb连接,请遵循powershell生成的自述文件

答案 1 :(得分:1)

您需要一项服务才能实际连接到另一个吊舱。使用服务名称连接到mongo pod。

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

Mongodb掌舵图公开了服务'mongodb'https://github.com/helm/charts/blob/master/stable/mongodb/templates/svc-primary-rs.yaml

假设集群中安装了另一个mongodb客户端pod。 在此pod中,运行mongo --host mongodb进行连接

通过头盔图表安装吊舱时,会在控制台上打印一个有用的自述文件。使用此处显示的命令来调试连接问题