kubernetes吊舱无法连接到Mongodb

kubernetes吊舱无法连接到Mongodb

本文介绍了Minikube/kubernetes吊舱无法连接到Mongodb Atlas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在尝试在Minkube中测试的Spring Boot应用程序,该应用程序应该连接到MongoDb Atlas.

I have a Spring Boot application that I'm trying to test in Minkube, which should connect to MongoDb Atlas.

在PC上运行应用程序时,它连接到我的数据库,在创建并作为Docker映像运行后,它也在我的PC上的Ubuntu VM上运行时连接.在Minikube上作为Kubernetes部署运行时,它不会连接到Mongodb Atlas.

It connects to my database when running the application on my pc, it also connects after creating and running as a Docker image, running on a Ubuntu VM on my PC. It does not connect to Mongodb Atlas when running as a Kubernetes deployment on Minikube.

必须缺少一个配置步骤,但是我不知道是什么.我认为我的问题是我需要做些特殊的事情来允许Minikube/Kubernetes查找外部URL吗?"

There must be a configuration step im missing, but i just dont know what. I think my question is "Do i need to do anything special to allow Minikube/Kubernetes to find external URLs"?

如果有人有任何建议,我会在下面张贴我的错误和当前的调查途径,

I will post my errors and current routes of investigation bellow, if anyone has any suggestions they would be greatly appreciated.

在Kubernetes中运行时出现嵌套错误(错误消息很长,所以只是相关的信息-我认为),因为您可以看到它找不到我的数据库URL,并抱怨找不到DNS名称".可以通过在Intelij中本地运行程序并稍微修改URL来复制此代码

Nested error (its a long error message so just the relevant bit - i think) when running in Kubernetes, as you can see its unable to find my database URL, and complains of "DNS name not found". Can replicate this by running the program locally in Intelij and mangling the URL a bit

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is com.mongodb.MongoConfigurationException: Unable to look up TXT record for host xxxxxx-erble.gcp.mongodb.net
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 128 common frames omitted
Caused by: com.mongodb.MongoConfigurationException: Unable to look up TXT record for host xxxxxx-erble.gcp.mongodb.net
        at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:131) ~[mongodb-driver-core-4.0.1.jar:na]
        at com.mongodb.ConnectionString.<init>(ConnectionString.java:378) ~[mongodb-driver-core-4.0.1.jar:na]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.applyHostAndPort(MongoClientFactorySupport.java:101) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.computeClientSettings(MongoClientFactorySupport.java:69) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:60) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:57) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 129 common frames omitted
Caused by: javax.naming.NameNotFoundException: DNS name not found [response code 3]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:664) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.isMatchResponse(DnsClient.java:582) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:430) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:214) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434) ~[jdk.naming.dns:na]
        at java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235) ~[na:na]
        at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141) ~[na:na]
        at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129) ~[na:na]
        at java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:171) ~[na:na]
        at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:114) ~[mongodb-driver-core-4.0.1.jar:na]
        ... 139 common frames omitted


我尝试过的事情:


Things i have tried:

  • 在我的deployment.yaml中:将dnsPolicy设置为Default(加上所有其他可能的值)
  • 创建外部服务,并使用externalName代替我的URL
kind: Service
apiVersion: v1
metadata:
  name: mongodbatlas
spec:
  type: ExternalName
  externalName: xxxxx-erble.gcp.mongodb.net

  • 创建允许所有出口的网络策略:
  • apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: allow-all-egress
    spec:
      podSelector: {}
      egress:
        - {}
      policyTypes:
        - Egress
    


    下面是我的部署和服务YAML,其中删除了个人用户名/密码/存储库详细信息:


    Below is my deployment and service YAML, with personal username/password/repository details removed:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: frontend
      labels:
        app: frontend
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: frontend
      template:
        metadata:
          labels:
            app: frontend
        spec:
          dnsPolicy: Default
          containers:
            - name: frontend
              image: <DOCKERRPO>/<REPO>:front
              imagePullPolicy: Always
              env:
                - name: MONGODB_ATLAS
                  value: mongodb+srv://<USERNAME>:<PASSWORD>@xxxxxx-erble.gcp.mongodb.net/test?retryWrites=true&w=majority
                - name: FRONT_SERVER_PORT
                  value: "8080"
                - name: spring_profiles_active
                  value: dev
              ports:
                - containerPort: 8080
              resources:
                limits:
                  memory: 512Mi
          imagePullSecrets:
            - name: <SECRET>
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: frontend
    spec:
      ports:
        - nodePort: 32000
          port: 80
          targetPort: 8080
      selector:
        app: frontend
      type: NodePort
    

    推荐答案

    已找到解决方案:

    由于某种原因,我的设置中的一个组件(spring boot/kubernetes/minikube/virtualbox等)在解决DNS SRV URI时出现问题.提供用于连接到我的集群的MongoDB Atlas URI失败,将其更改为折旧的URI样式似乎允许我的服务连接.

    For some reason, one of the components of my setup (spring boot/kubernetes/minikube/virtualbox etc.) has an issue resolving DNS SRV URIs. The MongoDB Atlas URI provided for connecting to my cluster fails, changing this to the depreciated style of URI seems to allow my service to connect.

    GAE无法查找SRV记录对于mongodb Atlas实例

    这篇关于Minikube/kubernetes吊舱无法连接到Mongodb Atlas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:01