一个Pod中的Springboot应用程序无法通过Openshift连接到另一个Pod上的mongodb

时间:2020-01-09 05:27:58

标签: mongodb spring-boot kubernetes openshift okd

我正在尝试在OpenShift中部署mongodb pod(1个主要节点和2个辅助节点),它们已部署,但由于以下错误而进入“错误”状态,

exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating

在遵循以下adm政策后,mongo pod已成功启动并运行

oc adm policy add-scc-to-group anyuid system:authenticated

现在,我正在尝试部署一个连接到mongodb的springboot应用程序,但是由于以下错误,该pod多次重新启动,

 INFO Exception in monitor thread while connecting to server mongo-node-2.database:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.SocketTimeoutException: connect timed out
                        INFO Exception in monitor thread while connecting to server mongo-node-3.database:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.SocketTimeoutException: connect timed out
                        INFO Exception in monitor thread while connecting to server mongo-node-1.database:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.SocketTimeoutException: connect timed out
                        WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata
org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exceptio  is org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata
Caused by: org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata

当通过openshift部署时,springboot应用程序Pod可以连接到mongodb pod,因此我使用k8s创建了一个虚拟pod,并尝试连接mongodb pod,连接成功,

mongo mongo-node-1.database.svc (successful)

我能够通过NodePort从OpenShift集群外部通过NodePort连接到mongodb,使用dns(mongo-node-1.database.svc)从集群内部连接,也可以通过通过k8s创建的Pod,而不是通过OpenShift部署连接到。 / p>

我怀疑问题可能是由于OpenShift阻止springboot pod与mongodb pod连接的某些政策引起的。

请提出任何建议。

1 个答案:

答案 0 :(得分:1)

问题是由于一个项目(名称空间)中的Pod和服务无法访问OpenShift中另一个项目(名称空间)中的Pod和服务。

使用以下命令,并且现在可以访问其中的所有Pod和服务,反之亦然。

$ oc adm pod-network join-projects --to=<project1> <project2> <project3>

参考链接-https://docs.openshift.com/container-platform/3.11/admin_guide/managing_networking.html