简体   繁体   English

kubernetes中Mongodb集群的外部连接

[英]External connection to Mongodb cluster in kubernetes

I've used mongo k8s sidecar to provision a 3 member replica set mongo cluster on kubernetes.我已经使用mongo k8s sidecarkubernetes上配置了一个 3 成员副本集 mongo 集群。 I need to expose mongodb service externally and hence created a LoadBalancer.我需要在外部公开 mongodb 服务,因此创建了一个 LoadBalancer。

This is how the service looks like这是服务的样子

LoadBalancer Ingress:     xxx.yyy.elb.amazonaws.com
Port:                     <unset>  27017/TCP
TargetPort:               27017/TCP
NodePort:                 <unset>  30994/TCP
Endpoints:            100.14.1.3:27017,100.14.1.4:27017,100.14.2.5:27017

Trying to connect using mongodb shell 3.6 works fine mongo --host xxx.yyy.elb.amazonaws.com尝试使用 mongodb shell 3.6 连接工作正常mongo --host xxx.yyy.elb.amazonaws.com

But in the java client code I see the following exception.但在 java 客户端代码中,我看到以下异常。 java.net.UnknownHostException: mongo-1.mongo.dev.svc.cluster.local

I can confirm that the mongo pods are up and running.我可以确认 mongo pod 已启动并正在运行。 I am able to connect to mongo from other pods within the cluster - just not able to reach it externally.我能够从集群内的其他 pod 连接到 mongo - 只是无法从外部访问它。

Few things I don't understand is whats exactly happening in the java client.我不明白的几件事是 java 客户端中到底发生了什么。

The java client (which uses spring-data-mongo for configuration) is being created as follows. java 客户端(使用 spring-data-mongo 进行配置)正在创建如下。

MongoClient mongoClient = new MongoClient( "xxx.yyy.elb.amazonaws.com" , 27017 );

The fullstack trace is as follows全栈跟踪如下

Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@161f6623. Client view of cluster state is {type=REPLICA_SET, servers=[{address=mongo-2.mongo.dev.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo-2.mongo.dev.svc.cluster.local}, caused by {java.net.UnknownHostException: mongo-2.mongo.dev.svc.cluster.local}}, {address=mongo-0.mongo.dev.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo-0.mongo.dev.svc.cluster.local}, caused by {java.net.UnknownHostException: mongo-0.mongo.dev.svc.cluster.local}}, {address=mongo-1.mongo.dev.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongo-1.mongo.dev.svc.cluster.local}, caused by {java.net.UnknownHostException: mongo-1.mongo.dev.svc.cluster.local}}] at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:401) at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:120)

Why is the mongoClient using the pod name , even though I've passed the loadbalancer address?为什么 mongoClient 使用 pod name ,即使我已经传递了负载均衡器地址? How do I fix this?我该如何解决?

Thanks in advance提前致谢

You are getting an error for mongo-1.mongo.dev.svc.cluster.local and that's the internal endpoint within the cluster.您收到mongo-1.mongo.dev.svc.cluster.local错误,这是集群内的内部端点。 In other words, that's how you would get to your Mongo instance from other pods in the cluster.换句话说,这就是您从集群中的其他 pod 访问您的 Mongo 实例的方式。

On the Java client, you need to use xxx.yyy.elb.amazonaws.com:27017 as the Mongo endpoint configuration.在 Java 客户端上,您需要使用xxx.yyy.elb.amazonaws.com:27017作为 Mongo 端点配置。

Something like this:像这样的东西:

MongoClient mongoClient = new MongoClient( "xxx.yyy.elb.amazonaws.com" , 27017 );

To give you an overview of the path, your Mongo instance is exposed through a LoadBalancer Kubernetes Service on port 27017 .为了让您大致了解路径,您的 Mongo 实例通过端口27017上的LoadBalancer Kubernetes 服务公开。

Then the traffic comes into the load balancer and from there gets forwarded to your endpoints 100.14.1.3:27017 , etc.然后流量进入负载均衡器,并从那里转发到您的端点100.14.1.3:27017等。

Then from there, they enter the Node on the NodePort 30994 on each node.然后从那里,他们在每个节点的 NodePort 30994上输入节点。

Then the nodes that have a pod running reply with an answer.然后有一个 pod 运行的节点回复一个答案。

The Mongo process in the container itself runs on port 27017 so the moment the traffic gets to the node on port 30994 the container runtime forwards the traffic to your application in the container to 27017 .容器中的 Mongo 进程本身在端口27017上运行,因此当流量到达端口30994上的节点时,容器运行时会将流量转发到容器中的应用程序到27017

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

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