简体   繁体   English

Spring MongoDB - 如何连接到副本集?

[英]Spring MongoDB - How to connect to replica set?

I'm running locally a statefulset replica of MongoDB on Minikube and I'm trying to connect to this one using Spring Mongo DB.我在 Minikube 上本地运行 MongoDB 的有状态集副本,我正在尝试使用 Spring Mongo DB 连接到这个副本。

In the configuration properties I have:在我的配置属性中:

spring.data.mongodb.uri=mongodb://mongod-0.mongo:27017/test

The problem is that, when I try to deploy the application locally, I receive this error:问题是,当我尝试在本地部署应用程序时,收到此错误:

com.mongodb.MongoSocketException: mongod-0.mongo: Name or service not known

Looks like I can't enrich the deployed replica but I don't know why.看起来我无法丰富已部署的副本,但我不知道为什么。

The statefulset, the service and the pods are running correctly. statefulset、服务和 pod 运行正常。

Here is the configuration of them:这是它们的配置:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongod
spec:
  serviceName: mongodb-service
  replicas: 1
  selector:
    matchLabels:
      role: mongo
  template:
    metadata:
      labels:
        role: mongo
        environment: test
        replicaset: MainRepSet
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongod-container
          image: mongo
          command:
            - "mongod"
            - "--bind_ip"
            - "0.0.0.0"
            - "--replSet"
            - "MainRepSet"
          resources:
            requests:
              cpu: 0.2
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongodb-persistent-storage-claim
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: mongodb-persistent-storage-claim
        annotations:
          volume.beta.kubernetes.io/storage-class: "standard"
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi 
apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  labels:
    name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
  clusterIP: None
  selector:
    role: mongo

Someone has any idea how I could connect my application to the replica?有人知道如何将我的应用程序连接到副本吗?

it's due to your service name is: mongodb-service这是因为您的服务名称是: mongodb-service

You have to use always service name in connection strings everywhere mostly.大多数情况下,您必须在连接字符串中始终使用服务名称

Traffic flow goes like:交通流量是这样的:

Service -> deployment or statefulsets -> PODs

now you have exposed the service name and port(27017) and K8s will auto manage the service name into internal DNS, so you can use the name for the connection string.现在你已经暴露了服务名称和端口(27017),K8s 将自动管理服务名称到内部 DNS,所以你可以使用连接字符串的名称。

Your application will be only able to connect on service name if running on the same K8s cluster.如果在同一个 K8s 集群上运行,您的应用程序将只能连接服务名称

Example :示例

spring.data.mongodb.uri=mongodb://mongo-service:27017/test

You can follow this article also for refrence.您也可以关注这篇文章以供参考。 : https://medium.com/geekculture/how-to-deploy-spring-boot-and-mongodb-to-kube.netes-minikube-71c92c273d5e : https://medium.com/geekculture/how-to-deploy-spring-boot-and-mongodb-to-kube.netes-minikube-71c92c273d5e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM